potato/internal/controller/api/v1/basic/vendor.go

210 lines
6.1 KiB
Go
Raw Normal View History

2021-06-10 19:03:15 +08:00
/*
* @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy
2021-06-11 15:46:30 +08:00
* @LastEditTime: 2021-06-11 15:22:16
2021-06-10 19:03:15 +08:00
* @FilePath: /potato/internal/controller/api/v1/basic/vendor.go
*/
2019-08-11 15:41:32 +08:00
package basic
import (
"github.com/gin-gonic/gin"
2021-06-11 15:46:30 +08:00
"github.com/jinzhu/gorm"
2021-06-11 01:24:00 +08:00
"github.com/viletyy/potato/global"
"github.com/viletyy/potato/internal/service"
"github.com/viletyy/potato/pkg/app"
"github.com/viletyy/potato/pkg/errcode"
"github.com/viletyy/yolk/convert"
2019-08-11 15:41:32 +08:00
)
2021-06-10 19:03:15 +08:00
type Vendor struct{}
2021-03-24 11:29:15 +08:00
2021-06-11 01:24:00 +08:00
func NewVendor() Vendor {
return Vendor{}
}
2019-08-11 15:41:32 +08:00
// @Summary 系统厂商列表
// @Tags vendors
// @Description
// @Accept json
// @Produce json
2021-06-10 22:41:58 +08:00
// @Param token header string true "auth by /auth"
2021-03-24 11:29:15 +08:00
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
2021-06-11 01:24:00 +08:00
// @Param name query string false "系统厂商名称" maxlength(100)
// @Param uuid query int false "系统厂商云id"
2021-06-10 19:03:15 +08:00
// @Success 200 {object} basic.Vendor "请求成功"
2019-08-11 15:41:32 +08:00
// @Router /v1/vendors [get]
2021-06-11 01:24:00 +08:00
func (vendor Vendor) List(c *gin.Context) {
param := service.VendorListRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
paper := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}
total, err := svc.CountVendor(&service.CountVendorRequest{Name: param.Name, Uuid: param.Uuid})
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CountVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorCountVendorFail)
return
}
vendors, err := svc.GetVendorList(&param, &paper)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetVendorList err: %v", err)
response.ToErrorResponse(errcode.ErrorGetVendorListFail)
return
}
response.ToResponseList(vendors, total)
}
// @Summary 系统厂商
// @Tags vendors
// @Description
// @Accept json
// @Produce json
// @Param token header string true "auth by /auth"
// @Param id path int true "系统厂商 ID"
// @Success 200 {object} basic.Vendor "请求成功"
// @Router /v1/vendors/{id} [get]
func (vendor Vendor) Get(c *gin.Context) {
vendorId, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.VendorRequest{ID: vendorId}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbVendor, err := svc.GetVendor(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.GetVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorGetVendorFail)
return
}
response.ToResponse(dbVendor)
}
2019-08-11 15:41:32 +08:00
// @Summary 新增系统厂商
// @Tags vendors
// @Description
// @Accept mpfd
// @Produce json
2021-06-10 22:41:58 +08:00
// @Param token header string true "auth by /auth"
2021-06-11 01:24:00 +08:00
// @Param name formData string true "系统厂商名称" minlength(1) maxlength(100)
// @Param uuid formData int false "系统厂商云id"
2021-06-10 19:03:15 +08:00
// @Success 200 {object} basic.Vendor "请求成功"
2019-08-11 15:41:32 +08:00
// @Router /v1/vendors [post]
2021-06-11 01:24:00 +08:00
func (vendor Vendor) Create(c *gin.Context) {
param := service.CreateVendorRequest{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs.Errors())
return
}
svc := service.New(c.Request.Context())
dbVendor, err := svc.CreateVendor(&param)
if err != nil {
global.GO_LOG.Sugar().Errorf("svc.CreateVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorCreateVendorFail)
return
}
response.ToResponse(dbVendor)
}
2019-08-11 15:41:32 +08:00
// @Summary 修改系统厂商
// @Tags vendors
// @Description
// @Accept mpfd
// @Produce json
2021-06-10 22:41:58 +08:00
// @Param token header string true "auth by /auth"
2019-08-11 15:41:32 +08:00
// @Param id path int true "系统厂商 ID"
2021-06-11 01:24:00 +08:00
// @Param name formData string false "系统厂商名称" minlength(1) maxlength(100)
// @Param uuid formData int false "系统厂商云id"
2021-06-10 19:03:15 +08:00
// @Success 200 {object} basic.Vendor "请求成功"
2019-08-11 15:41:32 +08:00
// @Router /v1/vendors/{id} [patch]
2021-06-11 01:24:00 +08:00
func (vendor Vendor) Update(c *gin.Context) {
vendorId, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.UpdateVendorRequest{ID: vendorId}
valid, errs := app.BindAndValid(c, &param)
if !valid {
global.GO_LOG.Sugar().Errorf("app.BindAndValid errs: %v", errs)
response.ToResponseErrors(errs)
return
}
svc := service.New(c.Request.Context())
dbVendor, err := svc.UpdateVendor(&param)
if err != nil {
2021-06-11 15:46:30 +08:00
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.UpdateVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorGetVendorFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.UpdateVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorUpdateVendorFail)
return
}
2021-06-11 01:24:00 +08:00
}
response.ToResponse(dbVendor)
}
2019-08-11 15:41:32 +08:00
// @Summary 删除系统厂商
// @Tags vendors
// @Description
// @Accept json
// @Produce json
2021-06-10 22:41:58 +08:00
// @Param token header string true "auth by /auth"
2019-08-11 15:41:32 +08:00
// @Param id path int true "系统厂商 ID"
2021-06-10 19:03:15 +08:00
// @Success 200 {object} basic.Vendor "请求成功"
2019-08-11 15:41:32 +08:00
// @Router /v1/vendors/{id} [delete]
2021-06-10 19:03:15 +08:00
func (vendor Vendor) Delete(c *gin.Context) {
2021-06-11 01:24:00 +08:00
vendorId, err := convert.StrTo(c.Param("id")).Int64()
response := app.NewResponse(c)
if err != nil {
global.GO_LOG.Sugar().Errorf("convert.StrTo err: %v", err)
response.ToErrorResponse(errcode.InvalidParams)
return
}
param := service.DeleteVendorRequest{ID: vendorId}
svc := service.New(c.Request.Context())
err = svc.DeleteVendor(&param)
if err != nil {
2021-06-11 15:46:30 +08:00
if err == gorm.ErrRecordNotFound {
global.GO_LOG.Sugar().Errorf("svc.DeleteVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorGetVendorFail)
return
} else {
global.GO_LOG.Sugar().Errorf("svc.DeleteVendor err: %v", err)
response.ToErrorResponse(errcode.ErrorDeleteVendorFail)
return
}
2021-06-11 01:24:00 +08:00
}
response.ToErrorResponse(errcode.Success)
2021-03-23 00:55:26 +08:00
}