Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增常用网站相关接口 #9

Merged
merged 7 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/controllers/activityController/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func CreateActivity(c *gin.Context) {
Campus: data.Campus,
Location: data.Location,
Img: data.Img,
AuthorID: c.GetUint("user_id"),
PublishTime: time.Now(),
AuthorID: utils.GetUser(c).ID,
})
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/activityController/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func DeleteActivity(c *gin.Context) {
return
}

user := c.GetUint("user_id")
adminType := c.GetUint("admin_type")
if activity.AuthorID != user && adminType != models.SuperAdmin {
user := utils.GetUser(c)
if activity.AuthorID != user.ID && user.Type != models.SuperAdmin {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}
Expand Down
9 changes: 2 additions & 7 deletions app/controllers/activityController/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"4u-go/app/apiException"
"4u-go/app/services/activityService"
"4u-go/app/services/sessionService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -41,11 +40,7 @@ func GetActivityList(c *gin.Context) {
return
}

user, err := sessionService.GetUserSession(c)
if err != nil {
apiException.AbortWithException(c, apiException.NotLogin, err)
return
}
user := utils.GetUser(c)

list, err := activityService.GetActivityList(data.Campus)
if err != nil {
Expand All @@ -62,7 +57,7 @@ func GetActivityList(c *gin.Context) {
Department: activity.Department,
StartTime: activity.StartTime.Format(time.RFC3339),
EndTime: activity.EndTime.Format(time.RFC3339),
PublishTime: activity.PublishTime.Format(time.RFC3339),
PublishTime: activity.CreatedAt.Format(time.RFC3339),
Campus: activity.Campus,
Location: activity.Location,
Img: activity.Img,
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/activityController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ func UpdateActivity(c *gin.Context) {
return
}

user := c.GetUint("user_id")
adminType := c.GetUint("admin_type")
if activity.AuthorID != user && adminType != models.SuperAdmin {
user := utils.GetUser(c)
if activity.AuthorID != user.ID && user.Type != models.SuperAdmin {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}
Expand Down
9 changes: 3 additions & 6 deletions app/controllers/announcementController/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package announcementController

import (
"time"

"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/announcementService"
Expand All @@ -25,10 +23,9 @@ func CreateAnnouncement(c *gin.Context) {
}

err = announcementService.SaveAnnouncement(models.Announcement{
Title: data.Title,
Content: data.Content,
PublishTime: time.Now(),
AuthorID: c.GetUint("user_id"),
Title: data.Title,
Content: data.Content,
AuthorID: utils.GetUser(c).ID,
})
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/announcementController/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func DeleteAnnouncement(c *gin.Context) {
return
}

user := c.GetUint("user_id")
adminType := c.GetUint("admin_type")
if announcement.AuthorID != user && adminType != models.SuperAdmin {
user := utils.GetUser(c)
if announcement.AuthorID != user.ID && user.Type != models.SuperAdmin {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}
Expand Down
9 changes: 2 additions & 7 deletions app/controllers/announcementController/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"4u-go/app/apiException"
"4u-go/app/services/announcementService"
"4u-go/app/services/sessionService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)
Expand All @@ -24,11 +23,7 @@ type announcementElement struct {

// GetAnnouncementList 获取公告列表
func GetAnnouncementList(c *gin.Context) {
user, err := sessionService.GetUserSession(c)
if err != nil {
apiException.AbortWithException(c, apiException.NotLogin, err)
return
}
user := utils.GetUser(c)

list, err := announcementService.GetAnnouncementList()
if err != nil {
Expand All @@ -42,7 +37,7 @@ func GetAnnouncementList(c *gin.Context) {
ID: announcement.ID,
Title: announcement.Title,
Content: announcement.Content,
PublishTime: announcement.PublishTime.Format(time.RFC3339),
PublishTime: announcement.CreatedAt.Format(time.RFC3339),
Editable: announcement.AuthorID == user.ID || user.Type == 4,
})
}
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/announcementController/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ func UpdateAnnouncement(c *gin.Context) {
return
}

user := c.GetUint("user_id")
adminType := c.GetUint("admin_type")
if announcement.AuthorID != user && adminType != models.SuperAdmin {
user := utils.GetUser(c)
if announcement.AuthorID != user.ID && user.Type != models.SuperAdmin {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/collegeController/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ func GetCollegeList(c *gin.Context) {
return
}

utils.JsonSuccessResponse(c, getCollegeResponse{collegeList})
utils.JsonSuccessResponse(c, getCollegeResponse{
CollegeList: collegeList,
})
}
6 changes: 3 additions & 3 deletions app/controllers/userController/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func AuthByPassword(c *gin.Context) {
"studentID": user.StudentID,
"userType": user.Type,
"phoneNum": user.PhoneNum,
"createTime": user.CreateTime,
"createTime": user.CreatedAt,
},
})
}
Expand All @@ -79,7 +79,7 @@ func AuthBySession(c *gin.Context) {
"studentID": user.StudentID,
"userType": user.Type,
"phoneNum": user.PhoneNum,
"createTime": user.CreateTime,
"createTime": user.CreatedAt,
},
})
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func WeChatLogin(c *gin.Context) {
"studentID": user.StudentID,
"userType": user.Type,
"phoneNum": user.PhoneNum,
"createTime": user.CreateTime,
"createTime": user.CreatedAt,
},
})
}
46 changes: 46 additions & 0 deletions app/controllers/websiteController/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package websiteController

