Skip to content

Commit

Permalink
feat: init box module
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Nov 27, 2023
1 parent 65e8f5e commit 693c74a
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 36 deletions.
13 changes: 13 additions & 0 deletions application/box/business.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions application/box/model.go
Original file line number Diff line number Diff line change
@@ -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"`
}
18 changes: 18 additions & 0 deletions application/permission/business.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions application/permission/model.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 1 addition & 1 deletion application/project/business.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions delivery/http/box/handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions delivery/http/box/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package box
8 changes: 8 additions & 0 deletions delivery/http/box/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package box

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

// Router ...
func Router(r *gin.RouterGroup) {
r.GET("/my", getMyBoxes)
}
19 changes: 18 additions & 1 deletion delivery/http/permissions/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion delivery/http/permissions/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion domain/project/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions infrastructure/persistencie/project/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 15 additions & 15 deletions migrations/20220812135531_create_table_users.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
14 changes: 7 additions & 7 deletions migrations/20220812140006_create_table_sessions.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
15 changes: 7 additions & 8 deletions migrations/20220812140141_create_projects_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
5 changes: 5 additions & 0 deletions migrations/20231126221000_create_table_boxes.down.sql
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions migrations/20231126221000_create_table_boxes.up.sql
Original file line number Diff line number Diff line change
@@ -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(),
);
5 changes: 5 additions & 0 deletions migrations/20231126225334_create_table_users_boxes.down.sql
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions migrations/20231126225334_create_table_users_boxes.up.sql
Original file line number Diff line number Diff line change
@@ -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(),
);
2 changes: 2 additions & 0 deletions server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 693c74a

Please sign in to comment.