-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add case usecase, repository factory, models, DTOs, and API end…
…points
- Loading branch information
1 parent
88d101a
commit 6d376b6
Showing
9 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cases | ||
|
||
import ( | ||
"gh5-backend/internal/factory" | ||
"gh5-backend/internal/model/dto" | ||
res "gh5-backend/pkg/utils/response" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type delivery struct { | ||
Factory factory.Factory | ||
} | ||
|
||
func NewDelivery(f factory.Factory) *delivery { | ||
return &delivery{f} | ||
} | ||
|
||
func (h *delivery) Route(g *echo.Group) { | ||
g.GET("", h.Get) | ||
g.GET("/:id", h.GetByID) | ||
g.POST("/", h.Create) | ||
} | ||
|
||
func (h *delivery) Get(c echo.Context) error { | ||
result, err := h.Factory.Usecase.Case.Find(c.Request().Context()) | ||
if err != nil { | ||
return res.ErrorResponse(err).Send(c) | ||
} | ||
|
||
return res.CustomSuccessBuilder(200, result, "Get all cases success").Send(c) | ||
} | ||
|
||
func (h *delivery) GetByID(c echo.Context) error { | ||
payload := new(dto.ByIDRequest) | ||
if err := c.Bind(payload); err != nil { | ||
return res.ErrorBuilder(&res.ErrorConstant.BadRequest, err).Send(c) | ||
} | ||
|
||
if err := c.Validate(payload); err != nil { | ||
return res.ErrorBuilder(&res.ErrorConstant.Validation, err).Send(c) | ||
} | ||
|
||
result, err := h.Factory.Usecase.Case.FindByID(c.Request().Context(), *payload) | ||
if err != nil { | ||
return res.ErrorResponse(err).Send(c) | ||
} | ||
|
||
return res.CustomSuccessBuilder(200, result, "Get case by id success").Send(c) | ||
} | ||
|
||
func (h *delivery) Create(c echo.Context) error { | ||
payload := new(dto.CreateCaseRequest) | ||
if err := c.Bind(payload); err != nil { | ||
return res.ErrorBuilder(&res.ErrorConstant.BadRequest, err).Send(c) | ||
} | ||
if err := c.Validate(payload); err != nil { | ||
return res.ErrorBuilder(&res.ErrorConstant.Validation, err).Send(c) | ||
} | ||
|
||
result, err := h.Factory.Usecase.Case.Create(c.Request().Context(), *payload) | ||
if err != nil { | ||
return res.ErrorResponse(err).Send(c) | ||
} | ||
|
||
return res.SuccessResponse(result).Send(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dto | ||
|
||
import ( | ||
model "gh5-backend/internal/model/entity" | ||
res "gh5-backend/pkg/utils/response" | ||
) | ||
|
||
// request | ||
type ( | ||
CreateCaseRequest struct { | ||
model.CaseEntity | ||
} | ||
) | ||
|
||
type ( | ||
UpdateCaseRequest struct { | ||
ID string `param:"id" validate:"required"` | ||
CaseNumber string `json:"case_number"` | ||
CaseDescription string `json:"case_description" validate:"required"` | ||
CaseDetail string `json:"case_detail"` | ||
Status string `json:"status" validate:"required"` | ||
IsActive bool `json:"is_active"` | ||
ContributorID string `json:"contributor_id"` | ||
UploaderID string `json:"uploader_id"` | ||
} | ||
) | ||
|
||
// response | ||
type ( | ||
CaseResponse struct { | ||
Data model.CaseModel | ||
} | ||
CaseResponseDoc struct { | ||
Body struct { | ||
Meta res.Meta `json:"meta"` | ||
Data CaseResponse `json:"data"` | ||
} `json:"body"` | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package entity | ||
|
||
import ( | ||
"context" | ||
|
||
abstraction "gh5-backend/internal/model/base" | ||
constant "gh5-backend/pkg/constants" | ||
"gh5-backend/pkg/ctxval" | ||
"gh5-backend/pkg/utils/date" | ||
|
||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type CaseEntity struct { | ||
CaseNumber string `json:"case_number" gorm:"size:50"` | ||
CaseDescription string `json:"case_description" gorm:"not null"` | ||
CaseDetail string `json:"case_detail"` | ||
Status string `json:"status" validate:"required" gorm:"not null"` | ||
IsActive bool `json:"is_active"` | ||
ContributorID *string `json:"contributor_id"` | ||
UploaderID *string `json:"uploader_id"` | ||
|
||
Contributor *UserModel `json:"contributor" gorm:"foreignkey:ContributorID"` | ||
Uploader *UserModel `json:"uploader" gorm:"foreignkey:UploaderID"` | ||
} | ||
|
||
type CaseModel struct { | ||
abstraction.Entity | ||
CaseEntity | ||
Context context.Context `json:"-" gorm:"-"` | ||
} | ||
|
||
func (CaseModel) TableName() string { | ||
return "cases" | ||
} | ||
|
||
func (m *CaseModel) BeforeCreate(tx *gorm.DB) (err error) { | ||
m.ID = uuid.New().String() | ||
m.IsActive = true | ||
m.CreatedAt = *date.DateTodayLocal() | ||
m.CreatedBy = constant.DB_DEFAULT_CREATED_BY | ||
|
||
return | ||
} | ||
|
||
func (m *CaseModel) BeforeUpdate(tx *gorm.DB) (err error) { | ||
m.ModifiedAt = date.DateTodayLocal() | ||
|
||
authCtx := ctxval.GetAuthValue(m.Context) | ||
if authCtx != nil { | ||
m.ModifiedBy = &authCtx.Name | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package repository | ||
|
||
import ( | ||
"gh5-backend/internal/model/entity" | ||
|
||
"github.com/sirupsen/logrus" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type CaseRepository struct { | ||
Repository[entity.CaseModel] | ||
Log *logrus.Logger | ||
} | ||
|
||
func NewCaseRepository(conn *gorm.DB, log *logrus.Logger) *CaseRepository { | ||
model := entity.CaseModel{} | ||
repository := NewRepository(conn, model, model.TableName()) | ||
return &CaseRepository{ | ||
Repository: *repository, | ||
Log: log, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package usecase | ||
|
||
import ( | ||
"context" | ||
"gh5-backend/internal/factory/repository" | ||
"gh5-backend/internal/model/dto" | ||
model "gh5-backend/internal/model/entity" | ||
) | ||
|
||
type CaseUsecase struct { | ||
RepositoryFactory repository.Factory | ||
} | ||
|
||
func NewCaseUsecase(f repository.Factory) *CaseUsecase { | ||
return &CaseUsecase{f} | ||
} | ||
|
||
func (u *CaseUsecase) Find(ctx context.Context) ([]dto.CaseResponse, error) { | ||
var result []dto.CaseResponse | ||
|
||
Cases, err := u.RepositoryFactory.CaseRepository.Find(ctx) | ||
if err != nil { | ||
u.RepositoryFactory.Log.Warnf("Failed find all cases : %+v", err) | ||
return result, err | ||
} | ||
|
||
for _, Case := range Cases { | ||
result = append(result, dto.CaseResponse{ | ||
Data: Case, | ||
}) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (u *CaseUsecase) FindByID(ctx context.Context, payload dto.ByIDRequest) (dto.CaseResponse, error) { | ||
var result dto.CaseResponse | ||
|
||
data, err := u.RepositoryFactory.CaseRepository.FindByID(ctx, payload.ID) | ||
if err != nil { | ||
u.RepositoryFactory.Log.Warnf("Failed find case by id : %+v", err) | ||
return result, err | ||
} | ||
|
||
result = dto.CaseResponse{ | ||
Data: *data, | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (u *CaseUsecase) Create(ctx context.Context, payload dto.CreateCaseRequest) (dto.CaseResponse, error) { | ||
var ( | ||
result dto.CaseResponse | ||
data model.CaseModel | ||
Case = model.CaseModel{ | ||
CaseEntity: model.CaseEntity{ | ||
CaseNumber: payload.CaseNumber, | ||
CaseDetail: payload.CaseDetail, | ||
Status: payload.Status, | ||
UploaderID: payload.UploaderID, | ||
}, | ||
Context: ctx, | ||
} | ||
) | ||
|
||
data, err := u.RepositoryFactory.CaseRepository.Create(ctx, Case) | ||
if err != nil { | ||
u.RepositoryFactory.Log.Warnf("Failed create case : %+v", err) | ||
return result, err | ||
} | ||
|
||
result = dto.CaseResponse{ | ||
Data: data, | ||
} | ||
|
||
return result, nil | ||
} |