init
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"affiliate_dash/internal/middleware"
|
||||
"affiliate_dash/internal/pkg/response"
|
||||
"affiliate_dash/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
svc *service.AuthService
|
||||
}
|
||||
|
||||
func NewAuthHandler(svc *service.AuthService) *AuthHandler {
|
||||
return &AuthHandler{svc: svc}
|
||||
}
|
||||
|
||||
type loginReq struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Login(c *gin.Context) {
|
||||
var req loginReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "请输入用户名和密码")
|
||||
return
|
||||
}
|
||||
result, err := h.svc.Login(req.Username, req.Password)
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, result)
|
||||
}
|
||||
|
||||
type registerReq struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=32"`
|
||||
Password string `json:"password" binding:"required,min=6,max=64"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Register(c *gin.Context) {
|
||||
var req registerReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误:用户名至少3位,密码至少6位")
|
||||
return
|
||||
}
|
||||
user, err := h.svc.Register(req.Username, req.Password, req.Nickname)
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, user)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Profile(c *gin.Context) {
|
||||
user, err := h.svc.GetProfile(middleware.GetUserID(c))
|
||||
if err != nil {
|
||||
response.NotFound(c, "用户不存在")
|
||||
return
|
||||
}
|
||||
response.OK(c, user)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"affiliate_dash/internal/middleware"
|
||||
"affiliate_dash/internal/model"
|
||||
"affiliate_dash/internal/pkg/response"
|
||||
"affiliate_dash/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type OrderHandler struct {
|
||||
svc *service.OrderService
|
||||
}
|
||||
|
||||
func NewOrderHandler(svc *service.OrderService) *OrderHandler {
|
||||
return &OrderHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *OrderHandler) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||||
q := service.OrderListQuery{
|
||||
Page: page,
|
||||
Size: size,
|
||||
Status: c.Query("status"),
|
||||
}
|
||||
// 分销商只能看自己的订单
|
||||
if middleware.GetRole(c) == model.RoleDistributor {
|
||||
id := middleware.GetUserID(c)
|
||||
q.DistributorID = &id
|
||||
} else if d := c.Query("distributor_id"); d != "" {
|
||||
id, _ := strconv.ParseUint(d, 10, 64)
|
||||
uid := uint(id)
|
||||
q.DistributorID = &uid
|
||||
}
|
||||
list, total, err := h.svc.List(q)
|
||||
if err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.Page(c, list, total, page, size)
|
||||
}
|
||||
|
||||
type createOrderReq struct {
|
||||
SkinID uint `json:"skin_id" binding:"required"`
|
||||
BuyerName string `json:"buyer_name"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
func (h *OrderHandler) Create(c *gin.Context) {
|
||||
var req createOrderReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
distributorID := middleware.GetUserID(c)
|
||||
// 管理员可指定分销商
|
||||
if middleware.GetRole(c) == model.RoleAdmin {
|
||||
if d := c.Query("distributor_id"); d != "" {
|
||||
id, _ := strconv.ParseUint(d, 10, 64)
|
||||
distributorID = uint(id)
|
||||
}
|
||||
}
|
||||
order, err := h.svc.Create(service.CreateOrderInput{
|
||||
SkinID: req.SkinID,
|
||||
DistributorID: distributorID,
|
||||
BuyerName: req.BuyerName,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, order)
|
||||
}
|
||||
|
||||
type orderStatusReq struct {
|
||||
Status string `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
func (h *OrderHandler) UpdateStatus(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var req orderStatusReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := h.svc.UpdateStatus(uint(id), req.Status); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
|
||||
func (h *OrderHandler) Dashboard(c *gin.Context) {
|
||||
stats, err := h.svc.Dashboard()
|
||||
if err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, stats)
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
"affiliate_dash/internal/pkg/response"
|
||||
"affiliate_dash/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SkinHandler struct {
|
||||
svc *service.SkinService
|
||||
}
|
||||
|
||||
func NewSkinHandler(svc *service.SkinService) *SkinHandler {
|
||||
return &SkinHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *SkinHandler) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||||
q := service.SkinListQuery{
|
||||
Page: page,
|
||||
Size: size,
|
||||
Keyword: c.Query("keyword"),
|
||||
Game: c.Query("game"),
|
||||
Category: c.Query("category"),
|
||||
}
|
||||
if s := c.Query("status"); s != "" {
|
||||
v, _ := strconv.Atoi(s)
|
||||
q.Status = &v
|
||||
}
|
||||
list, total, err := h.svc.List(q)
|
||||
if err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.Page(c, list, total, page, size)
|
||||
}
|
||||
|
||||
func (h *SkinHandler) Get(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
skin, err := h.svc.Get(uint(id))
|
||||
if err != nil {
|
||||
response.NotFound(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, skin)
|
||||
}
|
||||
|
||||
type skinCreateReq struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Game string `json:"game"`
|
||||
Category string `json:"category"`
|
||||
CoverURL string `json:"cover_url"`
|
||||
Price float64 `json:"price" binding:"required"`
|
||||
CostPrice float64 `json:"cost_price"`
|
||||
Commission float64 `json:"commission"`
|
||||
Stock int `json:"stock"`
|
||||
Status int `json:"status"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
func (h *SkinHandler) Create(c *gin.Context) {
|
||||
var req skinCreateReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
status := req.Status
|
||||
if status != 0 && status != 1 {
|
||||
status = 1
|
||||
}
|
||||
// 未传 status 时默认上架;若明确要下架请先创建再更新
|
||||
if status == 0 {
|
||||
status = 1
|
||||
}
|
||||
skin := &model.Skin{
|
||||
Name: req.Name,
|
||||
Game: req.Game,
|
||||
Category: req.Category,
|
||||
CoverURL: req.CoverURL,
|
||||
Price: req.Price,
|
||||
CostPrice: req.CostPrice,
|
||||
Commission: req.Commission,
|
||||
Stock: req.Stock,
|
||||
Status: status,
|
||||
Description: req.Description,
|
||||
}
|
||||
if err := h.svc.Create(skin); err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, skin)
|
||||
}
|
||||
|
||||
func (h *SkinHandler) Update(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var updates map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&updates); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
delete(updates, "id")
|
||||
delete(updates, "created_at")
|
||||
delete(updates, "updated_at")
|
||||
if err := h.svc.Update(uint(id), updates); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
|
||||
func (h *SkinHandler) Delete(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err := h.svc.Delete(uint(id)); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"affiliate_dash/internal/pkg/response"
|
||||
"affiliate_dash/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
svc *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(svc *service.UserService) *UserHandler {
|
||||
return &UserHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *UserHandler) List(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||||
q := service.UserListQuery{
|
||||
Page: page,
|
||||
Size: size,
|
||||
Keyword: c.Query("keyword"),
|
||||
Role: c.Query("role"),
|
||||
}
|
||||
if s := c.Query("status"); s != "" {
|
||||
v, _ := strconv.Atoi(s)
|
||||
q.Status = &v
|
||||
}
|
||||
list, total, err := h.svc.List(q)
|
||||
if err != nil {
|
||||
response.ServerError(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.Page(c, list, total, page, size)
|
||||
}
|
||||
|
||||
type createUserReq struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Nickname string `json:"nickname"`
|
||||
Role string `json:"role"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(c *gin.Context) {
|
||||
var req createUserReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
user, err := h.svc.Create(req.Username, req.Password, req.Nickname, req.Role, req.ParentID)
|
||||
if err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, user)
|
||||
}
|
||||
|
||||
type userStatusReq struct {
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
func (h *UserHandler) UpdateStatus(c *gin.Context) {
|
||||
id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
var req userStatusReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "参数错误")
|
||||
return
|
||||
}
|
||||
if err := h.svc.UpdateStatus(uint(id), req.Status); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
response.OK(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user