后台商品管理
This commit is contained in:
@@ -6,6 +6,8 @@ type ListingDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
AccountID uint64 `json:"account_id"`
|
||||
OwnerID uint64 `json:"owner_id"`
|
||||
OwnerPhone string `json:"owner_phone,omitempty"`
|
||||
OwnerNickname string `json:"owner_nickname,omitempty"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
GameName string `json:"game_name"`
|
||||
@@ -47,3 +49,10 @@ type UpdateRequest = CreateRequest
|
||||
type ReviewRequest struct {
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type AdminListQuery struct {
|
||||
OwnerID uint64
|
||||
Status string
|
||||
ReviewStatus string
|
||||
Limit int
|
||||
}
|
||||
|
||||
@@ -88,6 +88,19 @@ func (h *Handler) ListPendingReview(c *gin.Context) {
|
||||
response.OK(c, gin.H{"items": items})
|
||||
}
|
||||
|
||||
func (h *Handler) ListAdmin(c *gin.Context) {
|
||||
query, ok := parseAdminListQuery(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
items, err := h.service.ListAdmin(query)
|
||||
if err != nil {
|
||||
writeListingError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"items": items})
|
||||
}
|
||||
|
||||
func (h *Handler) Approve(c *gin.Context) {
|
||||
id, ok := parseID(c)
|
||||
if !ok {
|
||||
@@ -208,6 +221,29 @@ func parseID(c *gin.Context) (uint64, bool) {
|
||||
return id, true
|
||||
}
|
||||
|
||||
func parseAdminListQuery(c *gin.Context) (AdminListQuery, bool) {
|
||||
var query AdminListQuery
|
||||
if raw := c.Query("owner_id"); raw != "" {
|
||||
value, err := strconv.ParseUint(raw, 10, 64)
|
||||
if err != nil || value == 0 {
|
||||
response.BadRequest(c, "号主 ID 不正确")
|
||||
return query, false
|
||||
}
|
||||
query.OwnerID = value
|
||||
}
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil || value <= 0 {
|
||||
response.BadRequest(c, "查询条数不正确")
|
||||
return query, false
|
||||
}
|
||||
query.Limit = value
|
||||
}
|
||||
query.Status = c.Query("status")
|
||||
query.ReviewStatus = c.Query("review_status")
|
||||
return query, true
|
||||
}
|
||||
|
||||
func writeListingError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, ErrDependencyUnavailable):
|
||||
|
||||
@@ -137,6 +137,31 @@ func (r *Repository) ListPendingReview() ([]ListingDTO, error) {
|
||||
return rowsToDTO(rows), nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListAdmin(query AdminListQuery) ([]ListingDTO, error) {
|
||||
limit := query.Limit
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
}
|
||||
|
||||
db := r.baseQuery()
|
||||
if query.OwnerID > 0 {
|
||||
db = db.Where("l.owner_id = ?", query.OwnerID)
|
||||
}
|
||||
if query.Status != "" {
|
||||
db = db.Where("l.status = ?", query.Status)
|
||||
}
|
||||
if query.ReviewStatus != "" {
|
||||
db = db.Where("l.review_status = ?", query.ReviewStatus)
|
||||
}
|
||||
|
||||
var rows []listingRow
|
||||
err := db.Order("l.id DESC").Limit(limit).Scan(&rows).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rowsToDTO(rows), nil
|
||||
}
|
||||
|
||||
func (r *Repository) Approve(listingID uint64) (*ListingDTO, error) {
|
||||
var dto *ListingDTO
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -291,8 +316,10 @@ func (r *Repository) findDTO(where string, args ...any) (*ListingDTO, error) {
|
||||
|
||||
func (r *Repository) baseQuery() *gorm.DB {
|
||||
return r.db.Table("rental_listings AS l").
|
||||
Select("l.*, a.title, a.description, a.game_name, a.server_region, a.login_platform, a.rank_level, a.haf_coin_amount").
|
||||
Joins("JOIN game_accounts AS a ON a.id = l.account_id")
|
||||
Select(`l.*, a.title, a.description, a.game_name, a.server_region, a.login_platform, a.rank_level,
|
||||
a.haf_coin_amount, COALESCE(u.phone, '') AS owner_phone, COALESCE(u.nickname, '') AS owner_nickname`).
|
||||
Joins("JOIN game_accounts AS a ON a.id = l.account_id").
|
||||
Joins("LEFT JOIN users AS u ON u.id = l.owner_id")
|
||||
}
|
||||
|
||||
func (r *Repository) findForReviewUpdate(tx *gorm.DB, listingID uint64) (*model.RentalListing, *model.GameAccount, error) {
|
||||
@@ -310,6 +337,8 @@ func (r *Repository) findForReviewUpdate(tx *gorm.DB, listingID uint64) (*model.
|
||||
type listingRow struct {
|
||||
model.RentalListing
|
||||
Title string
|
||||
OwnerPhone string
|
||||
OwnerNickname string
|
||||
Description string
|
||||
GameName string
|
||||
ServerRegion string
|
||||
@@ -331,6 +360,8 @@ func (row listingRow) toDTO() ListingDTO {
|
||||
ID: row.ID,
|
||||
AccountID: row.AccountID,
|
||||
OwnerID: row.OwnerID,
|
||||
OwnerPhone: row.OwnerPhone,
|
||||
OwnerNickname: row.OwnerNickname,
|
||||
Title: row.Title,
|
||||
Description: row.Description,
|
||||
GameName: row.GameName,
|
||||
|
||||
@@ -52,6 +52,13 @@ func (s *Service) ListPendingReview() ([]ListingDTO, error) {
|
||||
return s.repo.ListPendingReview()
|
||||
}
|
||||
|
||||
func (s *Service) ListAdmin(query AdminListQuery) ([]ListingDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.ListAdmin(query)
|
||||
}
|
||||
|
||||
func (s *Service) Approve(id uint64) (*ListingDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
|
||||
Reference in New Issue
Block a user