diff --git a/application/box/business.go b/application/box/business.go new file mode 100644 index 0000000..bf8ce54 --- /dev/null +++ b/application/box/business.go @@ -0,0 +1,13 @@ +package box + +import ( + "context" + + "github.com/google/uuid" +) + +// GetMyBox ... +func GetMyBox(ctx context.Context, uid *uuid.UUID) (res *MyBoxesRes, err error) { + res = new(MyBoxesRes) + return +} diff --git a/application/box/model.go b/application/box/model.go new file mode 100644 index 0000000..7385454 --- /dev/null +++ b/application/box/model.go @@ -0,0 +1,14 @@ +package box + +// MyBoxesRes ... +type MyBoxesRes struct { + Boxes []*Box `json:"boxes,omitempty"` +} + +// Box ... +type Box struct { + Id *string `json:"id,omitempty"` + Name *string `json:"name" binding:"required,min=3,max=30"` + Desc *string `json:"desc,omitempty"` + CreatedByID *string `json:"created_by_id,omitempty"` +} diff --git a/application/permission/business.go b/application/permission/business.go new file mode 100644 index 0000000..1e4147a --- /dev/null +++ b/application/permission/business.go @@ -0,0 +1,18 @@ +package permission + +import ( + "context" + "time" + + "github.com/google/uuid" + "github.com/isaqueveras/powersso/utils" +) + +// GetPermissions ... +func GetPermissions(ctx context.Context, pid *uuid.UUID) (res *PermissionsRes, err error) { + res = new(PermissionsRes) + res.ProjectID = pid + res.DateCache = utils.Pointer(time.Now()) + res.Permission = utils.Pointer(uint64(9003840938243279983)) + return res, nil +} diff --git a/application/permission/model.go b/application/permission/model.go new file mode 100644 index 0000000..ebbb60e --- /dev/null +++ b/application/permission/model.go @@ -0,0 +1,14 @@ +package permission + +import ( + "time" + + "github.com/google/uuid" +) + +// PermissionsRes models ... +type PermissionsRes struct { + ProjectID *uuid.UUID `json:"pid"` + Permission *uint64 `json:"permission"` + DateCache *time.Time `json:"date_cache,omitempty"` +} diff --git a/application/project/business.go b/application/project/business.go index feec9c7..d5dfb6f 100644 --- a/application/project/business.go +++ b/application/project/business.go @@ -23,7 +23,7 @@ func CreateNewProject(ctx context.Context, in *NewProject) (err error) { data := &domain.CreateProject{ Name: in.Name, - Description: in.Description, + Desc: in.Description, Slug: in.Slug, Url: in.Url, CreatedByID: in.CreatedByID, diff --git a/delivery/http/box/handler.go b/delivery/http/box/handler.go new file mode 100644 index 0000000..e1d96ed --- /dev/null +++ b/delivery/http/box/handler.go @@ -0,0 +1,28 @@ +package box + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/isaqueveras/powersso/application/box" + "github.com/isaqueveras/powersso/middleware" + "github.com/isaqueveras/powersso/oops" + "github.com/isaqueveras/powersso/utils" +) + +func getMyBoxes(ctx *gin.Context) { + uid, err := uuid.Parse(middleware.GetSession(ctx).UserID) + if err != nil { + oops.Handling(ctx, err) + return + } + + boxes, err := box.GetMyBox(ctx, utils.Pointer(uid)) + if err != nil { + oops.Handling(ctx, err) + return + } + + ctx.JSON(http.StatusOK, boxes) +} diff --git a/delivery/http/box/handler_test.go b/delivery/http/box/handler_test.go new file mode 100644 index 0000000..99b4d55 --- /dev/null +++ b/delivery/http/box/handler_test.go @@ -0,0 +1 @@ +package box diff --git a/delivery/http/box/router.go b/delivery/http/box/router.go new file mode 100644 index 0000000..2ece617 --- /dev/null +++ b/delivery/http/box/router.go @@ -0,0 +1,8 @@ +package box + +import "github.com/gin-gonic/gin" + +// Router ... +func Router(r *gin.RouterGroup) { + r.GET("/my", getMyBoxes) +} diff --git a/delivery/http/permissions/handler.go b/delivery/http/permissions/handler.go index 12cd541..ffa913e 100644 --- a/delivery/http/permissions/handler.go +++ b/delivery/http/permissions/handler.go @@ -4,8 +4,25 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/isaqueveras/powersso/application/permission" + "github.com/isaqueveras/powersso/oops" + "github.com/isaqueveras/powersso/utils" ) +// @Router /v1/permission [get] func getPermission(ctx *gin.Context) { - ctx.JSON(http.StatusOK, []string{}) + pid, err := uuid.Parse(ctx.Param("pid")) + if err != nil { + oops.Handling(ctx, err) + return + } + + permissions, err := permission.GetPermissions(ctx, utils.Pointer(pid)) + if err != nil { + oops.Handling(ctx, err) + return + } + + ctx.JSON(http.StatusOK, permissions) } diff --git a/delivery/http/permissions/router.go b/delivery/http/permissions/router.go index 5c29a91..4678a32 100644 --- a/delivery/http/permissions/router.go +++ b/delivery/http/permissions/router.go @@ -4,5 +4,5 @@ import "github.com/gin-gonic/gin" // Router is the router for the permission module. func Router(r *gin.RouterGroup) { - r.GET("", getPermission) + r.GET(":pid", getPermission) } diff --git a/domain/project/model.go b/domain/project/model.go index 86d568d..bb188ca 100644 --- a/domain/project/model.go +++ b/domain/project/model.go @@ -7,7 +7,7 @@ package project // CreateProject models the data to create a project type CreateProject struct { Name *string - Description *string + Desc *string Slug *string Url *string CreatedByID *string diff --git a/infrastructure/persistencie/project/postgres.go b/infrastructure/persistencie/project/postgres.go index 5d18dbc..ce3b5e1 100644 --- a/infrastructure/persistencie/project/postgres.go +++ b/infrastructure/persistencie/project/postgres.go @@ -20,8 +20,8 @@ type database struct { func (db *database) createNewProject(ctx context.Context, in *project.CreateProject) error { _, err := db.DB.Builder. Insert("projects"). - Columns("name", "description", "slug", "created_by", "url"). - Values(in.Name, in.Description, in.Slug, in.CreatedByID, in.Url). + Columns("name", "desc", "slug", "created_by", "url"). + Values(in.Name, in.Desc, in.Slug, in.CreatedByID, in.Url). ExecContext(ctx) return err } diff --git a/migrations/20220812135531_create_table_users.up.sql b/migrations/20220812135531_create_table_users.up.sql index 0aa23fd..89111cd 100644 --- a/migrations/20220812135531_create_table_users.up.sql +++ b/migrations/20220812135531_create_table_users.up.sql @@ -6,21 +6,21 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE TYPE "level" AS ENUM ('user', 'admin', 'integration'); CREATE TABLE users ( - id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - first_name VARCHAR(20) NOT NULL CHECK ( first_name <> '' ), - last_name VARCHAR(32) NOT NULL CHECK ( last_name <> '' ), - email VARCHAR(64) UNIQUE NOT NULL CHECK ( email <> '' ), - "password" VARCHAR(150) NOT NULL CHECK ( octet_length(password) <> 8 ), - "level" "level" NOT NULL DEFAULT 'user', - flag INTEGER NOT NULL DEFAULT 0, - "key" VARCHAR(50) NOT NULL, - active BOOLEAN NOT NULL DEFAULT TRUE, - attempts INTEGER NOT NULL DEFAULT 0, - last_failure TIMESTAMP, - otp VARCHAR, - created_by UUID, - created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), - last_login TIMESTAMP WITH TIME ZONE + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + "first_name" VARCHAR(20) NOT NULL CHECK ( first_name <> '' ), + "last_name" VARCHAR(32) NOT NULL CHECK ( last_name <> '' ), + "email" VARCHAR(64) UNIQUE NOT NULL CHECK ( email <> '' ), + "password" VARCHAR(150) NOT NULL CHECK ( octet_length(password) <> 8 ), + "level" "level" NOT NULL DEFAULT 'user', + "flag" INTEGER NOT NULL DEFAULT 0, + "key" VARCHAR(50) NOT NULL, + "active" BOOLEAN NOT NULL DEFAULT TRUE, + "attempts" INTEGER NOT NULL DEFAULT 0, + "last_failure" TIMESTAMP, + "otp" VARCHAR, + "created_by" UUID, + "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + "last_login" TIMESTAMP WITH TIME ZONE ); CREATE INDEX users_email_idx ON public.users (email); diff --git a/migrations/20220812140006_create_table_sessions.up.sql b/migrations/20220812140006_create_table_sessions.up.sql index e789d98..22f2a22 100644 --- a/migrations/20220812140006_create_table_sessions.up.sql +++ b/migrations/20220812140006_create_table_sessions.up.sql @@ -3,11 +3,11 @@ -- license that can be found in the LICENSE file. CREATE TABLE "sessions" ( - id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - user_id UUID NOT NULL REFERENCES users (id), - ip VARCHAR NOT NULL, - user_agent VARCHAR NOT NULL, - expires_at TIMESTAMP WITH TIME ZONE NOT NULL, - created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP WITH TIME ZONE + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + "user_id" UUID NOT NULL REFERENCES users (id), + "ip" VARCHAR NOT NULL, + "user_agent" VARCHAR NOT NULL, + "expires_at" TIMESTAMP WITH TIME ZONE NOT NULL, + "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + "deleted_at" TIMESTAMP WITH TIME ZONE ); diff --git a/migrations/20220812140141_create_projects_table.up.sql b/migrations/20220812140141_create_projects_table.up.sql index 8463ff9..119ef89 100644 --- a/migrations/20220812140141_create_projects_table.up.sql +++ b/migrations/20220812140141_create_projects_table.up.sql @@ -3,12 +3,11 @@ -- license that can be found in the LICENSE file. CREATE TABLE projects ( - "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - "created_by" UUID NOT NULL REFERENCES users (id), - "name" VARCHAR(20) NOT NULL, - "description" VARCHAR(50), - "url" VARCHAR(200) NOT NULL, - "slug" VARCHAR(20) NOT NULL, - "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), - "updated_at" TIMESTAMP WITH TIME ZONE + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" VARCHAR(30) NOT NULL, + "desc" VARCHAR(50), + "url" VARCHAR(200) NOT NULL, + "created_by" UUID NOT NULL REFERENCES users (id), + "updated_at" TIMESTAMP WITH TIME ZONE, + "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); diff --git a/migrations/20231126221000_create_table_boxes.down.sql b/migrations/20231126221000_create_table_boxes.down.sql new file mode 100644 index 0000000..0652964 --- /dev/null +++ b/migrations/20231126221000_create_table_boxes.down.sql @@ -0,0 +1,5 @@ +-- Copyright (c) 2023 Isaque Veras +-- Use of this source code is governed by MIT style +-- license that can be found in the LICENSE file. + +DROP TABLE IF EXISTS "boxes" CASCADE; diff --git a/migrations/20231126221000_create_table_boxes.up.sql b/migrations/20231126221000_create_table_boxes.up.sql new file mode 100644 index 0000000..07690ed --- /dev/null +++ b/migrations/20231126221000_create_table_boxes.up.sql @@ -0,0 +1,13 @@ +-- Copyright (c) 2023 Isaque Veras +-- Use of this source code is governed by MIT style +-- license that can be found in the LICENSE file. + +CREATE TABLE boxes ( + "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + "name" VARCHAR(30) NOT NULL, + "desc" VARCHAR(50), + "project_id" UUID NOT NULL REFERENCES projects (id), + "flag" BIGINT NOT NULL DEFAULT 0, + "created_by" UUID NOT NULL REFERENCES users (id), + "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), +); diff --git a/migrations/20231126225334_create_table_users_boxes.down.sql b/migrations/20231126225334_create_table_users_boxes.down.sql new file mode 100644 index 0000000..4cf406c --- /dev/null +++ b/migrations/20231126225334_create_table_users_boxes.down.sql @@ -0,0 +1,5 @@ +-- Copyright (c) 2023 Isaque Veras +-- Use of this source code is governed by MIT style +-- license that can be found in the LICENSE file. + +DROP TABLE IF EXISTS "users_boxes" CASCADE; diff --git a/migrations/20231126225334_create_table_users_boxes.up.sql b/migrations/20231126225334_create_table_users_boxes.up.sql new file mode 100644 index 0000000..2c9e42c --- /dev/null +++ b/migrations/20231126225334_create_table_users_boxes.up.sql @@ -0,0 +1,11 @@ +-- Copyright (c) 2023 Isaque Veras +-- Use of this source code is governed by MIT style +-- license that can be found in the LICENSE file. + +CREATE TABLE users_boxes ( + id UUID DEFAULT uuid_generate_v4(), + user_id UUID NOT NULL REFERENCES users (id), + box_id UUID NOT NULL REFERENCES boxes (id), + created_by UUID NOT NULL REFERENCES users (id), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), +); diff --git a/server/rest.go b/server/rest.go index 18cb411..4338466 100644 --- a/server/rest.go +++ b/server/rest.go @@ -13,6 +13,7 @@ import ( "github.com/isaqueveras/endless" "github.com/isaqueveras/powersso/delivery/http/auth" + "github.com/isaqueveras/powersso/delivery/http/box" "github.com/isaqueveras/powersso/delivery/http/permissions" "github.com/isaqueveras/powersso/delivery/http/project" "github.com/isaqueveras/powersso/middleware" @@ -42,6 +43,7 @@ func (s *Server) ServerHTTP() (err error) { auth.Router(v1.Group("auth")) auth.RouterAuthorization(v1.Group("auth", middleware.Auth())) project.Router(v1.Group("project", middleware.Auth())) + box.Router(v1.Group("box", middleware.Auth())) permissions.Router(v1.Group("permission", middleware.Auth())) endless.DefaultReadTimeOut = s.cfg.Server.ReadTimeout * time.Second