Refactor: Remove deprecated gRPC service files and implement new API structure

- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`.
- Added new API server implementations for objectstory, player, and shop services.
- Introduced configuration files for new APIs in `etc/*.yaml`.
- Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`.
- Removed unused user update handler and user API files.
- Added utility functions for context management and HTTP header parsing.
- Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
This commit is contained in:
wwweww
2026-02-28 18:35:56 +08:00
parent d2f33b4b96
commit 19cc7a778c
349 changed files with 42548 additions and 1453 deletions
@@ -1,4 +1,4 @@
Name: juwan-api
Name: shop-api
Host: 0.0.0.0
Port: 8888
@@ -4,24 +4,34 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 接受邀请
func AcceptInvitationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.AcceptInvitationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := shop.NewAcceptInvitationLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewAcceptInvitationLogic(ctx, svcCtx)
resp, err := l.AcceptInvitation(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 新增店铺公告
@@ -21,7 +26,12 @@ func AddAnnouncementHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewAddAnnouncementLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewAddAnnouncementLogic(ctx, svcCtx)
resp, err := l.AddAnnouncement(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 创建店铺
@@ -21,7 +26,12 @@ func CreateShopHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewCreateShopLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewCreateShopLogic(ctx, svcCtx)
resp, err := l.CreateShop(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,24 +4,34 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 删除店铺公告
func DeleteAnnouncementHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.DeleteAnnouncementReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := shop.NewDeleteAnnouncementLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewDeleteAnnouncementLogic(ctx, svcCtx)
resp, err := l.DeleteAnnouncement(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 获取当前用户的店铺
@@ -21,7 +26,12 @@ func GetMyShopHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewGetMyShopLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewGetMyShopLogic(ctx, svcCtx)
resp, err := l.GetMyShop(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -6,16 +6,17 @@ package shop
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 获取店铺详情
func GetShopHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.ShopIdReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
@@ -4,24 +4,34 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 获取收入统计
func GetShopIncomeStatsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.AcceptInvitationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := shop.NewGetShopIncomeStatsLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewGetShopIncomeStatsLogic(ctx, svcCtx)
resp, err := l.GetShopIncomeStats(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -6,16 +6,17 @@ package shop
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 获取店长的店铺
func GetUserShopHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.UserIdReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 邀请打手
@@ -21,7 +26,12 @@ func InvitePlayerHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewInvitePlayerLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewInvitePlayerLogic(ctx, svcCtx)
resp, err := l.InvitePlayer(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,24 +4,34 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 拒绝邀请
func RejectInvitationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.AcceptInvitationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := shop.NewRejectInvitationLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewRejectInvitationLogic(ctx, svcCtx)
resp, err := l.RejectInvitation(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,24 +4,34 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 移除打手
func RemovePlayerHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EmptyResp
var req types.InvitationReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := shop.NewRemovePlayerLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewRemovePlayerLogic(ctx, svcCtx)
resp, err := l.RemovePlayer(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 更新店铺信息
@@ -21,7 +26,12 @@ func UpdateShopHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewUpdateShopLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewUpdateShopLogic(ctx, svcCtx)
resp, err := l.UpdateShop(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -4,12 +4,17 @@
package shop
import (
"errors"
"juwan-backend/common/utils/contextj"
"juwan-backend/common/utils/httpj"
"juwan-backend/common/utils/responses"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/shop/api/internal/logic/shop"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 更新店铺模板
@@ -21,7 +26,12 @@ func UpdateShopTemplateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return
}
l := shop.NewUpdateShopTemplateLogic(r.Context(), svcCtx)
userId, err := httpj.GetUserIdFromHeader(r.Header)
if err != nil {
httpx.ErrorCtx(r.Context(), w, responses.NewErrorResp(403, errors.New("forbidden: user not authenticated")))
}
ctx := contextj.WithUserID(r.Context(), userId)
l := shop.NewUpdateShopTemplateLogic(ctx, svcCtx)
resp, err := l.UpdateShopTemplate(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
@@ -5,9 +5,12 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +31,44 @@ func NewAcceptInvitationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
func (l *AcceptInvitationLogic) AcceptInvitation(req *types.AcceptInvitationReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
AcceptInvitationLogic{}
return
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
invitation, err := l.svcCtx.ShopRpc.GetShopInvitationsById(l.ctx, &pb.GetShopInvitationsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if invitation.ShopInvitations == nil {
return nil, errors.New("invitation not found")
}
if invitation.ShopInvitations.PlayerId != userId {
return nil, errors.New("bad request: invitation not found or not owned by user")
}
i, err := l.svcCtx.ShopRpc.UpdateShopInvitations(l.ctx, &pb.UpdateShopInvitationsReq{
Id: req.Id,
ShopId: invitation.ShopInvitations.ShopId,
PlayerId: userId,
Status: "accepted",
InvitedBy: invitation.ShopInvitations.InvitedBy,
CreatedAt: invitation.ShopInvitations.CreatedAt,
RespondedAt: time.Now().Unix(),
})
if err != nil {
return nil, err
}
_, err = l.svcCtx.ShopRpc.AddShopPlayers(l.ctx, &pb.AddShopPlayersReq{
ShopId: i.ShopInvitations.ShopId,
PlayerId: userId,
IsPrimary: false,
JoinedAt: time.Now().Unix(),
LeftAt: 0,
})
if err != nil {
return nil, err
}
_ = i
return &types.EmptyResp{}, nil
}
@@ -5,6 +5,9 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
@@ -28,7 +31,41 @@ func NewAddAnnouncementLogic(ctx context.Context, svcCtx *svc.ServiceContext) *A
}
func (l *AddAnnouncementLogic) AddAnnouncement(req *types.AnnouncementReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userId, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userId {
return nil, contextj.ERRILLEGALUSER
}
shop.Shops.Announcements = append(shop.Shops.Announcements, req.Content)
_, err = l.svcCtx.ShopRpc.UpdateShops(l.ctx, &pb.UpdateShopsReq{
Id: shop.Shops.Id,
OwnerId: shop.Shops.OwnerId,
Name: shop.Shops.Name,
Banner: shop.Shops.Banner,
Description: shop.Shops.Description,
Rating: shop.Shops.Rating,
TotalOrders: shop.Shops.TotalOrders,
PlayerCount: shop.Shops.PlayerCount,
CommissionType: shop.Shops.CommissionType,
CommissionValue: shop.Shops.CommissionValue,
AllowMultiShop: shop.Shops.AllowMultiShop,
AllowIndependentOrders: shop.Shops.AllowIndependentOrders,
DispatchMode: shop.Shops.DispatchMode,
Announcements: shop.Shops.Announcements,
TemplateConfig: shop.Shops.TemplateConfig,
})
if err != nil {
return nil, err
}
return
return &types.EmptyResp{}, nil
}
@@ -5,6 +5,10 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
@@ -28,7 +32,42 @@ func NewCreateShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create
}
func (l *CreateShopLogic) CreateShop(req *types.CreateShopReq) (resp *types.ShopProfile, err error) {
// todo: add your logic here and delete this line
ownerID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
if req.Name == "" {
return nil, errors.New("name is required")
}
_, err = l.svcCtx.ShopRpc.AddShops(l.ctx, &pb.AddShopsReq{
OwnerId: ownerID,
Name: req.Name,
Description: req.Description,
Rating: "5",
TotalOrders: 0,
PlayerCount: 0,
CommissionType: req.CommissionType,
CommissionValue: req.CommissionValue,
AllowMultiShop: false,
AllowIndependentOrders: true,
DispatchMode: "manual",
Announcements: []string{},
TemplateConfig: `{}`,
})
if err != nil {
return nil, err
}
shop, err := getShopByOwnerID(l.ctx, l.svcCtx.ShopRpc, ownerID)
if err != nil {
return nil, err
}
if shop == nil {
return nil, errors.New("create shop failed")
}
return toShopProfile(shop), nil
return
}
@@ -5,9 +5,12 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +31,48 @@ func NewDeleteAnnouncementLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *DeleteAnnouncementLogic) DeleteAnnouncement(req *types.DeleteAnnouncementReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
return
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
index := int(req.Index)
if index < 0 || index >= len(shop.Shops.Announcements) {
return nil, errors.New("announcement index out of range")
}
shop.Shops.Announcements = append(shop.Shops.Announcements[:index], shop.Shops.Announcements[index+1:]...)
_, err = l.svcCtx.ShopRpc.UpdateShops(l.ctx, &pb.UpdateShopsReq{
Id: shop.Shops.Id,
OwnerId: shop.Shops.OwnerId,
Name: shop.Shops.Name,
Banner: shop.Shops.Banner,
Description: shop.Shops.Description,
Rating: shop.Shops.Rating,
TotalOrders: shop.Shops.TotalOrders,
PlayerCount: shop.Shops.PlayerCount,
CommissionType: shop.Shops.CommissionType,
CommissionValue: shop.Shops.CommissionValue,
AllowMultiShop: shop.Shops.AllowMultiShop,
AllowIndependentOrders: shop.Shops.AllowIndependentOrders,
DispatchMode: shop.Shops.DispatchMode,
Announcements: shop.Shops.Announcements,
TemplateConfig: shop.Shops.TemplateConfig,
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -5,9 +5,11 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +30,19 @@ func NewGetMyShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMySh
}
func (l *GetMyShopLogic) GetMyShop(req *types.EmptyResp) (resp *types.ShopProfile, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
shop, err := getShopByOwnerID(l.ctx, l.svcCtx.ShopRpc, userID)
if err != nil {
return nil, err
}
if shop == nil {
return nil, errors.New("shop not found")
}
return toShopProfile(shop), nil
return
}
@@ -5,9 +5,12 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +31,28 @@ func NewGetShopIncomeStatsLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *GetShopIncomeStatsLogic) GetShopIncomeStats(req *types.AcceptInvitationReq) (resp *types.IncomeStatsResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
return &types.IncomeStatsResp{
MonthlyIncome: "0",
PendingSettlement: "0",
TotalWithdrawn: "0",
TotalOrders: shop.Shops.TotalOrders,
CompletedOrders: shop.Shops.TotalOrders,
}, nil
return
}
@@ -5,6 +5,9 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
@@ -28,7 +31,13 @@ func NewGetShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetShopLo
}
func (l *GetShopLogic) GetShop(req *types.ShopIdReq) (resp *types.ShopProfile, err error) {
// todo: add your logic here and delete this line
result, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if result.Shops == nil {
return nil, errors.New("shop not found")
}
return toShopProfile(result.Shops), nil
return
}
@@ -5,6 +5,7 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
@@ -28,7 +29,13 @@ func NewGetUserShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
}
func (l *GetUserShopLogic) GetUserShop(req *types.UserIdReq) (resp *types.ShopProfile, err error) {
// todo: add your logic here and delete this line
shop, err := getShopByOwnerID(l.ctx, l.svcCtx.ShopRpc, req.Id)
if err != nil {
return nil, err
}
if shop == nil {
return nil, errors.New("shop not found")
}
return toShopProfile(shop), nil
return
}
@@ -0,0 +1,65 @@
package shop
import (
"context"
"encoding/json"
"strconv"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/app/shop/rpc/shopservice"
)
func toShopProfile(in *pb.Shops) *types.ShopProfile {
if in == nil {
return nil
}
var template any
if in.TemplateConfig != "" {
_ = json.Unmarshal([]byte(in.TemplateConfig), &template)
}
if template == nil {
template = map[string]any{}
}
ownerID := strconv.FormatInt(in.OwnerId, 10)
return &types.ShopProfile{
Id: strconv.FormatInt(in.Id, 10),
Owner: types.UserProfile{
Id: ownerID,
Username: "",
Nickname: "",
Avatar: "",
Role: "owner",
VerifiedRoles: []string{"owner"},
VerificationStatus: map[string]string{},
CreatedAt: "",
},
Name: in.Name,
Banner: in.Banner,
Description: in.Description,
Rating: in.Rating,
TotalOrders: in.TotalOrders,
PlayerCount: in.PlayerCount,
CommissionType: in.CommissionType,
CommissionValue: in.CommissionValue,
Announcements: in.Announcements,
TemplateConfig: template,
}
}
func getShopByOwnerID(ctx context.Context, rpc shopservice.ShopService, ownerID int64) (*pb.Shops, error) {
list, err := rpc.SearchShops(ctx, &pb.SearchShopsReq{
Page: 0,
Limit: 1,
OwnerId: ownerID,
})
if err != nil {
return nil, err
}
if len(list.Shops) == 0 {
return nil, nil
}
return list.Shops[0], nil
}
@@ -5,9 +5,13 @@ package shop
import (
"context"
"errors"
"time"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +32,33 @@ func NewInvitePlayerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Invi
}
func (l *InvitePlayerLogic) InvitePlayer(req *types.InvitationReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
return
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
_, err = l.svcCtx.ShopRpc.AddShopInvitations(l.ctx, &pb.AddShopInvitationsReq{
ShopId: req.Id,
PlayerId: req.PlayerId,
Status: "pending",
InvitedBy: userID,
CreatedAt: time.Now().Unix(),
RespondedAt: 0,
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -6,6 +6,8 @@ package shop
import (
"context"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
@@ -28,7 +30,33 @@ func NewListShopsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListSho
}
func (l *ListShopsLogic) ListShops(req *types.PageReq) (resp *types.ShopListResp, err error) {
// todo: add your logic here and delete this line
if req.Limit <= 0 {
req.Limit = 20
}
result, err := l.svcCtx.ShopRpc.SearchShops(l.ctx, &pb.SearchShopsReq{
Page: req.Offset / req.Limit,
Limit: req.Limit,
})
if err != nil {
return nil, err
}
items := make([]types.ShopProfile, 0, len(result.Shops))
for _, item := range result.Shops {
profile := toShopProfile(item)
if profile != nil {
items = append(items, *profile)
}
}
return &types.ShopListResp{
Items: items,
Meta: types.PageMeta{
Total: int64(len(items)),
Offset: req.Offset,
Limit: req.Limit,
},
}, nil
return
}
@@ -5,9 +5,13 @@ package shop
import (
"context"
"errors"
"time"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +32,34 @@ func NewRejectInvitationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
func (l *RejectInvitationLogic) RejectInvitation(req *types.AcceptInvitationReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
return
invitation, err := l.svcCtx.ShopRpc.GetShopInvitationsById(l.ctx, &pb.GetShopInvitationsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if invitation.ShopInvitations == nil {
return nil, errors.New("invitation not found")
}
if invitation.ShopInvitations.PlayerId != userID {
return nil, contextj.ERRILLEGALUSER
}
_, err = l.svcCtx.ShopRpc.UpdateShopInvitations(l.ctx, &pb.UpdateShopInvitationsReq{
Id: req.Id,
ShopId: invitation.ShopInvitations.ShopId,
PlayerId: invitation.ShopInvitations.PlayerId,
Status: "rejected",
InvitedBy: invitation.ShopInvitations.InvitedBy,
CreatedAt: invitation.ShopInvitations.CreatedAt,
RespondedAt: time.Now().Unix(),
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -5,9 +5,13 @@ package shop
import (
"context"
"errors"
"time"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +32,31 @@ func NewRemovePlayerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Remo
}
func (l *RemovePlayerLogic) RemovePlayer(req *types.InvitationReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
return
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
_, err = l.svcCtx.ShopRpc.UpdateShopPlayers(l.ctx, &pb.UpdateShopPlayersReq{
ShopId: req.Id,
PlayerId: req.PlayerId,
IsPrimary: false,
LeftAt: time.Now().Unix(),
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -5,9 +5,12 @@ package shop
import (
"context"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -27,8 +30,69 @@ func NewUpdateShopLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Update
}
}
func (l *UpdateShopLogic) UpdateShop(req *types.ShopIdReq) (resp *types.ShopProfile, err error) {
// todo: add your logic here and delete this line
func (l *UpdateShopLogic) UpdateShop(req *types.UpdateShopReq) (resp *types.ShopProfile, err error) {
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
current, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if current.Shops == nil {
return nil, errors.New("shop not found")
}
if current.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
name := current.Shops.Name
if req.Name != "" {
name = req.Name
}
description := current.Shops.Description
if req.Description != "" {
description = req.Description
}
commissionType := current.Shops.CommissionType
if req.CommissionType != "" {
commissionType = req.CommissionType
}
commissionValue := current.Shops.CommissionValue
if req.CommissionValue != "" {
commissionValue = req.CommissionValue
}
dispatchMode := current.Shops.DispatchMode
if req.DispatchMode != "" {
dispatchMode = req.DispatchMode
}
_, err = l.svcCtx.ShopRpc.UpdateShops(l.ctx, &pb.UpdateShopsReq{
Id: current.Shops.Id,
OwnerId: current.Shops.OwnerId,
Name: name,
Banner: current.Shops.Banner,
Description: description,
Rating: current.Shops.Rating,
TotalOrders: current.Shops.TotalOrders,
PlayerCount: current.Shops.PlayerCount,
CommissionType: commissionType,
CommissionValue: commissionValue,
AllowMultiShop: req.AllowMultiShop,
AllowIndependentOrders: req.AllowIndependentOrders,
DispatchMode: dispatchMode,
Announcements: current.Shops.Announcements,
TemplateConfig: current.Shops.TemplateConfig,
})
if err != nil {
return nil, err
}
updated, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
return toShopProfile(updated.Shops), nil
return
}
@@ -5,9 +5,13 @@ package shop
import (
"context"
"encoding/json"
"errors"
"juwan-backend/app/shop/api/internal/svc"
"juwan-backend/app/shop/api/internal/types"
"juwan-backend/app/shop/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -28,7 +32,48 @@ func NewUpdateShopTemplateLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
func (l *UpdateShopTemplateLogic) UpdateShopTemplate(req *types.UpdateTemplateReq) (resp *types.EmptyResp, err error) {
// todo: add your logic here and delete this line
userID, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
shop, err := l.svcCtx.ShopRpc.GetShopsById(l.ctx, &pb.GetShopsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if shop.Shops == nil {
return nil, errors.New("shop not found")
}
if shop.Shops.OwnerId != userID {
return nil, contextj.ERRILLEGALUSER
}
templateBytes, err := json.Marshal(map[string]any{"sections": req.Sections})
if err != nil {
return nil, errors.New("invalid sections")
}
_, err = l.svcCtx.ShopRpc.UpdateShops(l.ctx, &pb.UpdateShopsReq{
Id: shop.Shops.Id,
OwnerId: shop.Shops.OwnerId,
Name: shop.Shops.Name,
Banner: shop.Shops.Banner,
Description: shop.Shops.Description,
Rating: shop.Shops.Rating,
TotalOrders: shop.Shops.TotalOrders,
PlayerCount: shop.Shops.PlayerCount,
CommissionType: shop.Shops.CommissionType,
CommissionValue: shop.Shops.CommissionValue,
AllowMultiShop: shop.Shops.AllowMultiShop,
AllowIndependentOrders: shop.Shops.AllowIndependentOrders,
DispatchMode: shop.Shops.DispatchMode,
Announcements: shop.Shops.Announcements,
TemplateConfig: string(templateBytes),
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
return
}
+19 -19
View File
@@ -13,10 +13,10 @@ type AnnouncementReq struct {
}
type CreateShopReq struct {
Name string `json:"name"`
Description string `json:"description"`
CommissionType string `json:"commissionType"`
CommissionValue float64 `json:"commissionValue"`
Name string `json:"name"`
Description string `json:"description"`
CommissionType string `json:"commissionType"`
CommissionValue string `json:"commissionValue"`
}
type DeleteAnnouncementReq struct {
@@ -28,11 +28,11 @@ type EmptyResp struct {
}
type IncomeStatsResp struct {
MonthlyIncome float64 `json:"monthlyIncome"`
PendingSettlement float64 `json:"pendingSettlement"`
TotalWithdrawn float64 `json:"totalWithdrawn"`
TotalOrders int64 `json:"totalOrders"`
CompletedOrders int64 `json:"completedOrders"`
MonthlyIncome string `json:"monthlyIncome"`
PendingSettlement string `json:"pendingSettlement"`
TotalWithdrawn string `json:"totalWithdrawn"`
TotalOrders int64 `json:"totalOrders"`
CompletedOrders int64 `json:"completedOrders"`
}
type InvitationReq struct {
@@ -66,11 +66,11 @@ type ShopProfile struct {
Name string `json:"name"`
Banner string `json:"banner,optional"`
Description string `json:"description"`
Rating float64 `json:"rating"`
Rating string `json:"rating"`
TotalOrders int64 `json:"totalOrders"`
PlayerCount int64 `json:"playerCount"`
CommissionType string `json:"commissionType"`
CommissionValue float64 `json:"commissionValue"`
CommissionValue string `json:"commissionValue"`
Announcements []string `json:"announcements"`
TemplateConfig interface{} `json:"templateConfig"`
}
@@ -82,14 +82,14 @@ type SimpleUser struct {
}
type UpdateShopReq struct {
Id int64 `path:"id"`
Name string `json:"name,optional"`
Description string `json:"description,optional"`
CommissionType string `json:"commissionType,optional"`
CommissionValue float64 `json:"commissionValue,optional"`
AllowMultiShop bool `json:"allowMultiShop,optional"`
AllowIndependentOrders bool `json:"allowIndependentOrders,optional"`
DispatchMode string `json:"dispatchMode,optional"`
Id int64 `path:"id"`
Name string `json:"name,optional"`
Description string `json:"description,optional"`
CommissionType string `json:"commissionType,optional"`
CommissionValue string `json:"commissionValue,optional"`
AllowMultiShop bool `json:"allowMultiShop,optional"`
AllowIndependentOrders bool `json:"allowIndependentOrders,optional"`
DispatchMode string `json:"dispatchMode,optional"`
}
type UpdateTemplateReq struct {
@@ -15,7 +15,7 @@ import (
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "etc/juwan-api.yaml", "the config file")
var configFile = flag.String("f", "etc/shop-api.yaml", "the config file")
func main() {
flag.Parse()