mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
initial commit
This commit is contained in:
+263
@@ -0,0 +1,263 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
FeatName string // input full feature (ex: "master/area")
|
||||
Parts []string // split parts ["master","area"]
|
||||
Entity string // last ("area")
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("usage: make gen feat=<feature> (ex: customer | master/area)")
|
||||
}
|
||||
|
||||
feat := os.Args[1]
|
||||
parts := strings.Split(feat, "/")
|
||||
entity := parts[len(parts)-1]
|
||||
|
||||
d := Data{
|
||||
FeatName: feat,
|
||||
Parts: parts,
|
||||
Entity: entity,
|
||||
}
|
||||
|
||||
// daftar template yang mau diproses
|
||||
files := []struct {
|
||||
TplPath string
|
||||
OutDir string
|
||||
OutSuffix string
|
||||
TplName string
|
||||
}{
|
||||
{
|
||||
TplPath: "tools/templates/model.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "models"),
|
||||
OutSuffix: ".model.go",
|
||||
TplName: "model",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/validation.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "validations"),
|
||||
OutSuffix: ".validation.go",
|
||||
TplName: "validation",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/service.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "services"),
|
||||
OutSuffix: ".service.go",
|
||||
TplName: "service",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/controller.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "controllers"),
|
||||
OutSuffix: ".controller.go",
|
||||
TplName: "controller",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/repository.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "repositories"),
|
||||
OutSuffix: ".repository.go",
|
||||
TplName: "repository",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/dto.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s", "dto"),
|
||||
OutSuffix: ".dto.go",
|
||||
TplName: "dto",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/route.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s"),
|
||||
OutSuffix: "",
|
||||
TplName: "route",
|
||||
},
|
||||
{
|
||||
TplPath: "tools/templates/module.tmpl",
|
||||
OutDir: filepath.Join("internal", "modules", toKebabPath(d.Parts[:len(d.Parts)-1]), toKebab(d.Entity)+"s"),
|
||||
OutSuffix: "",
|
||||
TplName: "module",
|
||||
},
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
// pastikan template ketemu
|
||||
if _, err := os.Stat(file.TplPath); err != nil {
|
||||
log.Fatalf("template not found at %s: %v", file.TplPath, err)
|
||||
}
|
||||
|
||||
// parse template
|
||||
tpl := template.Must(
|
||||
template.New(file.TplName).
|
||||
Funcs(template.FuncMap{
|
||||
"Pascal": toPascalCase,
|
||||
"Camel": toCamelCase,
|
||||
"Plural": toPlural,
|
||||
"Kebab": toKebab,
|
||||
}).
|
||||
ParseFiles(file.TplPath),
|
||||
)
|
||||
|
||||
// pastikan folder ada
|
||||
if err := os.MkdirAll(file.OutDir, 0o755); err != nil {
|
||||
log.Fatalf("make dir: %v", err)
|
||||
}
|
||||
|
||||
// nama file output
|
||||
var outFile string
|
||||
switch file.TplName {
|
||||
case "route":
|
||||
outFile = filepath.Join(file.OutDir, "route.go")
|
||||
case "module":
|
||||
outFile = filepath.Join(file.OutDir, "module.go")
|
||||
default:
|
||||
outFile = filepath.Join(file.OutDir, strings.ToLower(d.Entity)+file.OutSuffix)
|
||||
}
|
||||
|
||||
// hindari overwrite
|
||||
if _, err := os.Stat(outFile); err == nil {
|
||||
log.Fatalf("file already exists: %s", outFile)
|
||||
}
|
||||
|
||||
f, err := os.Create(outFile)
|
||||
if err != nil {
|
||||
log.Fatalf("create file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if err := tpl.ExecuteTemplate(f, file.TplName, d); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Generated:", outFile)
|
||||
}
|
||||
|
||||
updateMainRoute(d)
|
||||
}
|
||||
|
||||
func updateMainRoute(d Data) {
|
||||
routeFile := "internal/route/route.go"
|
||||
content, err := os.ReadFile(routeFile)
|
||||
if err != nil {
|
||||
log.Printf("skip update route.go: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// entity & path
|
||||
modPath := filepath.Join(append(toCamelParts(d.Parts[:len(d.Parts)-1]), toCamelCase(d.Entity)+"s")...)
|
||||
modName := toCamelCase(d.Entity) + "s"
|
||||
pkgName := toPascalCase(d.Entity) + "Module"
|
||||
|
||||
// Inject import
|
||||
importLine := fmt.Sprintf("\t%[1]s \"%s/internal/modules/%s\"", modName, "github.com/hafizhproject45/Golang-Boilerplate.git", modPath)
|
||||
if !strings.Contains(string(content), importLine) {
|
||||
content = []byte(strings.Replace(string(content),
|
||||
"// MODULE IMPORTS",
|
||||
importLine+"\n\t// MODULE IMPORTS",
|
||||
1))
|
||||
}
|
||||
|
||||
// Inject registry
|
||||
registryLine := fmt.Sprintf("\t\t%[1]s.%[2]s{},", modName, pkgName)
|
||||
if !strings.Contains(string(content), registryLine) {
|
||||
content = []byte(strings.Replace(string(content),
|
||||
"// MODULE REGISTRY",
|
||||
registryLine+"\n\t\t// MODULE REGISTRY",
|
||||
1))
|
||||
}
|
||||
|
||||
if err := os.WriteFile(routeFile, content, 0644); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Updated:", routeFile)
|
||||
}
|
||||
|
||||
func toPascalCase(s string) string {
|
||||
sep := func(r rune) bool { return r == '_' || r == '-' || r == ' ' || r == '/' }
|
||||
parts := strings.FieldsFunc(s, sep)
|
||||
for i, p := range parts {
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
parts[i] = strings.ToUpper(p[:1]) + strings.ToLower(p[1:])
|
||||
}
|
||||
return strings.Join(parts, "")
|
||||
}
|
||||
|
||||
func toCamelCase(s string) string {
|
||||
p := toPascalCase(s)
|
||||
if p == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.ToLower(p[:1]) + p[1:]
|
||||
}
|
||||
|
||||
// simple pluralizer (cukup untuk kasus umum: tambah 's')
|
||||
func toPlural(s string) string {
|
||||
s = strings.ToLower(s)
|
||||
if strings.HasSuffix(s, "y") && len(s) > 1 {
|
||||
prev := s[len(s)-2]
|
||||
if !(prev == 'a' || prev == 'i' || prev == 'u' || prev == 'e' || prev == 'o') {
|
||||
return s[:len(s)-1] + "ies"
|
||||
}
|
||||
}
|
||||
return s + "s"
|
||||
}
|
||||
|
||||
// kebab-case (untuk folder)
|
||||
func toKebab(s string) string {
|
||||
s = strings.ReplaceAll(s, "_", "-")
|
||||
var b strings.Builder
|
||||
for i, r := range s {
|
||||
if r >= 'A' && r <= 'Z' {
|
||||
if i > 0 {
|
||||
b.WriteByte('-')
|
||||
}
|
||||
b.WriteRune(r + 32)
|
||||
} else {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
out := b.String()
|
||||
out = strings.ReplaceAll(out, "--", "-")
|
||||
return strings.Trim(out, "-")
|
||||
}
|
||||
|
||||
// join multiple parts jadi kebab path
|
||||
func toKebabPath(parts []string) string {
|
||||
return filepath.Join(toKebabParts(parts)...)
|
||||
}
|
||||
|
||||
func toKebabParts(parts []string) []string {
|
||||
var out []string
|
||||
for _, p := range parts {
|
||||
out = append(out, toKebab(p))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// join multiple parts jadi camelCase path
|
||||
// func toCamelPath(parts []string) string {
|
||||
// return filepath.Join(toCamelParts(parts)...)
|
||||
// }
|
||||
|
||||
func toCamelParts(parts []string) []string {
|
||||
var out []string
|
||||
for i, p := range parts {
|
||||
if i == 0 {
|
||||
// part pertama lower-case semua
|
||||
out = append(out, toCamelCase(p))
|
||||
} else {
|
||||
// part berikutnya PascalCase biar tetap nyambung camel
|
||||
out = append(out, toPascalCase(p))
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
{{define "controller"}}package controller
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/dto"
|
||||
service "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/services"
|
||||
validation "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/validations"
|
||||
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/response"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}}Controller struct {
|
||||
{{Pascal .Entity}}Service service.{{Pascal .Entity}}Service
|
||||
}
|
||||
|
||||
func New{{Pascal .Entity}}Controller({{Camel .Entity}}Service service.{{Pascal .Entity}}Service) *{{Pascal .Entity}}Controller {
|
||||
return &{{Pascal .Entity}}Controller{
|
||||
{{Pascal .Entity}}Service: {{Camel .Entity}}Service,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *{{Pascal .Entity}}Controller) GetAll(c *fiber.Ctx) error {
|
||||
query := &validation.Query{
|
||||
Page: c.QueryInt("page", 1),
|
||||
Limit: c.QueryInt("limit", 10),
|
||||
Search: c.Query("search", ""),
|
||||
}
|
||||
|
||||
result, totalResults, err := u.{{Pascal .Entity}}Service.GetAll(c, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.SuccessWithPaginate[dto.{{Pascal .Entity}}ListDTO]{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get all {{Camel .Entity}}s successfully",
|
||||
Meta: response.Meta{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
TotalPages: int64(math.Ceil(float64(totalResults) / float64(query.Limit))),
|
||||
TotalResults: totalResults,
|
||||
},
|
||||
Data: dto.To{{Pascal .Entity}}ListDTOs(result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *{{Pascal .Entity}}Controller) GetOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
result, err := u.{{Pascal .Entity}}Service.GetOne(c, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Get {{Camel .Entity}} successfully",
|
||||
Data: dto.To{{Pascal .Entity}}ListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *{{Pascal .Entity}}Controller) CreateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Create)
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.{{Pascal .Entity}}Service.CreateOne(c, req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusCreated).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusCreated,
|
||||
Status: "success",
|
||||
Message: "Create {{Camel .Entity}} successfully",
|
||||
Data: dto.To{{Pascal .Entity}}ListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *{{Pascal .Entity}}Controller) UpdateOne(c *fiber.Ctx) error {
|
||||
req := new(validation.Update)
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := c.BodyParser(req); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid request body")
|
||||
}
|
||||
|
||||
result, err := u.{{Pascal .Entity}}Service.UpdateOne(c, req, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Success{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Update {{Camel .Entity}} successfully",
|
||||
Data: dto.To{{Pascal .Entity}}ListDTO(*result),
|
||||
})
|
||||
}
|
||||
|
||||
func (u *{{Pascal .Entity}}Controller) DeleteOne(c *fiber.Ctx) error {
|
||||
param := c.Params("id")
|
||||
|
||||
id, err := strconv.Atoi(param)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "Invalid Id")
|
||||
}
|
||||
|
||||
if err := u.{{Pascal .Entity}}Service.DeleteOne(c, uint(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).
|
||||
JSON(response.Common{
|
||||
Code: fiber.StatusOK,
|
||||
Status: "success",
|
||||
Message: "Delete {{Camel .Entity}} successfully",
|
||||
})
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,38 @@
|
||||
{{define "dto"}}package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
model "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
)
|
||||
|
||||
// === DTO Structs ===
|
||||
|
||||
type {{Pascal .Entity}}ListDTO struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type {{Pascal .Entity}}DetailDTO struct {
|
||||
{{Pascal .Entity}}ListDTO
|
||||
}
|
||||
|
||||
// === Mapper Functions ===
|
||||
|
||||
func To{{Pascal .Entity}}ListDTO(m model.{{Pascal .Entity}}) {{Pascal .Entity}}ListDTO {
|
||||
return {{Pascal .Entity}}ListDTO{
|
||||
Id: m.Id,
|
||||
Name: m.Name,
|
||||
}
|
||||
}
|
||||
|
||||
func To{{Pascal .Entity}}ListDTOs(m []model.{{Pascal .Entity}}) []{{Pascal .Entity}}ListDTO {
|
||||
result := make([]{{Pascal .Entity}}ListDTO, len(m))
|
||||
for i, r := range m {
|
||||
result[i] = To{{Pascal .Entity}}ListDTO(r)
|
||||
}
|
||||
return result
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,13 @@
|
||||
{{define "model"}}package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}} struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,27 @@
|
||||
{{define "module"}}package {{Kebab .Entity}}s
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
|
||||
r{{Pascal .Entity}} "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/repositories"
|
||||
s{{Pascal .Entity}} "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/services"
|
||||
|
||||
rUser "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/users/repositories"
|
||||
sUser "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/users/services"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}}Module struct{}
|
||||
|
||||
func ({{Pascal .Entity}}Module) RegisterRoutes(router fiber.Router, db *gorm.DB, validate *validator.Validate) {
|
||||
{{Camel .Entity}}Repo := r{{Pascal .Entity}}.New{{Pascal .Entity}}Repository(db)
|
||||
userRepo := rUser.NewUserRepository(db)
|
||||
|
||||
{{Camel .Entity}}Service := s{{Pascal .Entity}}.New{{Pascal .Entity}}Service({{Camel .Entity}}Repo, validate)
|
||||
userService := sUser.NewUserService(userRepo, validate)
|
||||
|
||||
{{Pascal .Entity}}Routes(router, userService, {{Camel .Entity}}Service)
|
||||
}
|
||||
|
||||
{{end}}
|
||||
@@ -0,0 +1,22 @@
|
||||
{{define "repository"}}package repository
|
||||
|
||||
import (
|
||||
model "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/repository"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type {{Pascal .Entity}}Repository interface {
|
||||
repository.BaseRepository[model.{{Pascal .Entity}}]
|
||||
}
|
||||
|
||||
type {{Pascal .Entity}}RepositoryImpl struct {
|
||||
*repository.BaseRepositoryImpl[model.{{Pascal .Entity}}]
|
||||
}
|
||||
|
||||
func New{{Pascal .Entity}}Repository(db *gorm.DB) {{Pascal .Entity}}Repository {
|
||||
return &{{Pascal .Entity}}RepositoryImpl{
|
||||
BaseRepositoryImpl: repository.NewBaseRepository[model.{{Pascal .Entity}}](db),
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,23 @@
|
||||
{{define "route"}}package {{Kebab .Entity}}s
|
||||
|
||||
import (
|
||||
m "github.com/hafizhproject45/Golang-Boilerplate.git/internal/middleware"
|
||||
controller "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/controllers"
|
||||
{{Camel .Entity}} "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/services"
|
||||
user "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/users/services"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func {{Pascal .Entity}}Routes(v1 fiber.Router, u user.UserService, s {{Camel .Entity}}.{{Pascal .Entity}}Service) {
|
||||
ctrl := controller.New{{Pascal .Entity}}Controller(s)
|
||||
|
||||
route := v1.Group("/{{Kebab .Entity}}s")
|
||||
|
||||
route.Get("/", m.Auth(u), ctrl.GetAll)
|
||||
route.Post("/", m.Auth(u), ctrl.CreateOne)
|
||||
route.Get("/:id", m.Auth(u), ctrl.GetOne)
|
||||
route.Patch("/:id", m.Auth(u), ctrl.UpdateOne)
|
||||
route.Delete("/:id", m.Auth(u), ctrl.DeleteOne)
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,120 @@
|
||||
{{define "service"}}package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
model "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/models"
|
||||
repository "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/repositories"
|
||||
validation "github.com/hafizhproject45/Golang-Boilerplate.git/internal/modules/{{Kebab .FeatName}}s/validations"
|
||||
"github.com/hafizhproject45/Golang-Boilerplate.git/internal/utils"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
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)
|
||||
DeleteOne(ctx *fiber.Ctx, id uint) error
|
||||
}
|
||||
|
||||
type {{Camel .Entity}}Service struct {
|
||||
Log *logrus.Logger
|
||||
Validate *validator.Validate
|
||||
Repository repository.{{Pascal .Entity}}Repository
|
||||
}
|
||||
|
||||
func New{{Pascal .Entity}}Service(repo repository.{{Pascal .Entity}}Repository, validate *validator.Validate) {{Pascal .Entity}}Service {
|
||||
return &{{Camel .Entity}}Service{
|
||||
Log: utils.Log,
|
||||
Validate: validate,
|
||||
Repository: repo,
|
||||
}
|
||||
}
|
||||
func (s {{Camel .Entity}}Service) GetAll(c *fiber.Ctx, params *validation.Query) ([]model.{{Pascal .Entity}}, int64, error) {
|
||||
if err := s.Validate.Struct(params); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
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 {
|
||||
if params.Search != "" {
|
||||
return db.Where("name LIKE ?", "%"+params.Search+"%")
|
||||
}
|
||||
return db.Order("created_at DESC").Order("updated_at DESC")
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed to get {{Camel .Entity}}s: %+v", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
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)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fiber.NewError(fiber.StatusNotFound, "{{Pascal .Entity}} not found")
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Errorf("Failed get {{Camel .Entity}} by id: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
return {{Camel .Entity}}, nil
|
||||
}
|
||||
|
||||
func (s *{{Camel .Entity}}Service) CreateOne(c *fiber.Ctx, req *validation.Create) (*model.{{Pascal .Entity}}, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createBody := &model.{{Pascal .Entity}}{
|
||||
Name: req.Name,
|
||||
}
|
||||
|
||||
if err := s.Repository.CreateOne(c.Context(), createBody, nil); err != nil {
|
||||
s.Log.Errorf("Failed to create {{Camel .Entity}}: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return createBody, nil
|
||||
}
|
||||
|
||||
func (s {{Camel .Entity}}Service) UpdateOne(c *fiber.Ctx, req *validation.Update, id uint) (*model.{{Pascal .Entity}}, error) {
|
||||
if err := s.Validate.Struct(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updateBody := make(map[string]any)
|
||||
|
||||
if req.Name != nil {
|
||||
updateBody["name"] = *req.Name
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
s.Log.Errorf("Failed to update {{Camel .Entity}}: %+v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.GetOne(c, id)
|
||||
}
|
||||
|
||||
func (s {{Camel .Entity}}Service) DeleteOne(c *fiber.Ctx, id uint) error {
|
||||
if err := s.Repository.DeleteOne(c.Context(), id); err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fiber.NewError(fiber.StatusNotFound, "{{Pascal .Entity}} not found")
|
||||
}
|
||||
s.Log.Errorf("Failed to delete {{Camel .Entity}}: %+v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
{{end}}
|
||||
@@ -0,0 +1,16 @@
|
||||
{{define "validation"}}package validation
|
||||
|
||||
type Create struct {
|
||||
Name string `json:"name" validate:"required_strict,min=3"`
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
Name *string `json:"name,omitempty" validate:"omitempty,max=50"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
Page int `query:"page" validate:"omitempty,number,min=1"`
|
||||
Limit int `query:"limit" validate:"omitempty,number,min=1,max=100"`
|
||||
Search string `query:"search" validate:"omitempty,max=50"`
|
||||
}
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user