package merchant import ( "time" "com.snow.auto_monitor/app/constants/errorcode" common "com.snow.auto_monitor/app/http/controllers" merEnt "com.snow.auto_monitor/app/http/entities/merchant" merMod "com.snow.auto_monitor/app/models/merchant" merServ "com.snow.auto_monitor/app/services/merchant" "github.com/gin-gonic/gin" ) func GetById(c *gin.Context) { request := new(merEnt.GetListByIdReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } res, err := merServ.GetById(request.Id) if err != nil { common.Error500(c) return } var response *merEnt.GetListByIdResp = nil if res != nil { response = &merEnt.GetListByIdResp{ Id: res.Id, Name: res.Name, CreatedAt: res.CreatedAt.Format(time.RFC3339), } } common.Success(c, response) } func Search(c *gin.Context) { request := new(merEnt.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 := merServ.Search( request.Id, request.Name, start, end, request.PageSize, request.PageNum, ) if err != nil { common.Error500(c) return } var response []*merEnt.SearchResp = nil if len(res) > 0 { for _, item := range res { response = append(response, &merEnt.SearchResp{ Id: item.Id, Name: item.Name, CreatedAt: item.CreatedAt.Format(time.RFC3339), }) } } total, err := merServ.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(merEnt.CreateReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } merchant := &merMod.Merchant{ Name: request.Name, PrivateKey: request.PrivateKey, } affected, err := merServ.Create(merchant) if err != nil && affected > 0 { common.Error500(c) return } response := &merEnt.CreateResp{ Id: merchant.Id, } common.Success(c, response) } func Update(c *gin.Context) { request := new(merEnt.UpdateReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } merchant := &merMod.Merchant{ Id: request.Id, Name: request.Name, PrivateKey: request.PrivateKey, } affected, err := merServ.Update(merchant) if err != nil && affected > 0 { common.Error500(c) return } response := &merEnt.UpdateResp{ Id: merchant.Id, } common.Success(c, response) } func Delete(c *gin.Context) { request := new(merEnt.DeleteReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } affected, err := merServ.Delete(request.Id) if err != nil && affected > 0 { common.Error500(c) return } response := &merEnt.DeleteResp{ Id: request.Id, } common.Success(c, response) }