fusenapi/server/product-template/internal/logic/addbasemaplogic.go
laodaming 002ec9d292 fix
2023-06-25 14:11:47 +08:00

62 lines
1.6 KiB
Go

package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"gorm.io/gorm"
"net/http"
"strings"
"time"
"context"
"fusenapi/server/product-template/internal/svc"
"fusenapi/server/product-template/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type AddBaseMapLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewAddBaseMapLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddBaseMapLogic {
return &AddBaseMapLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AddBaseMapLogic) AddBaseMap(req *types.AddBaseMapReq, r *http.Request) (resp *basic.Response) {
authKey := r.Header.Get("Auth-Key")
genentModel := gmodel.NewFsGerentModel(l.svcCtx.MysqlConn)
_, err := genentModel.Find(l.ctx, authKey)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first..")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
req.Name = strings.Trim(req.Name, " ")
req.Url = strings.Trim(req.Url, " ")
if req.Name == "" || req.Url == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param is empty")
}
now := time.Now().Unix()
err = l.svcCtx.AllModels.FsProductTemplateBasemap.Create(l.ctx, &gmodel.FsProductTemplateBasemap{
Name: &req.Name,
Url: &req.Url,
Ctime: &now,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeSaveErr, "failed to add base map")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}