All checks were successful
Build and Release / Create Release (push) Successful in 0s
Build and Release / Unit Tests (push) Successful in 3m17s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 4m49s
Build and Release / Lint (push) Successful in 4m57s
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Successful in 3m0s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Successful in 9h4m54s
Build and Release / Build Binaries (amd64, darwin, macos) (push) Successful in 7m7s
Build and Release / Build Binaries (arm64, darwin, macos) (push) Successful in 7m59s
Build and Release / Build Binary (linux/arm64) (push) Successful in 8m35s
Adds revive:disable-line comments for exported types without doc comments. Fixes column alignment in migration struct. Adds explanatory nolint comments for intentional nil returns. Sorts imports alphabetically.
127 lines
3.4 KiB
Go
127 lines
3.4 KiB
Go
// Copyright 2026 MarketAlly. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package wishlist
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.gitcaddy.com/server/v3/models/db"
|
|
user_model "code.gitcaddy.com/server/v3/models/user"
|
|
"code.gitcaddy.com/server/v3/modules/timeutil"
|
|
)
|
|
|
|
// WishlistComment represents a comment on a wishlist item.
|
|
type WishlistComment struct { //revive:disable-line:exported
|
|
ID int64 `xorm:"pk autoincr"`
|
|
ItemID int64 `xorm:"INDEX NOT NULL"`
|
|
UserID int64 `xorm:"INDEX NOT NULL"`
|
|
Content string `xorm:"LONGTEXT NOT NULL"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
|
|
User *user_model.User `xorm:"-"`
|
|
}
|
|
|
|
func init() {
|
|
db.RegisterModel(new(WishlistComment))
|
|
}
|
|
|
|
// LoadUser loads the comment author.
|
|
func (c *WishlistComment) LoadUser(ctx context.Context) error {
|
|
if c.User != nil {
|
|
return nil
|
|
}
|
|
u, err := user_model.GetUserByID(ctx, c.UserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.User = u
|
|
return nil
|
|
}
|
|
|
|
// GetCommentsByItemID returns all comments for a wishlist item, with users loaded.
|
|
func GetCommentsByItemID(ctx context.Context, itemID int64) ([]*WishlistComment, error) {
|
|
comments := make([]*WishlistComment, 0, 20)
|
|
err := db.GetEngine(ctx).Where("item_id = ?", itemID).
|
|
OrderBy("created_unix ASC").
|
|
Find(&comments)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, c := range comments {
|
|
_ = c.LoadUser(ctx)
|
|
}
|
|
return comments, nil
|
|
}
|
|
|
|
// GetCommentByID returns a single comment by ID.
|
|
func GetCommentByID(ctx context.Context, id int64) (*WishlistComment, error) {
|
|
c := &WishlistComment{}
|
|
has, err := db.GetEngine(ctx).ID(id).Get(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !has {
|
|
return nil, fmt.Errorf("wishlist comment %d not found", id)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// CreateComment inserts a new comment.
|
|
func CreateComment(ctx context.Context, c *WishlistComment) error {
|
|
_, err := db.GetEngine(ctx).Insert(c)
|
|
return err
|
|
}
|
|
|
|
// DeleteComment removes a comment and its reactions.
|
|
func DeleteComment(ctx context.Context, id int64) error {
|
|
if err := DeleteCommentReactionsByCommentID(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
_, err := db.GetEngine(ctx).ID(id).Delete(new(WishlistComment))
|
|
return err
|
|
}
|
|
|
|
// CountCommentsByItemID returns the number of comments on an item.
|
|
func CountCommentsByItemID(ctx context.Context, itemID int64) (int64, error) {
|
|
return db.GetEngine(ctx).Where("item_id = ?", itemID).Count(new(WishlistComment))
|
|
}
|
|
|
|
// CountCommentsByItemIDBatch returns comment counts for multiple items.
|
|
func CountCommentsByItemIDBatch(ctx context.Context, itemIDs []int64) (map[int64]int64, error) {
|
|
result := make(map[int64]int64, len(itemIDs))
|
|
if len(itemIDs) == 0 {
|
|
return result, nil
|
|
}
|
|
|
|
type countRow struct {
|
|
ItemID int64 `xorm:"item_id"`
|
|
Cnt int64 `xorm:"cnt"`
|
|
}
|
|
var rows []countRow
|
|
err := db.GetEngine(ctx).Table("wishlist_comment").
|
|
Select("item_id, COUNT(*) AS cnt").
|
|
In("item_id", itemIDs).
|
|
GroupBy("item_id").
|
|
Find(&rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, r := range rows {
|
|
result[r.ItemID] = r.Cnt
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// DeleteCommentsByItemID removes all comments for an item.
|
|
func DeleteCommentsByItemID(ctx context.Context, itemID int64) error {
|
|
// Delete reactions first
|
|
if err := DeleteCommentReactionsByItemID(ctx, itemID); err != nil {
|
|
return err
|
|
}
|
|
_, err := db.GetEngine(ctx).Where("item_id = ?", itemID).Delete(new(WishlistComment))
|
|
return err
|
|
}
|