import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/websiteService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)

type createWebsiteData struct {
Title string `json:"title" binding:"required"`
Type uint `json:"type" binding:"required"`
College uint `json:"college"`
Description string `json:"description" binding:"required"`
Condition string `json:"condition" binding:"required"`
URL string `json:"url" binding:"required"`
}

// CreateWebsite 新建一个网站
func CreateWebsite(c *gin.Context) {
var data createWebsiteData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

// TODO: 限制学院管理员只能发自己学院的网站

err = websiteService.SaveWebsite(models.Website{
Title: data.Title,
Type: data.Type,
College: data.College,
Description: data.Description,
Condition: data.Condition,
URL: data.URL,
AuthorID: utils.GetUser(c).ID,
})
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, nil)
}
51 changes: 51 additions & 0 deletions app/controllers/websiteController/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package websiteController

import (
"errors"

"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/websiteService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type deleteWebsiteData struct {
ID uint `json:"id" binding:"required"`
}

// DeleteWebsite 删除一个网站
func DeleteWebsite(c *gin.Context) {
var data deleteWebsiteData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

// 判断网站是否存在
website, err := websiteService.GetWebsiteById(data.ID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
apiException.AbortWithException(c, apiException.ResourceNotFound, err)
} else {
apiException.AbortWithException(c, apiException.ServerError, err)
}
return
}

user := utils.GetUser(c)
if website.AuthorID != user.ID && user.Type != models.SuperAdmin {
apiException.AbortWithException(c, apiException.NotPermission, nil)
return
}

err = websiteService.DeleteWebsiteById(data.ID)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

utils.JsonSuccessResponse(c, nil)
}
88 changes: 88 additions & 0 deletions app/controllers/websiteController/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package websiteController

import (
"4u-go/app/apiException"
"4u-go/app/models"
"4u-go/app/services/websiteService"
"4u-go/app/utils"
"github.com/gin-gonic/gin"
)

type getWebsiteData struct {
Type uint `json:"type" binding:"required"`
College uint `json:"college"`
}

type getWebsiteResponse struct {
WebsiteList []websiteElement `json:"website_list"`
}

type getEditableWebsiteResponse struct {
WebsiteList []models.Website `json:"website_list"`
}

type websiteElement struct {
ID uint `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Condition string `json:"condition"`
Editable bool `json:"editable"`
}

// GetWebsiteList 获取网站列表
func GetWebsiteList(c *gin.Context) {
var data getWebsiteData
err := c.ShouldBindJSON(&data)
if err != nil {
apiException.AbortWithException(c, apiException.ParamError, err)
return
}

list, err := websiteService.GetWebsiteList(data.Type, data.College)
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}

websiteList := make([]websiteElement, 0)
for _, website := range list {
websiteList = append(websiteList, websiteElement{
ID: website.ID,
Title: website.Title,
Description: website.Description,
URL: website.URL,
Condition: website.Condition,
})
}

utils.JsonSuccessResponse(c, getWebsiteResponse{
WebsiteList: websiteList,
})
}

// GetEditableWebsites 获取可管理的网站列表
func GetEditableWebsites(c *gin.Context) {
list, err := websiteService.GetAllWebsites()
if err != nil {
apiException.AbortWithException(c, apiException.ServerError, err)
return
}
websiteList := list

user := utils.GetUser(c)
if user.Type != models.SuperAdmin {
editableList := make([]models.Website, 0)
for _, website := range list {
// TODO: 根据管理员对应学院进行筛选
if website.AuthorID == user.ID {
editableList = append(editableList, website)
}
}
websiteList = editableList
}

utils.JsonSuccessResponse(c, getEditableWebsiteResponse{
WebsiteList: websiteList,
})
}
Loading