168 lines
3.0 KiB
Go
168 lines
3.0 KiB
Go
|
package product
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"com.snow.auto_monitor/app/constants/errorcode"
|
||
|
common "com.snow.auto_monitor/app/http/controllers"
|
||
|
proEnt "com.snow.auto_monitor/app/http/entities/product"
|
||
|
proMod "com.snow.auto_monitor/app/models/product"
|
||
|
proServ "com.snow.auto_monitor/app/services/product"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
func GetById(c *gin.Context) {
|
||
|
request := new(proEnt.GetListByIdReq)
|
||
|
err := common.GenRequest(c, request)
|
||
|
if err != nil {
|
||
|
common.Error(c, errorcode.ParamError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
res, err := proServ.GetById(request.Id)
|
||
|
if err != nil {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var response *proEnt.GetListByIdResp = nil
|
||
|
|
||
|
if res != nil {
|
||
|
|
||
|
response = &proEnt.GetListByIdResp{
|
||
|
Id: res.Id,
|
||
|
Name: res.Name,
|
||
|
Price: res.Price,
|
||
|
CreatedAt: res.CreatedAt.Format(time.RFC3339),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
common.Success(c, response)
|
||
|
}
|
||
|
|
||
|
func Search(c *gin.Context) {
|
||
|
request := new(proEnt.SearcgReq)
|
||
|
err := common.GenRequest(c, request)
|
||
|
if err != nil {
|
||
|
common.Error(c, errorcode.ParamError)
|
||
|
return
|
||
|
}
|
||
|
start, end := "", ""
|
||
|
if len(request.CreatedAt) > 1 {
|
||
|
start = request.CreatedAt[0]
|
||
|
end = request.CreatedAt[1]
|
||
|
}
|
||
|
res, err := proServ.Search(
|
||
|
request.Id,
|
||
|
request.Name,
|
||
|
start,
|
||
|
end,
|
||
|
request.PageSize,
|
||
|
request.PageNum,
|
||
|
)
|
||
|
if err != nil {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var response []*proEnt.SearchResp = nil
|
||
|
|
||
|
if len(res) > 0 {
|
||
|
for _, item := range res {
|
||
|
response = append(response, &proEnt.SearchResp{
|
||
|
Id: item.Id,
|
||
|
Name: item.Name,
|
||
|
Price: item.Price,
|
||
|
CreatedAt: item.CreatedAt.Format(time.RFC3339),
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
total, err := proServ.CountAll(
|
||
|
request.Id,
|
||
|
request.Name,
|
||
|
start,
|
||
|
end,
|
||
|
)
|
||
|
if err != nil {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
common.SuccessWithList(c, response, total)
|
||
|
}
|
||
|
|
||
|
func Create(c *gin.Context) {
|
||
|
request := new(proEnt.CreateReq)
|
||
|
err := common.GenRequest(c, request)
|
||
|
if err != nil {
|
||
|
common.Error(c, errorcode.ParamError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
product := &proMod.Product{
|
||
|
Name: request.Name,
|
||
|
Price: request.Price,
|
||
|
}
|
||
|
|
||
|
affected, err := proServ.Create(product)
|
||
|
if err != nil && affected > 0 {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response := &proEnt.CreateResp{
|
||
|
Id: product.Id,
|
||
|
}
|
||
|
|
||
|
common.Success(c, response)
|
||
|
}
|
||
|
|
||
|
func Update(c *gin.Context) {
|
||
|
request := new(proEnt.UpdateReq)
|
||
|
err := common.GenRequest(c, request)
|
||
|
if err != nil {
|
||
|
common.Error(c, errorcode.ParamError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
product := &proMod.Product{
|
||
|
Id: request.Id,
|
||
|
Name: request.Name,
|
||
|
Price: request.Price,
|
||
|
}
|
||
|
|
||
|
affected, err := proServ.Update(product)
|
||
|
if err != nil && affected > 0 {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response := &proEnt.UpdateResp{
|
||
|
Id: product.Id,
|
||
|
}
|
||
|
|
||
|
common.Success(c, response)
|
||
|
}
|
||
|
|
||
|
func Delete(c *gin.Context) {
|
||
|
request := new(proEnt.DeleteReq)
|
||
|
err := common.GenRequest(c, request)
|
||
|
if err != nil {
|
||
|
common.Error(c, errorcode.ParamError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
affected, err := proServ.Delete(request.Id)
|
||
|
if err != nil && affected > 0 {
|
||
|
common.Error500(c)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response := &proEnt.DeleteResp{
|
||
|
Id: request.Id,
|
||
|
}
|
||
|
|
||
|
common.Success(c, response)
|
||
|
}
|