Skip to content

Commit

Permalink
Merge branch 'master' into license-update
Browse files Browse the repository at this point in the history
  • Loading branch information
WAAutoMaton authored May 12, 2024
2 parents df3ced1 + e911aeb commit 40097c5
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 42 deletions.
41 changes: 29 additions & 12 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
name: Docker Image CI
on:
push:
branches: [ "master" ]
branches: [ "**" ]

jobs:
build:
runs-on: ubuntu-22.04
permissions:
packages: write
contents: read
env:
tag: ${{ secrets.CI_REGISTRY }}/geekpie/coursebench-backend:latest
REGISTRY: ghcr.io
USERNAME: ShanghaitechGeekPie
IMAGE_NAME : ${{ github.repository }}
steps:
- uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
- name: Checkout repository
uses: actions/checkout@v3

- name: Login to Container registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.USERNAME }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
registry: ${{ secrets.CI_REGISTRY }}
username: ${{ secrets.CI_REGISTRY_USER }}
password: ${{ secrets.CI_REGISTRY_PASSWORD }}
- name: Build the Docker image
run: docker build . --file Dockerfile --tag $tag
- name: Push the Docker image1
run: docker push $tag
context: .
push: ${{github.ref == 'refs/heads/master' || github.ref == 'refs/heads/release'}}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 2 additions & 0 deletions internal/controllers/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controllers

import (
"coursebench-backend/internal/controllers/comments"

"github.com/gofiber/fiber/v2"
)

Expand All @@ -30,6 +31,7 @@ func CommentRoutes(r fiber.Router) {
route.Get("/course_group/:id", comments.CourseGroupComment)
route.Get("/course/:id", comments.CourseComment)
route.Get("/recent", comments.RecentComment)
route.Get("/recent/:id", comments.RecentCommentByPage)
route.Post("/like", comments.Like)
route.Post("/fold", comments.Fold)
route.Post("/cover", comments.Cover)
Expand Down
3 changes: 2 additions & 1 deletion internal/controllers/comments/course_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
"coursebench-backend/pkg/database"
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"github.com/gofiber/fiber/v2"
"strconv"

"github.com/gofiber/fiber/v2"
)

func CourseGroupComment(c *fiber.Ctx) (err error) {
Expand Down
27 changes: 26 additions & 1 deletion internal/controllers/comments/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"
"time"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
"time"
)

type PostRequest struct {
Expand Down Expand Up @@ -133,6 +134,30 @@ func Post(c *fiber.Ctx) (err error) {
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

// If this is the first time for the poster to post a comment, the reward the inviter.
var user models.User
err = tx.First(&user, uid).Error
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

if user.HasPostedComments {
return nil
}
user.HasPostedComments = true
tx.Save(user)

if user.InvitedByUserID == 0 {
return nil
}
inviter, err := queries.GetUserByID(tx, user.InvitedByUserID)
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}
inviter.Reward += 100
tx.Save(inviter)

return nil
})
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions internal/controllers/comments/recent_by_page.go
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,
})
}
6 changes: 4 additions & 2 deletions internal/controllers/comments/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"
"github.com/gofiber/fiber/v2"
"strconv"

"github.com/gofiber/fiber/v2"
)

type CommentResponse struct {
Expand Down Expand Up @@ -59,6 +60,7 @@ type CommentResponse struct {
CoverTitle string `json:"cover_title"`
CoverContent string `json:"cover_content"`
CoverReason string `json:"cover_reason"`
Reward int `json:"reward"`
}

type CommentLikeResult struct {
Expand Down Expand Up @@ -125,8 +127,8 @@ func GenerateResponse(comments []models.Comment, uid uint, likeResult []CommentL
CoverTitle: v.CoverTitle,
CoverContent: v.CoverContent,
CoverReason: v.CoverReason,
Reward: v.Reward,
}
// 该评论未设置匿名,或者是自己的评论,则显示用户信息
if !anonymous || v.User.ID == uid {
t, _ := queries.GetProfile(nil, v.UserID, uid)
c.User = &t
Expand Down
13 changes: 13 additions & 0 deletions internal/controllers/reward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package controllers

import (
"coursebench-backend/internal/controllers/reward"

"github.com/gofiber/fiber/v2"
)

func RewardRoutes(r fiber.Router) {
route := r.Group("/reward")
route.Get("/ranklist", reward.Ranklist)
route.Post("/set/:id", reward.SetComment)
}
20 changes: 20 additions & 0 deletions internal/controllers/reward/ranklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reward

import (
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"

"github.com/gofiber/fiber/v2"
)

func Ranklist(c *fiber.Ctx) error {
var response []models.RanklistResponse
response, err := queries.Ranklist(nil)
if err != nil {
return err
}
return c.Status(fiber.StatusOK).JSON(models.OKResponse{
Data: response,
Error: false,
})
}
71 changes: 71 additions & 0 deletions internal/controllers/reward/setcomment.go
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,
})
}
3 changes: 2 additions & 1 deletion internal/controllers/users/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"
"github.com/gofiber/fiber/v2"
"strconv"

"github.com/gofiber/fiber/v2"
)

func Profile(c *fiber.Ctx) error {
Expand Down
16 changes: 9 additions & 7 deletions internal/controllers/users/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ import (
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"

"github.com/gofiber/fiber/v2"
)

type RegisterRequest struct {
Email string `json:"email"`
Password string `json:"password"`
Year int `json:"year"`
Grade models.GradeType `json:"grade"`
Captcha string `json:"captcha"`
Nickname string `json:"nickname"`
Email string `json:"email"`
Password string `json:"password"`
Year int `json:"year"`
Grade models.GradeType `json:"grade"`
Captcha string `json:"captcha"`
Nickname string `json:"nickname"`
InvitationCode string `json:"invitation_code"`
}

func Register(c *fiber.Ctx) (err error) {
Expand All @@ -54,7 +56,7 @@ func Register(c *fiber.Ctx) (err error) {
Avatar: "",
IsAnonymous: false,
}
if err = queries.Register(nil, &user); err != nil {
if err = queries.Register(nil, &user, userReq.InvitationCode); err != nil {
return
}
if config.GlobalConf.DisableMail {
Expand Down
3 changes: 2 additions & 1 deletion internal/fiber/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fiber
import (
"coursebench-backend/internal/config"
"coursebench-backend/internal/controllers"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
Expand All @@ -33,5 +34,5 @@ func Routes(app *fiber.App) {
controllers.CourseRoutes(route)
controllers.CommentRoutes(route)
controllers.TeacherRoute(route)

controllers.RewardRoutes(route)
}
1 change: 1 addition & 0 deletions pkg/errors/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var (
CaptchaMismatch = createDescription("CaptchaMismatch", "验证码错误", SILENT, 400)
NoCaptchaToken = createDescription("NoCaptchaToken", "未请求过验证码Token,请检查您的 Cookie 设置", SILENT, 400)
CaptchaExpired = createDescription("CaptchaExpired", "验证码已过期", SILENT, 400)
InvitationCodeInvalid = createDescription("InvitationCodeInvalid", "邀请码无效", SILENT, 400)

TeacherNotExists = createDescription("TeacherNotExists", "未找到教师", SILENT, 400)

Expand Down
2 changes: 2 additions & 0 deletions pkg/models/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package models

import (
"coursebench-backend/pkg/modelRegister"

"github.com/lib/pq"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -46,6 +47,7 @@ type Comment struct {
CoverTitle string
CoverContent string
CoverReason string
Reward int
}

type CommentLike struct {
Expand Down
Loading

0 comments on commit 40097c5

Please sign in to comment.