1. message
package handler
import (
"context"
"mxshop_srvs/userop_srv/proto"
"mxshop_srvs/userop_srv/model"
"mxshop_srvs/userop_srv/global"
)
func (*UserOpServer) MessageList(ctx context.Context, req *proto.MessageRequest) (*proto.MessageListResponse, error) {
var rsp proto.MessageListResponse
var messages []model.LeavingMessages
var messageList []*proto.MessageResponse
result := global.DB.Where(&model.LeavingMessages{User:req.UserId}).Find(&messages)
rsp.Total = int32(result.RowsAffected)
for _, message := range messages {
messageList = append(messageList, &proto.MessageResponse{
Id: message.ID,
UserId: message.User,
MessageType: message.MessageType,
Subject: message.Subject,
Message: message.Message,
File: message.File,
})
}
rsp.Data = messageList
return &rsp, nil
}
func (*UserOpServer) CreateMessage(ctx context.Context, req *proto.MessageRequest) (*proto.MessageResponse, error) {
var message model.LeavingMessages
message.User = req.UserId
message.MessageType = req.MessageType
message.Subject = req.Subject
message.Message = req.Message
message.File = req.File
global.DB.Save(&message)
return &proto.MessageResponse{Id:message.ID}, nil
}
2. address
package handler
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"mxshop_srvs/userop_srv/model"
"mxshop_srvs/userop_srv/proto"
"mxshop_srvs/userop_srv/global"
)
func (*UserOpServer) GetAddressList(ctx context.Context, req *proto.AddressRequest) (*proto.AddressListResponse, error) {
var addresses []model.Address
var rsp proto.AddressListResponse
var addressResponse []*proto.AddressResponse
if result := global.DB.Where(&model.Address{User:req.UserId}).Find(&addresses); result.RowsAffected != 0 {
rsp.Total = int32(result.RowsAffected)
}
for _, address := range addresses {
addressResponse = append(addressResponse, &proto.AddressResponse{
Id: address.ID,
UserId: address.User,
Province: address.Province,
City: address.City,
District: address.District,
Address: address.Address,
SignerName: address.SignerName,
SignerMobile:address.SignerMobile,
})
}
rsp.Data = addressResponse
return &rsp, nil
}
func (*UserOpServer) CreateAddress(ctx context.Context, req *proto.AddressRequest) (*proto.AddressResponse, error) {
var address model.Address
address.User = req.UserId
address.Province = req.Province
address.City = req.City
address.District = req.District
address.Address = req.Address
address.SignerName = req.SignerName
address.SignerMobile = req.SignerMobile
global.DB.Save(&address)
return &proto.AddressResponse{Id:address.ID}, nil
}
func (*UserOpServer) DeleteAddress(ctx context.Context, req *proto.AddressRequest) (*emptypb.Empty, error) {
if result := global.DB.Where("id=? and user=?", req.Id, req.UserId).Delete(&model.Address{}); result.RowsAffected == 0{
return nil, status.Errorf(codes.NotFound, "收货地址不存在")
}
return &emptypb.Empty{}, nil
}
func (*UserOpServer) UpdateAddress(ctx context.Context, req *proto.AddressRequest) (*emptypb.Empty, error) {
var address model.Address
if result := global.DB.Where("id=? and user=?", req.Id, req.UserId).First(&address); result.RowsAffected == 0 {
return nil, status.Errorf(codes.NotFound, "购物车记录不存在")
}
if address.Province != "" {
address.Province = req.Province
}
if address.City != "" {
address.City = req.City
}
if address.District != "" {
address.District = req.District
}
if address.Address != "" {
address.Address = req.Address
}
if address.SignerName != "" {
address.SignerName = req.SignerName
}
if address.SignerMobile != "" {
address.SignerMobile = req.SignerMobile
}
global.DB.Save(&address)
return &emptypb.Empty{}, nil
}
3. userfav
package handler
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
"mxshop_srvs/userop_srv/model"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"mxshop_srvs/userop_srv/proto"
"mxshop_srvs/userop_srv/global"
)
func (*UserOpServer) GetFavList(ctx context.Context, req *proto.UserFavRequest) (*proto.UserFavListResponse, error) {
var rsp proto.UserFavListResponse
var userFavs []model.UserFav
var userFavList []*proto.UserFavResponse
result := global.DB.Where(&model.UserFav{User:req.UserId, Goods:req.GoodsId}).Find(&userFavs)
rsp.Total = int32(result.RowsAffected)
for _, userFav := range userFavs {
userFavList = append(userFavList, &proto.UserFavResponse{
UserId: userFav.User,
GoodsId: userFav.Goods,
})
}
rsp.Data = userFavList
return &rsp, nil
}
func (*UserOpServer) AddUserFav(ctx context.Context, req *proto.UserFavRequest) (*emptypb.Empty, error) {
var userFav model.UserFav
userFav.User = req.UserId
userFav.Goods = req.GoodsId
global.DB.Save(&userFav)
return &emptypb.Empty{}, nil
}
func (*UserOpServer) DeleteUserFav(ctx context.Context, req *proto.UserFavRequest) (*emptypb.Empty, error) {
if result := global.DB.Unscoped().Where("goods=? and user=?", req.GoodsId, req.UserId).Delete(&model.Address{}); result.RowsAffected == 0{
return nil, status.Errorf(codes.NotFound, "收藏记录不存在")
}
return &emptypb.Empty{}, nil
}
func (*UserOpServer) GetUserFavDetail(ctx context.Context, req *proto.UserFavRequest) (*emptypb.Empty, error) {
var userfav model.UserFav
if result := global.DB.Where("goods=? and user=?", req.GoodsId, req.UserId).Find(userfav); result.RowsAffected == 0{
return nil, status.Errorf(codes.NotFound, "收藏记录不存在")
}
return &emptypb.Empty{}, nil
}