Skip to content

Commit

Permalink
feat(organization): init module
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Apr 13, 2024
1 parent ddbc385 commit 35b7d6c
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 1 deletion.
39 changes: 39 additions & 0 deletions application/authorization/organization/business.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import (
"context"

db "github.com/isaqueveras/powersso/database/postgres"
domain "github.com/isaqueveras/powersso/domain/authorization/organization"
infra "github.com/isaqueveras/powersso/infrastructure/persistencie/authorization/organization"
"github.com/isaqueveras/powersso/oops"
)

// Create is the business logic for creating a new room
func Create(ctx context.Context, in *Organization) (err error) {
var tx *db.Transaction
if tx, err = db.NewTransaction(ctx, false); err != nil {
return oops.Err(err)
}
defer tx.Rollback()

repo := infra.New(ctx, tx)
if err = repo.Create(&domain.Organization{
Name: in.Name,
Desc: in.Description,
URL: in.Url,
CreatedByID: in.CreatedByID,
}); err != nil {
return oops.Err(err)
}

if err = tx.Commit(); err != nil {
return oops.Err(err)
}

return nil
}
16 changes: 16 additions & 0 deletions application/authorization/organization/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import "github.com/google/uuid"

// Organization models the data to create a organization
type Organization struct {
ID *uuid.UUID `json:"-"`
Name *string `json:"name" binding:"required,min=3,max=20"`
Description *string `json:"description"`
Url *string `json:"url" binding:"required,max=200"`
CreatedByID *uuid.UUID `json:"-"`
}
36 changes: 36 additions & 0 deletions delivery/http/authorization/organization/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/isaqueveras/powersso/application/authorization/organization"
"github.com/isaqueveras/powersso/middleware"
"github.com/isaqueveras/powersso/oops"
"github.com/isaqueveras/powersso/utils"
)

// @Router /v1/room/create [post]
func create(ctx *gin.Context) {
var (
in = new(organization.Organization)
session = middleware.GetSession(ctx)
)

if err := ctx.ShouldBindJSON(&in); err != nil {
oops.Handling(ctx, err)
return
}

in.CreatedByID = utils.Pointer(session.UserID)
if err := organization.Create(ctx, in); err != nil {
oops.Handling(ctx, err)
return
}

ctx.JSON(http.StatusCreated, utils.NoContent{})
}
5 changes: 5 additions & 0 deletions delivery/http/authorization/organization/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization
12 changes: 12 additions & 0 deletions delivery/http/authorization/organization/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import "github.com/gin-gonic/gin"

// Router is the router for the room module.
func Router(r *gin.RouterGroup) {
r.POST("create", create)
}
10 changes: 10 additions & 0 deletions domain/authorization/organization/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

// IOrganization define an interface for data layer access methods
type IOrganization interface {
Create(*Organization) error
}
21 changes: 21 additions & 0 deletions domain/authorization/organization/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import (
"time"

"github.com/google/uuid"
)

// Organization models the data of a organization
type Organization struct {
ID *uuid.UUID
Name *string
Desc *string
URL *string
CreatedByID *uuid.UUID
CreatedAt *time.Time
}
4 changes: 3 additions & 1 deletion domain/authorization/permission/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

package permission

import "github.com/google/uuid"

// Permission ...
type Permission struct {
Name *string
Credential *string
CreatedByID *string
CreatedByID *uuid.UUID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022 Isaque Veras
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package organization

import (
"context"

"github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/domain/authorization/organization"
"github.com/isaqueveras/powersso/oops"
)

// repository is the implementation of the session repository
type repository struct {
pg *postgres.Transaction
ctx context.Context
}

// New creates a new repository
func New(ctx context.Context, tx *postgres.Transaction) organization.IOrganization {
return &repository{ctx: ctx, pg: tx}
}

// Create contains the flow for create organization in database
func (r *repository) Create(org *organization.Organization) error {
if _, err := r.pg.Builder.
Insert("organization").
Columns("name", "desc", "created_by", "url").
Values(org.Name, org.Desc, org.CreatedByID, org.URL).
ExecContext(r.ctx); err != nil {
return oops.Err(err)
}
return nil
}

0 comments on commit 35b7d6c

Please sign in to comment.