Skip to content

Commit

Permalink
refactor: Unify project packages (#130)
Browse files Browse the repository at this point in the history
* refactor(accounts): Use a single file for all domain errors

* refactor(accounts): Use a single file for all requests schemas

* refactor(accounts): Remove http prefix from controllers and routes files

* refactor(courses): Update files names

* chore(labs): Track responses folder

Add an empty .gitkeep file to track the folder

* refactor(lang): Update files names and track requests folder

* refactor(rubrics): Use a single file for all domain errors

* refactor(rubrics): Remove http prefix from controllers and routes files

* refactor(session): Use a single file for all dtos

* refactor(session): Use a single file for all domain errors

* refactor(session): Remove http prefix from controllers and routes files

* refactor(session): Use a single file for all requests and track responses folder

* refactor: Replace snake case imports with camel case

Use golang naming convention
  • Loading branch information
PedroChaparro authored Dec 30, 2023
1 parent 7d538f4 commit 1511156
Show file tree
Hide file tree
Showing 40 changed files with 279 additions and 301 deletions.
12 changes: 6 additions & 6 deletions __tests__/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"testing"

"github.com/UPB-Code-Labs/main-api/src/accounts/infrastructure/requests"
config_infrastructure "github.com/UPB-Code-Labs/main-api/src/config/infrastructure"
shared_infrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
configInfrastructure "github.com/UPB-Code-Labs/main-api/src/config/infrastructure"
sharedInfrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ type GenericTestCase struct {
func TestMain(m *testing.M) {
// Setup database
setupDatabase()
defer shared_infrastructure.ClosePostgresConnection()
defer sharedInfrastructure.ClosePostgresConnection()

// Setup http router
setupRouter()
Expand All @@ -52,12 +52,12 @@ func TestMain(m *testing.M) {
}

func setupDatabase() {
shared_infrastructure.GetPostgresConnection()
config_infrastructure.RunMigrations()
sharedInfrastructure.GetPostgresConnection()
configInfrastructure.RunMigrations()
}

func setupRouter() {
router = config_infrastructure.InstanceHttpServer()
router = configInfrastructure.InstanceHttpServer()
}

func registerBaseAccounts() {
Expand Down
42 changes: 42 additions & 0 deletions src/accounts/domain/errors/accounts_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package errors

import (
"fmt"
"net/http"
)

type EmailAlreadyInUseError struct {
Email string
}

func (err EmailAlreadyInUseError) Error() string {
return fmt.Sprintf("Email %s is already in use", err.Email)
}

func (err EmailAlreadyInUseError) StatusCode() int {
return http.StatusConflict
}

type InstitutionalIdAlreadyInUseError struct {
InstitutionalId string
}

func (err InstitutionalIdAlreadyInUseError) Error() string {
return fmt.Sprintf("Institutional ID %s is already in use", err.InstitutionalId)
}

func (err InstitutionalIdAlreadyInUseError) StatusCode() int {
return http.StatusConflict
}

type UserNotFoundError struct {
Uuuid string
}

func (err UserNotFoundError) Error() string {
return fmt.Sprintf("User with UUID %s not found", err.Uuuid)
}

func (err UserNotFoundError) StatusCode() int {
return http.StatusNotFound
}
18 changes: 0 additions & 18 deletions src/accounts/domain/errors/email_already_in_use.go

This file was deleted.

18 changes: 0 additions & 18 deletions src/accounts/domain/errors/institutional_id_in_use.go

This file was deleted.

18 changes: 0 additions & 18 deletions src/accounts/domain/errors/user_not_found.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package infrastructure
import (
"github.com/UPB-Code-Labs/main-api/src/accounts/application"
"github.com/UPB-Code-Labs/main-api/src/accounts/infrastructure/implementations"
shared_infrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
sharedInfrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
"github.com/gin-gonic/gin"
)

Expand All @@ -22,25 +22,25 @@ func StartAccountsRoutes(g *gin.RouterGroup) {
accountsGroup.POST("/students", controller.HandleRegisterStudent)
accountsGroup.GET(
"/students",
shared_infrastructure.WithAuthenticationMiddleware(),
shared_infrastructure.WithAuthorizationMiddleware([]string{"teacher"}),
sharedInfrastructure.WithAuthenticationMiddleware(),
sharedInfrastructure.WithAuthorizationMiddleware([]string{"teacher"}),
controller.HandleSearchStudents,
)
accountsGroup.POST(
"/admins",
shared_infrastructure.WithAuthenticationMiddleware(),
shared_infrastructure.WithAuthorizationMiddleware([]string{"admin"}),
sharedInfrastructure.WithAuthenticationMiddleware(),
sharedInfrastructure.WithAuthorizationMiddleware([]string{"admin"}),
controller.HandleRegisterAdmin,
)
accountsGroup.GET(
"/admins",
shared_infrastructure.WithAuthenticationMiddleware(),
shared_infrastructure.WithAuthorizationMiddleware([]string{"admin"}),
sharedInfrastructure.WithAuthenticationMiddleware(),
sharedInfrastructure.WithAuthorizationMiddleware([]string{"admin"}),
controller.HandleGetAdmins,
)
accountsGroup.POST("/teachers",
shared_infrastructure.WithAuthenticationMiddleware(),
shared_infrastructure.WithAuthorizationMiddleware([]string{"admin"}),
sharedInfrastructure.WithAuthenticationMiddleware(),
sharedInfrastructure.WithAuthorizationMiddleware([]string{"admin"}),
controller.HandleRegisterTeacher,
)
}
47 changes: 47 additions & 0 deletions src/accounts/infrastructure/requests/accounts_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package requests

import "github.com/UPB-Code-Labs/main-api/src/accounts/domain/dtos"

type RegisterAdminRequest struct {
FullName string `json:"full_name" validate:"required,min=4,max=255"`
Email string `json:"email" validate:"required,email"`
Password string `json:"password" validate:"required,min=8,max=255,secure_password"`
}

func (request *RegisterAdminRequest) ToDTO() *dtos.RegisterUserDTO {
return &dtos.RegisterUserDTO{
FullName: request.FullName,
Email: request.Email,
Password: request.Password,
}
}

type RegisterTeacherRequest struct {
FullName string `json:"full_name" validate:"required,min=4,max=255"`
Email string `json:"email" validate:"required,email,institutional_email"`
Password string `json:"password" validate:"required,min=8,max=255,secure_password"`
}

func (request *RegisterTeacherRequest) ToDTO() *dtos.RegisterUserDTO {
return &dtos.RegisterUserDTO{
FullName: request.FullName,
Email: request.Email,
Password: request.Password,
}
}

type RegisterUserRequest struct {
FullName string `json:"full_name" validate:"required,min=4,max=255"`
Email string `json:"email" validate:"required,email,institutional_email"`
InstitutionalId string `json:"institutional_id" validate:"required,numeric,min=6,max=9"`
Password string `json:"password" validate:"required,min=8,max=255,secure_password"`
}

func (request *RegisterUserRequest) ToDTO() *dtos.RegisterUserDTO {
return &dtos.RegisterUserDTO{
FullName: request.FullName,
Email: request.Email,
InstitutionalId: request.InstitutionalId,
Password: request.Password,
}
}
17 changes: 0 additions & 17 deletions src/accounts/infrastructure/requests/register_admin_request.go

This file was deleted.

17 changes: 0 additions & 17 deletions src/accounts/infrastructure/requests/register_teacher_request.go

This file was deleted.

19 changes: 0 additions & 19 deletions src/accounts/infrastructure/requests/register_user_request.go

This file was deleted.

36 changes: 18 additions & 18 deletions src/config/infrastructure/http_server.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package infrastructure

import (
accounts_http "github.com/UPB-Code-Labs/main-api/src/accounts/infrastructure/http"
blocks_http "github.com/UPB-Code-Labs/main-api/src/blocks/infrastructure/http"
courses_http "github.com/UPB-Code-Labs/main-api/src/courses/infrastructure/http"
laboratories_http "github.com/UPB-Code-Labs/main-api/src/laboratories/infrastructure/http"
languages_http "github.com/UPB-Code-Labs/main-api/src/languages/infrastructure/http"
rubrics_http "github.com/UPB-Code-Labs/main-api/src/rubrics/infrastructure/http"
session_http "github.com/UPB-Code-Labs/main-api/src/session/infrastructure/http"
shared_infra "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
accountsHttp "github.com/UPB-Code-Labs/main-api/src/accounts/infrastructure/http"
blocksHttp "github.com/UPB-Code-Labs/main-api/src/blocks/infrastructure/http"
coursesHttp "github.com/UPB-Code-Labs/main-api/src/courses/infrastructure/http"
laboratoriesHttp "github.com/UPB-Code-Labs/main-api/src/laboratories/infrastructure/http"
languagesHttp "github.com/UPB-Code-Labs/main-api/src/languages/infrastructure/http"
rubricsHttp "github.com/UPB-Code-Labs/main-api/src/rubrics/infrastructure/http"
sessionHttp "github.com/UPB-Code-Labs/main-api/src/session/infrastructure/http"
sharedInfrastructure "github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

var routesGroups = []func(*gin.RouterGroup){
accounts_http.StartAccountsRoutes,
blocks_http.StartBlocksRoutes,
session_http.StartSessionRoutes,
courses_http.StartCoursesRoutes,
rubrics_http.StartRubricsRoutes,
laboratories_http.StartLaboratoriesRoutes,
languages_http.StartLanguagesRoutes,
accountsHttp.StartAccountsRoutes,
blocksHttp.StartBlocksRoutes,
sessionHttp.StartSessionRoutes,
coursesHttp.StartCoursesRoutes,
rubricsHttp.StartRubricsRoutes,
laboratoriesHttp.StartLaboratoriesRoutes,
languagesHttp.StartLanguagesRoutes,
}

func InstanceHttpServer() (r *gin.Engine) {
engine := gin.Default()
engine.Use(shared_infra.ErrorHandlerMiddleware())
engine.Use(sharedInfrastructure.ErrorHandlerMiddleware())

isInProductionEnvironment := shared_infra.GetEnvironment().Environment == "production"
isInProductionEnvironment := sharedInfrastructure.GetEnvironment().Environment == "production"
if isInProductionEnvironment {
gin.SetMode(gin.ReleaseMode)
}

// Configure CORS rules
corsConfig := cors.DefaultConfig()
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
corsConfig.AllowOrigins = []string{shared_infra.GetEnvironment().WebClientUrl}
corsConfig.AllowOrigins = []string{sharedInfrastructure.GetEnvironment().WebClientUrl}
corsConfig.AllowCredentials = true
engine.Use(cors.New(corsConfig))

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/UPB-Code-Labs/main-api/src/courses/domain/dtos"
"github.com/UPB-Code-Labs/main-api/src/courses/domain/entities"
courses_errors "github.com/UPB-Code-Labs/main-api/src/courses/domain/errors"
coursesErrors "github.com/UPB-Code-Labs/main-api/src/courses/domain/errors"
"github.com/UPB-Code-Labs/main-api/src/shared/infrastructure"
)

Expand Down Expand Up @@ -207,7 +207,7 @@ func (repository *CoursesPostgresRepository) GetCourseByUUID(uuid string) (*enti
if err != nil {
// Throw a domain error if the course was not found
if err == sql.ErrNoRows {
return nil, courses_errors.NoCourseWithUUIDFound{
return nil, coursesErrors.NoCourseWithUUIDFound{
UUID: uuid,
}
}
Expand Down
Loading

0 comments on commit 1511156

Please sign in to comment.