-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controllers/comments.RecentCommentByPage): init
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package comments | ||
|
||
import ( | ||
"coursebench-backend/internal/middlewares/session" | ||
"coursebench-backend/internal/utils" | ||
"coursebench-backend/pkg/database" | ||
"coursebench-backend/pkg/errors" | ||
"coursebench-backend/pkg/models" | ||
"strconv" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func RecentCommentByPage(c *fiber.Ctx) (err error) { | ||
uid, err := session.GetUserID(c) | ||
if err != nil { | ||
uid = 0 | ||
} | ||
|
||
id_s := c.Params("id", "1") | ||
id, err := strconv.Atoi(id_s) | ||
if err != nil { | ||
return errors.New(errors.InvalidArgument) | ||
} | ||
|
||
db := database.GetDB() | ||
var comments []models.Comment | ||
result := db.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers"). | ||
Order("update_time DESC").Offset((id - 1) * 30).Limit(31).Find(&comments) | ||
if err := result.Error; err != nil { | ||
return errors.Wrap(err, errors.DatabaseError) | ||
} | ||
if result.RowsAffected == 31 { | ||
comments = comments[:30] | ||
} | ||
|
||
var likeResult []CommentLikeResult | ||
if uid != 0 { | ||
db.Raw("SELECT comment_likes.comment_id, comment_likes.is_like from comments, comment_likes where comment_likes.user_id = ? and comment_likes.comment_id = comments.id and comment_likes.deleted_at is NULL and comments.deleted_at is NULL order by create_time desc OFFSET ? LIMIT 30", | ||
(id-1)*30, | ||
uid).Scan(&likeResult) | ||
} | ||
var response []CommentResponse | ||
response = GenerateResponse(comments, uid, likeResult, true, utils.GetIP(c)) | ||
return c.Status(fiber.StatusOK).JSON(models.OKResponse{ | ||
Data: fiber.Map{ | ||
"has_more": result.RowsAffected == 31, | ||
"comments": response, | ||
}, | ||
Error: false, | ||
}) | ||
} |