145 lines
4.1 KiB
Plaintext
145 lines
4.1 KiB
Plaintext
|
package handler_{name}s
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"online-order/domain"
|
||
|
"online-order/entity"
|
||
|
"online-order/utils"
|
||
|
)
|
||
|
|
||
|
type controller struct {
|
||
|
service domain.{Name}Service
|
||
|
}
|
||
|
|
||
|
func New{Name}Controller(svc domain.{Name}Service) *controller {
|
||
|
return &controller{
|
||
|
service: svc,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// List{Name}s godoc
|
||
|
// @Summary List {Name}s
|
||
|
// @Description get {Name}s
|
||
|
// @Tags {Name}s
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param q query string false "name search by q" Format(email)
|
||
|
// @Success 200 {array} entity.{Name}Display
|
||
|
// @Failure 400 {object} httputil.HTTPError
|
||
|
// @Failure 500 {object} httputil.HTTPError
|
||
|
// @Router /{name}s [get]
|
||
|
func (c *controller) list{Name}(ctx *gin.Context) {
|
||
|
{name}s, err := c.service.List()
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusBadRequest, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseJSON(ctx, http.StatusOK, "successful", {name}s)
|
||
|
}
|
||
|
|
||
|
// Show{Name} godoc
|
||
|
// @Summary Show an {name}
|
||
|
// @Description get string by ID
|
||
|
// @Tags {Name}s
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param id path int true "{name} ID"
|
||
|
// @Success 200 {object} entity.{Name}Display
|
||
|
// @Failure 404 {object} httputil.HTTPError
|
||
|
// @Failure 500 {object} httputil.HTTPError
|
||
|
// @Router /{name}s/{id} [get]
|
||
|
func (c *controller) get{Name}(ctx *gin.Context) {
|
||
|
id := ctx.Param("id")
|
||
|
id_convert, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
{name}, err := c.service.GetByID(id_convert)
|
||
|
if err != nil || {name} == nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseJSON(ctx, http.StatusOK, "successful", {name})
|
||
|
}
|
||
|
|
||
|
// Update{Name} godoc
|
||
|
// @Summary Update an {name}
|
||
|
// @Description update {Name} by ID
|
||
|
// @Tags {Name}s
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param id path int true "{name} ID"
|
||
|
// @Success 200 string message
|
||
|
// @Failure 400 {object} httputil.HTTPError
|
||
|
// @Failure 404 {object} httputil.HTTPError
|
||
|
// @Failure 500 {object} httputil.HTTPError
|
||
|
// @Router /{name}s/{id} [put]
|
||
|
func (c *controller) update(ctx *gin.Context) {
|
||
|
var data *entity.{Name}CreateUpdate
|
||
|
var id = ctx.Param("id")
|
||
|
|
||
|
id_convert, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := ctx.ShouldBindJSON(&data); err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusBadRequest, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
{name}, err := c.service.GetByID(id_convert)
|
||
|
if err != nil || {name} == nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, entity.ErrNotFound.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
data = entity.ValidateUpdate(data, {name})
|
||
|
|
||
|
err = c.service.Update(data)
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusBadRequest, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
response := entity.{Name}DisplayFormatter(data)
|
||
|
utils.ResponseJSON(ctx, http.StatusAccepted, "successful", response)
|
||
|
}
|
||
|
|
||
|
// Delete{Name} godoc
|
||
|
// @Summary Delete an {name}
|
||
|
// @Description Delete {Name} by ID
|
||
|
// @Tags {Name}s
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param id path int true "{name} ID"
|
||
|
// @Success 200 string successfully deleted
|
||
|
// @Failure 404 {object} httputil.HTTPError
|
||
|
// @Failure 400 {object} httputil.HTTPError
|
||
|
// @Failure 500 {object} httputil.HTTPError
|
||
|
// @Router /{name}s/{id} [delete]
|
||
|
func (c *controller) delete{Name}(ctx *gin.Context) {
|
||
|
var id = ctx.Param("id")
|
||
|
id_convert, err := strconv.Atoi(id)
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
{name}, err := c.service.GetByID(id_convert)
|
||
|
if err != nil || {name} == nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusNotFound, entity.ErrNotFound.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err = c.service.Delete(id_convert)
|
||
|
if err != nil {
|
||
|
utils.ResponseJSON(ctx, http.StatusBadRequest, err.Error(), nil)
|
||
|
return
|
||
|
}
|
||
|
utils.ResponseJSON(ctx, http.StatusAccepted, "successfully deleted", nil)
|
||
|
}
|