mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add comments count to Resource model and update logic for comment creation and deletion
This commit is contained in:
@@ -81,6 +81,7 @@ export interface ResourceDetails {
|
|||||||
author: User;
|
author: User;
|
||||||
views: number;
|
views: number;
|
||||||
downloads: number;
|
downloads: number;
|
||||||
|
comments: number;
|
||||||
related: Resource[];
|
related: Resource[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -245,6 +245,13 @@ export default function ResourcePage() {
|
|||||||
/>
|
/>
|
||||||
<MdOutlineComment className="text-xl mr-2" />
|
<MdOutlineComment className="text-xl mr-2" />
|
||||||
<span className="text-sm">{t("Comments")}</span>
|
<span className="text-sm">{t("Comments")}</span>
|
||||||
|
{resource.comments ? (
|
||||||
|
<span
|
||||||
|
className={`px-1 py-0.5 ml-1 rounded-full text-xs ${page === 2 ? "bg-accent text-accent-content" : "text-base-content/60"}`}
|
||||||
|
>
|
||||||
|
{resource.comments}
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
</label>
|
</label>
|
||||||
<div key={"comments"} className="tab-content p-2">
|
<div key={"comments"} className="tab-content p-2">
|
||||||
<Comments resourceId={resource.id} />
|
<Comments resourceId={resource.id} />
|
||||||
|
@@ -35,6 +35,12 @@ func CreateComment(content string, userID uint, resourceID uint, imageIDs []uint
|
|||||||
if err := tx.Model(&model.User{}).Where("id = ?", userID).Update("comments_count", gorm.Expr("comments_count + 1")).Error; err != nil {
|
if err := tx.Model(&model.User{}).Where("id = ?", userID).Update("comments_count", gorm.Expr("comments_count + 1")).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update resource comments count
|
||||||
|
if err := tx.Model(&model.Resource{}).Where("id = ?", resourceID).Update("comments", gorm.Expr("comments + 1")).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -166,6 +172,9 @@ func DeleteCommentByID(commentID uint) error {
|
|||||||
if err := tx.Model(&model.User{}).Where("id = ?", comment.UserID).Update("comments_count", gorm.Expr("comments_count - 1")).Error; err != nil {
|
if err := tx.Model(&model.User{}).Where("id = ?", comment.UserID).Update("comments_count", gorm.Expr("comments_count - 1")).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := tx.Model(&model.Resource{}).Where("id = ?", comment.RefID).Update("comments", gorm.Expr("comments - 1")).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := tx.
|
if err := tx.
|
||||||
Where("type = ? and ref_id = ?", model.ActivityTypeNewComment, commentID).
|
Where("type = ? and ref_id = ?", model.ActivityTypeNewComment, commentID).
|
||||||
Delete(&model.Activity{}).
|
Delete(&model.Activity{}).
|
||||||
|
@@ -19,6 +19,7 @@ type Resource struct {
|
|||||||
User User
|
User User
|
||||||
Views uint
|
Views uint
|
||||||
Downloads uint
|
Downloads uint
|
||||||
|
Comments uint
|
||||||
}
|
}
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
@@ -48,6 +49,7 @@ type ResourceDetailView struct {
|
|||||||
Author UserView `json:"author"`
|
Author UserView `json:"author"`
|
||||||
Views uint `json:"views"`
|
Views uint `json:"views"`
|
||||||
Downloads uint `json:"downloads"`
|
Downloads uint `json:"downloads"`
|
||||||
|
Comments uint `json:"comments"`
|
||||||
Related []ResourceView `json:"related"`
|
Related []ResourceView `json:"related"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,5 +102,6 @@ func (r *Resource) ToDetailView() ResourceDetailView {
|
|||||||
Author: r.User.ToView(),
|
Author: r.User.ToView(),
|
||||||
Views: r.Views,
|
Views: r.Views,
|
||||||
Downloads: r.Downloads,
|
Downloads: r.Downloads,
|
||||||
|
Comments: r.Comments,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user