mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
initial commit
This commit is contained in:
@@ -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