-
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.
Merge pull request #17 from ShanghaitechGeekPie/prince213-reward
- Loading branch information
Showing
7 changed files
with
195 additions
and
26 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
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,57 @@ | ||
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" | ||
) | ||
|
||
type RecentCommentByPageResponse struct { | ||
PageCount int64 `json:"page_count"` | ||
HasMore bool `json:"has_more"` | ||
Comments []CommentResponse `json:"comments"` | ||
} | ||
|
||
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 | ||
var count int64 | ||
result := db.Preload("User").Preload("CourseGroup").Preload("CourseGroup.Course").Preload("CourseGroup.Teachers").Count(&count). | ||
Order("update_time DESC").Offset((id - 1) * 30).Limit(30).Find(&comments) | ||
if err := result.Error; err != nil { | ||
return errors.Wrap(err, errors.DatabaseError) | ||
} | ||
|
||
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: RecentCommentByPageResponse{ | ||
PageCount: (count + 29) / 30, // ceil | ||
HasMore: count > int64(id)*30, | ||
Comments: response, | ||
}, | ||
Error: false, | ||
}) | ||
} |
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,71 @@ | ||
package reward | ||
|
||
import ( | ||
"coursebench-backend/internal/middlewares/session" | ||
"coursebench-backend/pkg/database" | ||
"coursebench-backend/pkg/errors" | ||
"coursebench-backend/pkg/models" | ||
"coursebench-backend/pkg/queries" | ||
"strconv" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type SetCommentRequest struct { | ||
Reward int `json:"reward"` | ||
} | ||
|
||
func SetComment(c *fiber.Ctx) error { | ||
c.Accepts("application/json") | ||
var request SetCommentRequest | ||
if err := c.BodyParser(&request); err != nil { | ||
return errors.Wrap(err, errors.InvalidArgument) | ||
} | ||
|
||
idRaw := c.Params("id", "GG") | ||
id, err := strconv.Atoi(idRaw) | ||
if err != nil { | ||
return errors.New(errors.InvalidArgument) | ||
} | ||
|
||
uid, err := session.GetUserID(c) | ||
if err != nil { | ||
uid = 0 | ||
} | ||
|
||
db := database.GetDB() | ||
user, err := queries.GetUserByID(db, uid) | ||
if err != nil { | ||
return errors.Wrap(err, errors.DatabaseError) | ||
} | ||
if !(user.IsCommunityAdmin || user.IsAdmin) { | ||
return errors.New(errors.PermissionDenied) | ||
} | ||
|
||
comment := &models.Comment{} | ||
result := db.First(&comment, id) | ||
if result.Error != nil { | ||
return errors.Wrap(result.Error, errors.DatabaseError) | ||
} | ||
|
||
comment.User.Reward -= comment.Reward | ||
comment.Reward = request.Reward | ||
comment.User.Reward += comment.Reward | ||
|
||
if err := db.Transaction(func(tx *gorm.DB) error { | ||
if err := tx.Save(comment).Error; err != nil { | ||
return err | ||
} | ||
if err := tx.Save(user).Error; err != nil { | ||
return err | ||
} | ||
return nil | ||
}); err != nil { | ||
return errors.Wrap(err, errors.DatabaseError) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(models.OKResponse{ | ||
Error: false, | ||
}) | ||
} |
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