Skip to content

Commit 9080ba9

Browse files
authored
Merge pull request #14 from ShanghaitechGeekPie/tyy
Implement personal reward display
2 parents 1d322bb + 61d4123 commit 9080ba9

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

internal/controllers/comments/course_group.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"coursebench-backend/pkg/database"
77
"coursebench-backend/pkg/errors"
88
"coursebench-backend/pkg/models"
9-
"github.com/gofiber/fiber/v2"
109
"strconv"
10+
11+
"github.com/gofiber/fiber/v2"
1112
)
1213

1314
func CourseGroupComment(c *fiber.Ctx) (err error) {

internal/controllers/comments/user.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"coursebench-backend/pkg/errors"
88
"coursebench-backend/pkg/models"
99
"coursebench-backend/pkg/queries"
10-
"github.com/gofiber/fiber/v2"
1110
"strconv"
11+
12+
"github.com/gofiber/fiber/v2"
1213
)
1314

1415
type CommentResponse struct {
@@ -43,6 +44,7 @@ type CommentResponse struct {
4344
CoverTitle string `json:"cover_title"`
4445
CoverContent string `json:"cover_content"`
4546
CoverReason string `json:"cover_reason"`
47+
Reward int `json:"reward"`
4648
}
4749

4850
type CommentLikeResult struct {
@@ -109,8 +111,16 @@ func GenerateResponse(comments []models.Comment, uid uint, likeResult []CommentL
109111
CoverTitle: v.CoverTitle,
110112
CoverContent: v.CoverContent,
111113
CoverReason: v.CoverReason,
114+
Reward: v.Reward,
112115
}
113116
// 该评论未设置匿名,或者是自己的评论,则显示用户信息
117+
currentUser, err := queries.GetUserByID(nil, uid)
118+
if err != nil {
119+
c.Reward = -2
120+
}
121+
if !currentUser.IsAdmin && !currentUser.IsCommunityAdmin {
122+
c.Reward = -1
123+
}
114124
if !anonymous || v.User.ID == uid {
115125
t, _ := queries.GetProfile(nil, v.UserID, uid)
116126
c.User = &t

internal/controllers/users/profile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"coursebench-backend/pkg/errors"
66
"coursebench-backend/pkg/models"
77
"coursebench-backend/pkg/queries"
8-
"github.com/gofiber/fiber/v2"
98
"strconv"
9+
10+
"github.com/gofiber/fiber/v2"
1011
)
1112

1213
func Profile(c *fiber.Ctx) error {

pkg/models/comment.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import (
44
"coursebench-backend/pkg/modelRegister"
5+
56
"github.com/lib/pq"
67
"gorm.io/gorm"
78
)
@@ -30,6 +31,7 @@ type Comment struct {
3031
CoverTitle string
3132
CoverContent string
3233
CoverReason string
34+
Reward int
3335
}
3436

3537
type CommentLike struct {

pkg/models/user.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ type ProfileResponse struct {
4848
IsAdmin bool `json:"is_admin"`
4949
IsCommunityAdmin bool `json:"is_community_admin"`
5050
InvitationCode string `json:"invitation_code"`
51+
Reward int `json:"reward"`
5152
}

pkg/queries/user.go

+5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ func GetProfile(db *gorm.DB, id uint, uid uint) (models.ProfileResponse, error)
350350
if id == uid {
351351
r.InvitationCode = user.InvitationCode
352352
}
353+
if id == uid || user.IsAdmin || user.IsCommunityAdmin {
354+
r.Reward = user.Reward
355+
} else {
356+
r.Reward = -1
357+
}
353358
return r, nil
354359
}
355360
}

0 commit comments

Comments
 (0)