diff --git a/application/authorization/organization/business.go b/application/authorization/organization/business.go new file mode 100644 index 0000000..00b038f --- /dev/null +++ b/application/authorization/organization/business.go @@ -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 +} diff --git a/application/authorization/organization/model.go b/application/authorization/organization/model.go new file mode 100644 index 0000000..e291ecd --- /dev/null +++ b/application/authorization/organization/model.go @@ -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:"-"` +} diff --git a/delivery/http/authorization/organization/handler.go b/delivery/http/authorization/organization/handler.go new file mode 100644 index 0000000..a496bd6 --- /dev/null +++ b/delivery/http/authorization/organization/handler.go @@ -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{}) +} diff --git a/delivery/http/authorization/organization/handler_test.go b/delivery/http/authorization/organization/handler_test.go new file mode 100644 index 0000000..9e3f7ee --- /dev/null +++ b/delivery/http/authorization/organization/handler_test.go @@ -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 diff --git a/delivery/http/authorization/organization/router.go b/delivery/http/authorization/organization/router.go new file mode 100644 index 0000000..304978d --- /dev/null +++ b/delivery/http/authorization/organization/router.go @@ -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) +} diff --git a/domain/authorization/organization/interface.go b/domain/authorization/organization/interface.go new file mode 100644 index 0000000..25d869c --- /dev/null +++ b/domain/authorization/organization/interface.go @@ -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 +} diff --git a/domain/authorization/organization/model.go b/domain/authorization/organization/model.go new file mode 100644 index 0000000..4bfc603 --- /dev/null +++ b/domain/authorization/organization/model.go @@ -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 +} diff --git a/domain/authorization/permission/model.go b/domain/authorization/permission/model.go index e8a69a1..1a5e670 100644 --- a/domain/authorization/permission/model.go +++ b/domain/authorization/permission/model.go @@ -4,9 +4,11 @@ package permission +import "github.com/google/uuid" + // Permission ... type Permission struct { Name *string Credential *string - CreatedByID *string + CreatedByID *uuid.UUID } diff --git a/infrastructure/persistencie/authorization/organization/repository.go b/infrastructure/persistencie/authorization/organization/repository.go new file mode 100644 index 0000000..0365e3e --- /dev/null +++ b/infrastructure/persistencie/authorization/organization/repository.go @@ -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 +}