框架基本搭建完成
This commit is contained in:
51
pkg/service/users.go
Normal file
51
pkg/service/users.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"drive-linked/pkg/dto"
|
||||
"drive-linked/pkg/model"
|
||||
"drive-linked/pkg/serializer"
|
||||
"github.com/kataras/iris/v12"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UsersService struct {
|
||||
Ctx iris.Context
|
||||
}
|
||||
|
||||
const (
|
||||
MethodUserName = "name"
|
||||
MethodUserEmail = "email"
|
||||
)
|
||||
|
||||
func NewUsersService(ctx iris.Context) *UsersService {
|
||||
return &UsersService{Ctx: ctx}
|
||||
}
|
||||
|
||||
func (serv *UsersService) GetOneUser(field, method string) {
|
||||
var err error
|
||||
resp := dto.NewResponse(serv.Ctx)
|
||||
user := &model.User{}
|
||||
|
||||
switch method {
|
||||
case MethodUserName:
|
||||
err = user.GetByName(field)
|
||||
case MethodUserEmail:
|
||||
err = user.GetByEmail(field)
|
||||
}
|
||||
|
||||
switch err {
|
||||
case nil:
|
||||
// 复制到dto对象
|
||||
var userResult dto.UserProfile
|
||||
err = userResult.CopyOf(user)
|
||||
if err != nil {
|
||||
resp.Error(http.StatusInternalServerError, "service.users Error")
|
||||
}
|
||||
resp.Success(userResult)
|
||||
case sql.ErrNoRows:
|
||||
resp.Error(serializer.ErrNoUser, "找不到此用户")
|
||||
default:
|
||||
resp.Error(http.StatusInternalServerError, "获取用户信息失败,数据库异常")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user