Skip to content

Commit

Permalink
feat:protoc
Browse files Browse the repository at this point in the history
  • Loading branch information
CocaineCong committed May 18, 2023
1 parent d96494f commit 6f56c47
Show file tree
Hide file tree
Showing 20 changed files with 1,255 additions and 1,494 deletions.
18 changes: 9 additions & 9 deletions app/gateway/internal/handler/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (

"github.com/gin-gonic/gin"

taskPb "github.com/CocaineCong/grpc-todolist/idl/task"
"github.com/CocaineCong/grpc-todolist/idl/task/pb"
"github.com/CocaineCong/grpc-todolist/pkg/e"
"github.com/CocaineCong/grpc-todolist/pkg/res"
"github.com/CocaineCong/grpc-todolist/pkg/util"
)

func GetTaskList(ginCtx *gin.Context) {
var tReq taskPb.TaskRequest
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := util.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(taskPb.TaskServiceClient)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskShow(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Expand All @@ -29,11 +29,11 @@ func GetTaskList(ginCtx *gin.Context) {
}

func CreateTask(ginCtx *gin.Context) {
var tReq taskPb.TaskRequest
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := util.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(taskPb.TaskServiceClient)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskCreate(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Expand All @@ -45,11 +45,11 @@ func CreateTask(ginCtx *gin.Context) {
}

func UpdateTask(ginCtx *gin.Context) {
var tReq taskPb.TaskRequest
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := util.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(taskPb.TaskServiceClient)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskUpdate(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Expand All @@ -61,11 +61,11 @@ func UpdateTask(ginCtx *gin.Context) {
}

func DeleteTask(ginCtx *gin.Context) {
var tReq taskPb.TaskRequest
var tReq pb.TaskRequest
PanicIfTaskError(ginCtx.Bind(&tReq))
claim, _ := util.ParseToken(ginCtx.GetHeader("Authorization"))
tReq.UserID = uint32(claim.UserID)
TaskService := ginCtx.Keys["task"].(taskPb.TaskServiceClient)
TaskService := ginCtx.Keys["task"].(pb.TaskServiceClient)
TaskResp, err := TaskService.TaskDelete(context.Background(), &tReq)
PanicIfTaskError(err)
r := res.Response{
Expand Down
2 changes: 1 addition & 1 deletion app/gateway/internal/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gin-gonic/gin"

userPb "github.com/CocaineCong/grpc-todolist/idl/user"
userPb "github.com/CocaineCong/grpc-todolist/idl/user/pb"
"github.com/CocaineCong/grpc-todolist/pkg/e"
"github.com/CocaineCong/grpc-todolist/pkg/res"
"github.com/CocaineCong/grpc-todolist/pkg/util"
Expand Down
12 changes: 9 additions & 3 deletions app/user/internal/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"

"github.com/CocaineCong/grpc-todolist/app/user/internal/repository/db/dao"
userPb "github.com/CocaineCong/grpc-todolist/idl/user"
"github.com/CocaineCong/grpc-todolist/pkg/e"
)
Expand All @@ -24,12 +25,17 @@ func GetUserSrv() *UserSrv {
func (u *UserSrv) UserLogin(ctx context.Context, req *userPb.UserRequest) (resp *userPb.UserDetailResponse, err error) {
resp = new(userPb.UserDetailResponse)
resp.Code = e.SUCCESS
err = dao..ShowUserInfo(req)
r, err := dao.NewUserDao(ctx).GetUserInfo(req)
if err != nil {
resp.Code = e.ERROR
return resp, err
}
resp.UserDetail = repository.BuildUser(user)
userPb.UserModel{
UserID: r.UserID,
UserName: r.UserName,
NickName: r.UserName,
}
resp.UserDetail =
return resp, nil
}

Expand All @@ -46,7 +52,7 @@ func (u *UserSrv) UserRegister(ctx context.Context, req *service.UserRequest) (r
return resp, nil
}

func (u *UserSrv) UserLogout(ctx context.Context, req *service.UserRequest) (resp *service.UserDetailResponse, err error) {
func (u *UserSrv) UserLogout(ctx context.Context, req *service.UserRequest) (resp *service.UserDetailResponse, err error) {
resp = new(service.UserDetailResponse)
return resp, nil
}
44 changes: 16 additions & 28 deletions app/user/internal/repository/db/dao/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (

"gorm.io/gorm"

"github.com/CocaineCong/grpc-todolist/idl/user"
"github.com/CocaineCong/grpc-todolist/app/user/internal/repository/db/model"
userPb "github.com/CocaineCong/grpc-todolist/idl/user"
"github.com/CocaineCong/grpc-todolist/pkg/util"
)

Expand All @@ -18,45 +19,32 @@ func NewUserDao(ctx context.Context) *UserDao {
return &UserDao{NewDBClient(ctx)}
}

func (user *User) CheckUserExist(req *user.UserRequest) bool {
if err := DB.Where("user_name=?", req.UserName).First(&user).Error; err == gorm.ErrRecordNotFound {
return false
}
return true
}
// GetUserInfo 获取用户信息
func (dao *UserDao) GetUserInfo(req *userPb.UserRequest) (r *model.User, err error) {
err = dao.Model(&model.User{}).Where("user_name=?", req.UserName).
First(&r).Error

func (user *User) ShowUserInfo(req *service.UserRequest) (err error) {
if exist := user.CheckUserExist(req); exist {
return nil
}
return errors.New("UserName Not Exist")
return
}

func (*User) Create(req *service.UserRequest) error {
var user User
// CreateUser 用户创建
func (dao *UserDao) CreateUser(req *userPb.UserRequest) (err error) {
var user model.User
var count int64
DB.Where("user_name=?", req.UserName).Count(&count)
dao.Model(&model.User{}).Where("user_name = ?", req.UserName).Count(&count)
if count != 0 {
return errors.New("UserName Exist")
}
user = User{

user = model.User{
UserName: req.UserName,
NickName: req.NickName,
}
_ = user.SetPassword(req.Password)
if err := DB.Create(&user).Error; err != nil {
if err = dao.Model(&model.User{}).Create(&user).Error; err != nil {
util.LogrusObj.Error("Insert User Error:" + err.Error())
return err
return
}
return nil
}

// 视图返回
func BuildUser(item User) *service.UserModel {
userModel := service.UserModel{
UserID: uint32(item.UserID),
NickName: item.NickName,
UserName: item.UserName,
}
return &userModel
return
}
2 changes: 1 addition & 1 deletion app/user/internal/repository/db/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type User struct {
UserID uint `gorm:"primarykey"`
UserID int64 `gorm:"primarykey"`
UserName string `gorm:"unique"`
NickName string
PasswordDigest string
Expand Down
Loading

0 comments on commit 6f56c47

Please sign in to comment.