mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Feat(BE-36,37,38,39): master area, customer, kandang, location, warehouse
This commit is contained in:
+20
-11
@@ -3,7 +3,8 @@
|
||||
import (
|
||||
"time"
|
||||
|
||||
model "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
userDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/users/dto"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
@@ -15,6 +16,7 @@ type {{Pascal .Entity}}BaseDTO struct {
|
||||
|
||||
type {{Pascal .Entity}}ListDTO struct {
|
||||
{{Pascal .Entity}}BaseDTO
|
||||
CreatedUser *userDTO.UserBaseDTO `json:"created_user"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
@@ -25,24 +27,31 @@ type {{Pascal .Entity}}DetailDTO struct {
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func To{{Pascal .Entity}}BaseDTO(m model.{{Pascal .Entity}}) {{Pascal .Entity}}BaseDTO {
|
||||
func To{{Pascal .Entity}}BaseDTO(e entity.{{Pascal .Entity}}) {{Pascal .Entity}}BaseDTO {
|
||||
return {{Pascal .Entity}}BaseDTO{
|
||||
Id: m.Id,
|
||||
Name: m.Name,
|
||||
Id: e.Id,
|
||||
Name: e.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func To{{Pascal .Entity}}ListDTO(m model.{{Pascal .Entity}}) {{Pascal .Entity}}ListDTO {
|
||||
func To{{Pascal .Entity}}ListDTO(e entity.{{Pascal .Entity}}) {{Pascal .Entity}}ListDTO {
|
||||
var createdUser *userDTO.UserBaseDTO
|
||||
if e.CreatedUser.Id != 0 {
|
||||
mapped := userDTO.ToUserBaseDTO(e.CreatedUser)
|
||||
createdUser = &mapped
|
||||
}
|
||||
|
||||
return {{Pascal .Entity}}ListDTO{
|
||||
{{Pascal .Entity}}BaseDTO: To{{Pascal .Entity}}BaseDTO(m),
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
{{Pascal .Entity}}BaseDTO: To{{Pascal .Entity}}BaseDTO(e),
|
||||
CreatedAt: e.CreatedAt,
|
||||
UpdatedAt: e.UpdatedAt,
|
||||
CreatedUser: createdUser,
|
||||
}
|
||||
}
|
||||
|
||||
func To{{Pascal .Entity}}ListDTOs(m []model.{{Pascal .Entity}}) []{{Pascal .Entity}}ListDTO {
|
||||
result := make([]{{Pascal .Entity}}ListDTO, len(m))
|
||||
for i, r := range m {
|
||||
func To{{Pascal .Entity}}ListDTOs(e []entity.{{Pascal .Entity}}) []{{Pascal .Entity}}ListDTO {
|
||||
result := make([]{{Pascal .Entity}}ListDTO, len(e))
|
||||
for i, r := range e {
|
||||
result[i] = To{{Pascal .Entity}}ListDTO(r)
|
||||
}
|
||||
return result
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
{{define "entity"}}package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}} struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedBy uint `gorm:"not null"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
CreatedUser User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
{{end}}
|
||||
@@ -1,17 +0,0 @@
|
||||
{{define "model"}}package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}} struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedBy int64 `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
|
||||
CreatedUser model.User `gorm:"foreignKey:CreatedBy;references:Id"`
|
||||
}
|
||||
{{end}}
|
||||
@@ -1,22 +1,22 @@
|
||||
{{define "repository"}}package repository
|
||||
|
||||
import (
|
||||
model "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/repository"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}}Repository interface {
|
||||
repository.BaseRepository[model.{{Pascal .Entity}}]
|
||||
repository.BaseRepository[entity.{{Pascal .Entity}}]
|
||||
}
|
||||
|
||||
type {{Pascal .Entity}}RepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[model.{{Pascal .Entity}}]
|
||||
*repository.BaseRepositoryImpl[entity.{{Pascal .Entity}}]
|
||||
}
|
||||
|
||||
func New{{Pascal .Entity}}Repository(db *gorm.DB) {{Pascal .Entity}}Repository {
|
||||
return &{{Pascal .Entity}}RepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[model.{{Pascal .Entity}}](db),
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[entity.{{Pascal .Entity}}](db),
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import (
|
||||
"errors"
|
||||
|
||||
model "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
repository "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/repositories"
|
||||
validation "gitlab.com/mbugroup/lti-api.git/internal/modules/{{Kebab .FeatName}}s/validations"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
@@ -15,10 +15,10 @@ import (
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}}Service interface {
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]model.{{Pascal .Entity}}, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*model.{{Pascal .Entity}}, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*model.{{Pascal .Entity}}, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*model.{{Pascal .Entity}}, error)
|
||||
GetAll(ctx *fiber.Ctx, params *validation.Query) ([]entity.{{Pascal .Entity}}, int64, error)
|
||||
GetOne(ctx *fiber.Ctx, id uint) (*entity.{{Pascal .Entity}}, error)
|
||||
CreateOne(ctx *fiber.Ctx, req *validation.Create) (*entity.{{Pascal .Entity}}, error)
|
||||
UpdateOne(ctx *fiber.Ctx, req *validation.Update, id uint) (*entity.{{Pascal .Entity}}, error)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
@@ -35,7 +35,12 @@ func New{{Pascal .Entity}}Service(repo repository.{{Pascal .Entity}}Repository,
|
||||
Repository: repo,
|
||||
}
|
||||
}
|
||||
func (s {{Camel .Entity}}Service) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.{{Pascal .Entity}}, int64, error) {
|
||||
|
||||
func (s {{Camel .Entity}}Service) withRelations(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("CreatedUser")
|
||||
}
|
||||
|
||||
func (s {{Camel .Entity}}Service) GetAll(c *fiber.Ctx, params *validation.Query) ([]entity.{{Pascal .Entity}}, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -43,6 +48,7 @@ func (s {{Camel .Entity}}Service) GetAll(c *fiber.Ctx, params *validation.Query)
|
||||
offset := (params.Page - 1) * params.Limit
|
||||
|
||||
{{Camel .Entity}}s, total, err := s.Repository.GetAll(c.Context(), offset, params.Limit, func(db *gorm.DB) *gorm.DB {
|
||||
db = s.withRelations(db)
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
@@ -56,8 +62,8 @@ func (s {{Camel .Entity}}Service) GetAll(c *fiber.Ctx, params *validation.Query)
|
||||
return {{Camel .Entity}}s, total, nil
|
||||
}
|
||||
|
||||
func (s {{Camel .Entity}}Service) GetOne(c *fiber.Ctx, id uint) (*model.{{Pascal .Entity}}, error) {
|
||||
{{Camel .Entity}}, err := s.Repository.GetByID(c.Context(), id, nil)
|
||||
func (s {{Camel .Entity}}Service) GetOne(c *fiber.Ctx, id uint) (*entity.{{Pascal .Entity}}, error) {
|
||||
{{Camel .Entity}}, err := s.Repository.GetByID(c.Context(), id, s.withRelations)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "{{Pascal .Entity}} not found")
|
||||
}
|
||||
@@ -68,12 +74,12 @@ func (s {{Camel .Entity}}Service) GetOne(c *fiber.Ctx, id uint) (*model.{{Pascal
|
||||
return {{Camel .Entity}}, nil
|
||||
}
|
||||
|
||||
func (s *{{Camel .Entity}}Service) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.{{Pascal .Entity}}, error) {
|
||||
func (s *{{Camel .Entity}}Service) CreateOne(c *fiber.Ctx, req *validation.Create) (*entity.{{Pascal .Entity}}, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createBody := &model.{{Pascal .Entity}}{
|
||||
createBody := &entity.{{Pascal .Entity}}{
|
||||
Name: req.Name,
|
||||
}
|
||||
|
||||
@@ -85,7 +91,7 @@ func (s *{{Camel .Entity}}Service) CreateOne(c *fiber.Ctx, req *validation.Creat
|
||||
return createBody, nil
|
||||
}
|
||||
|
||||
func (s {{Camel .Entity}}Service) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*model.{{Pascal .Entity}}, error) {
|
||||
func (s {{Camel .Entity}}Service) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*entity.{{Pascal .Entity}}, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -96,6 +102,10 @@ func (s {{Camel .Entity}}Service) UpdateOne(c *fiber.Ctx, req *validation.Update
|
||||
updateBody["name"] = *req.Name
|
||||
}
|
||||
|
||||
if len(updateBody) == 0 {
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
if err := s.Repository.PatchOne(c.Context(), id, updateBody, nil); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "{{Pascal .Entity}} not found")
|
||||
|
||||
Reference in New Issue
Block a user