Skip to content

Commit

Permalink
refact: create new project
Browse files Browse the repository at this point in the history
  • Loading branch information
isaqueveras committed Nov 25, 2023
1 parent ec43c3b commit 65e8f5e
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 217 deletions.
34 changes: 16 additions & 18 deletions application/project/business.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,32 @@ package project
import (
"context"

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

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

var data *domain.CreateProject
if data, err = utils.TypeConverter[domain.CreateProject](&input); err != nil {
return oops.Err(err)
}

repo := infra.New(transaction)
if err = repo.Create(data); err != nil {
return oops.Err(err)
defer tx.Rollback()

data := &domain.CreateProject{
Name: in.Name,
Description: in.Description,
Slug: in.Slug,
Url: in.Url,
CreatedByID: in.CreatedByID,
}

if err = transaction.Commit(); err != nil {
repo := infra.New(ctx, tx)
if err = repo.CreateNewProject(data); err != nil {
return oops.Err(err)
}

return
return tx.Commit()
}
49 changes: 7 additions & 42 deletions application/project/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,11 @@

package project

import (
"errors"
"time"

"github.com/gosimple/slug"

"github.com/isaqueveras/powersso/utils"
)

// CreateProjectReq models the data to create a project
type CreateProjectReq struct {
Name *string `json:"name"`
Description *string `json:"description,omitempty"`
Color *string `json:"hex_color,omitempty"`
Participants []Participant `json:"participants"`
CreatedByID *string `json:"created_by_id,omitempty"`
Slug *string `json:"slug,omitempty"`
}

// Participant models the data the a participant
type Participant struct {
UserID *string `json:"user_id"`
StartDate *time.Time `json:"start_date"`
DepartureDate *time.Time `json:"departure_date,omitempty"`
}

// Validate validation of data for registration
func (cpr *CreateProjectReq) Validate() (err error) {
if cpr.Name == nil || *cpr.Name == "" {
return errors.New("cannot create a project without a name")
}

cpr.Slug = utils.Pointer(slug.Make(*cpr.Name))
if len(cpr.Participants) == 0 {
return errors.New("cannot create a project without participants")
}

if cpr.Color == nil || *cpr.Color == "" {
cpr.Color = utils.Pointer("#949494")
}

return
// NewProject models the data to create a project
type NewProject struct {
Name *string `json:"name" binding:"required,min=3,max=20"`
Description *string `json:"description"`
Slug *string `json:"-"`
Url *string `json:"url" binding:"required,max=200"`
CreatedByID *string `json:"-"`
}
11 changes: 11 additions & 0 deletions delivery/http/permissions/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package permissions

import (
"net/http"

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

func getPermission(ctx *gin.Context) {
ctx.JSON(http.StatusOK, []string{})
}
1 change: 1 addition & 0 deletions delivery/http/permissions/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package permissions
8 changes: 8 additions & 0 deletions delivery/http/permissions/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package permissions

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

// Router is the router for the permission module.
func Router(r *gin.RouterGroup) {
r.GET("", getPermission)
}
17 changes: 7 additions & 10 deletions delivery/http/project/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@ import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/gosimple/slug"
"github.com/isaqueveras/powersso/application/project"
"github.com/isaqueveras/powersso/middleware"
"github.com/isaqueveras/powersso/oops"
"github.com/isaqueveras/powersso/utils"
)

// @Router /v1/project/create [post]
func create(ctx *gin.Context) {
func newProject(ctx *gin.Context) {
var (
input = new(project.CreateProjectReq)
input = new(project.NewProject)
session = middleware.GetSession(ctx)
err error
)

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

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

input.Slug = utils.Pointer(slug.Make(*input.Name))
input.CreatedByID = &session.UserID
if err = project.Create(ctx, input); err != nil {

if err := project.CreateNewProject(ctx, input); err != nil {
oops.Handling(ctx, err)
return
}
Expand Down
7 changes: 3 additions & 4 deletions delivery/http/project/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package project

import (
"github.com/gin-gonic/gin"
"github.com/isaqueveras/powersso/middleware"
)

// RouterAuthorization is the router for the project module.
func RouterAuthorization(r *gin.RouterGroup) {
r.POST("create", middleware.OnlyAdmin(), create)
// Router is the router for the project module.
func Router(r *gin.RouterGroup) {
r.POST("new", newProject)
}
2 changes: 1 addition & 1 deletion domain/project/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package project

// IProject define an interface for data layer access methods
type IProject interface {
Create(*CreateProject) error
CreateNewProject(*CreateProject) error
}
20 changes: 5 additions & 15 deletions domain/project/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@

package project

import "time"

// CreateProject models the data to create a project
type CreateProject struct {
Name *string `json:"name"`
Description *string `json:"description,omitempty"`
Color *string `json:"hex_color,omitempty"`
Participants []Participant `json:"participants"`
CreatedByID *string `json:"created_by_id,omitempty"`
Slug *string `json:"slug,omitempty"`
}

// Participant models the data the a participant
type Participant struct {
UserID *string `json:"user_id"`
StartDate *time.Time `json:"start_date"`
DepartureDate *time.Time `json:"departure_date,omitempty"`
Name *string
Description *string
Slug *string
Url *string
CreatedByID *string
}
37 changes: 11 additions & 26 deletions infrastructure/persistencie/project/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,23 @@
package project

import (
"context"

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

// pg is the implementation of transaction for the session repository
type pg struct {
// database is the implementation of transaction for the session repository
type database struct {
DB *postgres.Transaction
}

// Create contains the flow for create project in database
func (pg *pg) create(input *project.CreateProject) (err error) {
var projectID *string
if err = pg.DB.Builder.
// createNewProject contains the flow for create a project in database
func (db *database) createNewProject(ctx context.Context, in *project.CreateProject) error {
_, err := db.DB.Builder.
Insert("projects").
Columns("created_by", "name", "description", "color", "slug").
Values(input.CreatedByID, input.Name, input.Description, input.Color, input.Slug).
Suffix(`RETURNING "id"`).
Scan(&projectID); err != nil {
return oops.Err(err)
}

for _, value := range input.Participants {
if err = pg.DB.Builder.
Insert("project_participants").
Columns("user_id", "start_date", "departure_date", "project_id").
Values(value.UserID, value.StartDate, value.DepartureDate, projectID).
Suffix(`RETURNING "user_id"`).
Scan(new(string)); err != nil {
return oops.Err(err)
}
}

return
Columns("name", "description", "slug", "created_by", "url").
Values(in.Name, in.Description, in.Slug, in.CreatedByID, in.Url).
ExecContext(ctx)
return err
}
15 changes: 9 additions & 6 deletions infrastructure/persistencie/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@
package project

import (
"context"

"github.com/isaqueveras/powersso/database/postgres"
"github.com/isaqueveras/powersso/domain/project"
)

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

// New creates a new repository
func New(transaction *postgres.Transaction) project.IProject {
return &repository{pg: &pg{DB: transaction}}
func New(ctx context.Context, tx *postgres.Transaction) project.IProject {
return &repository{ctx: ctx, pg: &database{DB: tx}}
}

// Create contains the flow for create project in database
func (r *repository) Create(input *project.CreateProject) error {
return r.pg.create(input)
// CreateNewProject contains the flow for create project in database
func (r *repository) CreateNewProject(input *project.CreateProject) error {
return r.pg.createNewProject(r.ctx, input)
}
17 changes: 8 additions & 9 deletions migrations/20220812140141_create_projects_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
-- 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),
"uri_redirect" VARCHAR(200) NOT NULL,
"color" VARCHAR(7),
slug VARCHAR,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE
"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
);

This file was deleted.

This file was deleted.

4 changes: 1 addition & 3 deletions scripts/create_user_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (

// CreateUserAdmin register the first admin user
func CreateUserAdmin(logg *utils.Logger) {
logg.Info("Initializing script create user admin")
defer logg.Info("Finalizing script create user admin")

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

Expand All @@ -42,6 +39,7 @@ func CreateUserAdmin(logg *utils.Logger) {
log.Fatal(err)
}

logg.Info("User admin created")
if err = tx.Commit(); err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 7 additions & 2 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/permissions"
"github.com/isaqueveras/powersso/delivery/http/project"
"github.com/isaqueveras/powersso/middleware"
)
Expand Down Expand Up @@ -40,7 +41,8 @@ func (s *Server) ServerHTTP() (err error) {
v1 := router.Group("v1")
auth.Router(v1.Group("auth"))
auth.RouterAuthorization(v1.Group("auth", middleware.Auth()))
project.RouterAuthorization(v1.Group("project", middleware.Auth()))
project.Router(v1.Group("project", middleware.Auth()))
permissions.Router(v1.Group("permission", middleware.Auth()))

endless.DefaultReadTimeOut = s.cfg.Server.ReadTimeout * time.Second
endless.DefaultWriteTimeOut = s.cfg.Server.WriteTimeout * time.Second
Expand All @@ -54,7 +56,10 @@ func (s *Server) ServerHTTP() (err error) {
}
})

s.routerDebugPProf(router)
if !s.cfg.Server.IsModeDevelopment() {
s.routerDebugPProf(router)
}

return
}

Expand Down
Loading

0 comments on commit 65e8f5e

Please sign in to comment.