feat: align ids to int64

This commit is contained in:
2026-01-08 09:57:04 +08:00
parent a1de16bc01
commit d98f41f1ac
39 changed files with 298 additions and 339 deletions

View File

@@ -11,7 +11,6 @@ import (
"quyun/v2/database/models"
"quyun/v2/pkg/consts"
"github.com/spf13/cast"
"gorm.io/gorm"
)
@@ -30,9 +29,8 @@ func (s *content) List(ctx context.Context, filter *content_dto.ContentListFilte
if filter.Genre != nil && *filter.Genre != "" {
q = q.Where(tbl.Genre.Eq(*filter.Genre))
}
if filter.TenantID != nil && *filter.TenantID != "" {
tid := cast.ToInt64(*filter.TenantID)
q = q.Where(tbl.TenantID.Eq(tid))
if filter.TenantID != nil && *filter.TenantID > 0 {
q = q.Where(tbl.TenantID.Eq(*filter.TenantID))
}
if filter.IsPinned != nil {
q = q.Where(tbl.IsPinned.Is(*filter.IsPinned))
@@ -129,12 +127,11 @@ func (s *content) List(ctx context.Context, filter *content_dto.ContentListFilte
}, nil
}
func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dto.ContentDetail, error) {
cid := cast.ToInt64(id)
func (s *content) Get(ctx context.Context, userID int64, id int64) (*content_dto.ContentDetail, error) {
// Increment Views
_, _ = models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.ID.Eq(cid)).
Where(models.ContentQuery.ID.Eq(id)).
UpdateSimple(models.ContentQuery.Views.Add(1))
_, q := models.ContentQuery.QueryContext(ctx)
@@ -146,7 +143,7 @@ func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dt
return db.Order("sort ASC")
}).
Preload("ContentAssets.Asset").
Where("id = ?", cid).
Where("id = ?", id).
First(&item).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -157,7 +154,7 @@ func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dt
// Fetch Price
var price float64
cp, err := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(cid)).First()
cp, err := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(id)).First()
if err == nil {
price = float64(cp.PriceAmount) / 100.0
}
@@ -172,12 +169,12 @@ func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dt
// Interaction
isLiked, _ = models.UserContentActionQuery.WithContext(ctx).
Where(models.UserContentActionQuery.UserID.Eq(uid),
models.UserContentActionQuery.ContentID.Eq(cid),
models.UserContentActionQuery.ContentID.Eq(id),
models.UserContentActionQuery.Type.Eq("like")).
Exists()
isFavorited, _ = models.UserContentActionQuery.WithContext(ctx).
Where(models.UserContentActionQuery.UserID.Eq(uid),
models.UserContentActionQuery.ContentID.Eq(cid),
models.UserContentActionQuery.ContentID.Eq(id),
models.UserContentActionQuery.Type.Eq("favorite")).
Exists()
@@ -188,7 +185,7 @@ func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dt
// Check Purchase
exists, _ := models.ContentAccessQuery.WithContext(ctx).
Where(models.ContentAccessQuery.UserID.Eq(uid),
models.ContentAccessQuery.ContentID.Eq(cid),
models.ContentAccessQuery.ContentID.Eq(id),
models.ContentAccessQuery.Status.Eq(consts.ContentAccessStatusActive)).
Exists()
if exists {
@@ -235,11 +232,10 @@ func (s *content) Get(ctx context.Context, userID int64, id string) (*content_dt
return detail, nil
}
func (s *content) ListComments(ctx context.Context, userID int64, id string, page int) (*requests.Pager, error) {
cid := cast.ToInt64(id)
func (s *content) ListComments(ctx context.Context, userID int64, id int64, page int) (*requests.Pager, error) {
tbl, q := models.CommentQuery.QueryContext(ctx)
q = q.Where(tbl.ContentID.Eq(cid)).Preload(tbl.User)
q = q.Where(tbl.ContentID.Eq(id)).Preload(tbl.User)
q = q.Order(tbl.CreatedAt.Desc())
p := requests.Pagination{Page: int64(page), Limit: 10}
@@ -274,14 +270,14 @@ func (s *content) ListComments(ctx context.Context, userID int64, id string, pag
data := make([]content_dto.Comment, len(list))
for i, v := range list {
data[i] = content_dto.Comment{
ID: cast.ToString(v.ID),
ID: v.ID,
Content: v.Content,
UserID: cast.ToString(v.UserID),
UserID: v.UserID,
UserNickname: v.User.Nickname,
UserAvatar: v.User.Avatar,
CreateTime: v.CreatedAt.Format("2006-01-02 15:04:05"),
Likes: int(v.Likes),
ReplyTo: cast.ToString(v.ReplyTo),
ReplyTo: v.ReplyTo,
IsLiked: likedMap[v.ID],
}
}
@@ -296,16 +292,15 @@ func (s *content) ListComments(ctx context.Context, userID int64, id string, pag
func (s *content) CreateComment(
ctx context.Context,
userID int64,
id string,
id int64,
form *content_dto.CommentCreateForm,
) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := userID
cid := cast.ToInt64(id)
c, err := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(cid)).First()
c, err := models.ContentQuery.WithContext(ctx).Where(models.ContentQuery.ID.Eq(id)).First()
if err != nil {
return errorx.ErrRecordNotFound
}
@@ -313,9 +308,9 @@ func (s *content) CreateComment(
comment := &models.Comment{
TenantID: c.TenantID,
UserID: uid,
ContentID: cid,
ContentID: id,
Content: form.Content,
ReplyTo: cast.ToInt64(form.ReplyTo),
ReplyTo: form.ReplyTo,
}
if err := models.CommentQuery.WithContext(ctx).Create(comment); err != nil {
@@ -324,33 +319,32 @@ func (s *content) CreateComment(
return nil
}
func (s *content) LikeComment(ctx context.Context, userID int64, id string) error {
func (s *content) LikeComment(ctx context.Context, userID int64, id int64) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := userID
cmid := cast.ToInt64(id)
// Fetch comment for author
cm, err := models.CommentQuery.WithContext(ctx).Where(models.CommentQuery.ID.Eq(cmid)).First()
cm, err := models.CommentQuery.WithContext(ctx).Where(models.CommentQuery.ID.Eq(id)).First()
if err != nil {
return errorx.ErrRecordNotFound
}
err = models.Q.Transaction(func(tx *models.Query) error {
exists, _ := tx.UserCommentAction.WithContext(ctx).
Where(tx.UserCommentAction.UserID.Eq(uid), tx.UserCommentAction.CommentID.Eq(cmid), tx.UserCommentAction.Type.Eq("like")).
Where(tx.UserCommentAction.UserID.Eq(uid), tx.UserCommentAction.CommentID.Eq(id), tx.UserCommentAction.Type.Eq("like")).
Exists()
if exists {
return nil
}
action := &models.UserCommentAction{UserID: uid, CommentID: cmid, Type: "like"}
action := &models.UserCommentAction{UserID: uid, CommentID: id, Type: "like"}
if err := tx.UserCommentAction.WithContext(ctx).Create(action); err != nil {
return err
}
_, err := tx.Comment.WithContext(ctx).Where(tx.Comment.ID.Eq(cmid)).UpdateSimple(tx.Comment.Likes.Add(1))
_, err := tx.Comment.WithContext(ctx).Where(tx.Comment.ID.Eq(id)).UpdateSimple(tx.Comment.Likes.Add(1))
return err
})
if err != nil {
@@ -408,11 +402,11 @@ func (s *content) GetFavorites(ctx context.Context, userID int64) ([]user_dto.Co
return s.getInteractList(ctx, userID, "favorite")
}
func (s *content) AddFavorite(ctx context.Context, userID int64, contentId string) error {
func (s *content) AddFavorite(ctx context.Context, userID int64, contentId int64) error {
return s.addInteract(ctx, userID, contentId, "favorite")
}
func (s *content) RemoveFavorite(ctx context.Context, userID int64, contentId string) error {
func (s *content) RemoveFavorite(ctx context.Context, userID int64, contentId int64) error {
return s.removeInteract(ctx, userID, contentId, "favorite")
}
@@ -420,11 +414,11 @@ func (s *content) GetLikes(ctx context.Context, userID int64) ([]user_dto.Conten
return s.getInteractList(ctx, userID, "like")
}
func (s *content) AddLike(ctx context.Context, userID int64, contentId string) error {
func (s *content) AddLike(ctx context.Context, userID int64, contentId int64) error {
return s.addInteract(ctx, userID, contentId, "like")
}
func (s *content) RemoveLike(ctx context.Context, userID int64, contentId string) error {
func (s *content) RemoveLike(ctx context.Context, userID int64, contentId int64) error {
return s.removeInteract(ctx, userID, contentId, "like")
}
@@ -470,7 +464,7 @@ func (s *content) ListTopics(ctx context.Context) ([]content_dto.Topic, error) {
}
topics = append(topics, content_dto.Topic{
ID: cast.ToString(i + 1), // Use index as ID for aggregation results
ID: int64(i + 1), // Use index as ID for aggregation results
Title: r.Genre,
Tag: r.Genre,
Count: r.Count,
@@ -484,10 +478,10 @@ func (s *content) ListTopics(ctx context.Context) ([]content_dto.Topic, error) {
func (s *content) toContentItemDTO(item *models.Content, price float64, authorIsFollowing bool) content_dto.ContentItem {
dto := content_dto.ContentItem{
ID: cast.ToString(item.ID),
ID: item.ID,
Title: item.Title,
Genre: item.Genre,
AuthorID: cast.ToString(item.UserID),
AuthorID: item.UserID,
Views: int(item.Views),
Likes: int(item.Likes),
CreatedAt: item.CreatedAt.Format("2006-01-02"),
@@ -553,16 +547,15 @@ func (s *content) toMediaURLs(assets []*models.ContentAsset) []content_dto.Media
return urls
}
func (s *content) addInteract(ctx context.Context, userID int64, contentId, typ string) error {
func (s *content) addInteract(ctx context.Context, userID int64, contentId int64, typ string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := userID
cid := cast.ToInt64(contentId)
// Fetch content for author
c, err := models.ContentQuery.WithContext(ctx).
Where(models.ContentQuery.ID.Eq(cid)).
Where(models.ContentQuery.ID.Eq(contentId)).
Select(models.ContentQuery.UserID, models.ContentQuery.Title).
First()
if err != nil {
@@ -571,19 +564,19 @@ func (s *content) addInteract(ctx context.Context, userID int64, contentId, typ
err = models.Q.Transaction(func(tx *models.Query) error {
exists, _ := tx.UserContentAction.WithContext(ctx).
Where(tx.UserContentAction.UserID.Eq(uid), tx.UserContentAction.ContentID.Eq(cid), tx.UserContentAction.Type.Eq(typ)).
Where(tx.UserContentAction.UserID.Eq(uid), tx.UserContentAction.ContentID.Eq(contentId), tx.UserContentAction.Type.Eq(typ)).
Exists()
if exists {
return nil
}
action := &models.UserContentAction{UserID: uid, ContentID: cid, Type: typ}
action := &models.UserContentAction{UserID: uid, ContentID: contentId, Type: typ}
if err := tx.UserContentAction.WithContext(ctx).Create(action); err != nil {
return err
}
if typ == "like" {
_, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.Likes.Add(1))
_, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(contentId)).UpdateSimple(tx.Content.Likes.Add(1))
return err
}
return nil
@@ -605,16 +598,15 @@ func (s *content) addInteract(ctx context.Context, userID int64, contentId, typ
return nil
}
func (s *content) removeInteract(ctx context.Context, userID int64, contentId, typ string) error {
func (s *content) removeInteract(ctx context.Context, userID int64, contentId int64, typ string) error {
if userID == 0 {
return errorx.ErrUnauthorized
}
uid := userID
cid := cast.ToInt64(contentId)
return models.Q.Transaction(func(tx *models.Query) error {
res, err := tx.UserContentAction.WithContext(ctx).
Where(tx.UserContentAction.UserID.Eq(uid), tx.UserContentAction.ContentID.Eq(cid), tx.UserContentAction.Type.Eq(typ)).
Where(tx.UserContentAction.UserID.Eq(uid), tx.UserContentAction.ContentID.Eq(contentId), tx.UserContentAction.Type.Eq(typ)).
Delete()
if err != nil {
return err
@@ -624,7 +616,7 @@ func (s *content) removeInteract(ctx context.Context, userID int64, contentId, t
}
if typ == "like" {
_, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(cid)).UpdateSimple(tx.Content.Likes.Sub(1))
_, err := tx.Content.WithContext(ctx).Where(tx.Content.ID.Eq(contentId)).UpdateSimple(tx.Content.Likes.Sub(1))
return err
}
return nil