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

修复一些bug以及添加物资借用问答板块 #57

Closed
wants to merge 2 commits into from
Closed
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
170 changes: 170 additions & 0 deletions app/controllers/funcControllers/suppliesBorrowController/QA.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package suppliesBorrowController

import (
"time"
"wejh-go/app/apiException"
"wejh-go/app/models"
"wejh-go/app/services/sessionServices"
"wejh-go/app/services/suppliesBorrowService"
"wejh-go/app/utils"

"github.com/gin-gonic/gin"
)

type CreateQAData struct {
Title string `json:"title" binding:"required"`
Content string `json:"content" binding:"required"`
}

// 创建 QA
func CreateQA(c *gin.Context) {
var data CreateQAData
err := c.ShouldBindJSON(&data)
if err != nil {
_ = c.AbortWithError(200, apiException.ParamError)
return
}

publisher := suppliesBorrowService.GetIdentity(c)
err = suppliesBorrowService.CreateQA(models.QA{
Title: data.Title,
Content: data.Content,
PublishTime: time.Now(),
Publisher: *publisher,
})

if err != nil {
_ = c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, nil)
}

type GetQAListData struct {
Publisher string `form:"publisher" binding:"required"`
}

// 用户获取对应发布方的 QA
func GetQAList(c *gin.Context) {
var data GetQAListData
err := c.ShouldBindQuery(&data)
if err != nil {
c.AbortWithError(200, apiException.ParamError)
return
}

_, err = sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}

var QAList []models.QA
QAList, err = suppliesBorrowService.GetQAListByPublisher(data.Publisher)
if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, QAList)
}

// 管理员获取自己发布的 QA
func GetQAListByAdmin(c *gin.Context) {
admin := suppliesBorrowService.GetIdentity(c)

var QAList []models.QA
var err error
if *admin == "Admin" {
QAList, err = suppliesBorrowService.GetQAListBySuperAdmin()
} else {
QAList, err = suppliesBorrowService.GetQAListByPublisher(*admin)
}

if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, QAList)
}

type UpdateQAData struct {
ID int `json:"id" binding:"required"`
Title string `json:"title" binding:"required"`
Content string `json:"content" binding:"required"`
}

// 更新 QA
func UpdateQA(c *gin.Context) {
var data UpdateQAData
err := c.ShouldBindJSON(&data)
if err != nil {
c.AbortWithError(200, apiException.ParamError)
return
}

pubisher := suppliesBorrowService.GetIdentity(c)

var QA *models.QA
QA, err = suppliesBorrowService.GetQAbyID(data.ID)
if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

if QA.Publisher != *pubisher && *pubisher != "Admin" {
c.AbortWithError(200, apiException.ServerError)
return
}

err = suppliesBorrowService.UpdateQA(models.QA{
ID: data.ID,
Title: data.Title,
Content: data.Content,
PublishTime: time.Now(),
})
if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

utils.JsonSuccessResponse(c, nil)
}

type DeleteQAData struct {
ID int `form:"id" binding:"required"`
}

// 删除 QA
func DeleteQA(c *gin.Context) {
var data DeleteQAData
err := c.ShouldBindQuery(&data)
if err != nil {
c.AbortWithError(200, apiException.ParamError)
return
}

pubisher := suppliesBorrowService.GetIdentity(c)

var QA *models.QA
QA, err = suppliesBorrowService.GetQAbyID(data.ID)
if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

if QA.Publisher != *pubisher && *pubisher != "Admin" {
c.AbortWithError(200, apiException.ServerError)
return
}

err = suppliesBorrowService.DeleteQA(data.ID)
if err != nil {
c.AbortWithError(200, apiException.ServerError)
return
}

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

import (
"wejh-go/app/apiException"
"wejh-go/app/models"
"wejh-go/app/services/sessionServices"

"github.com/gin-gonic/gin"
)

func CheckSuppliesBorrowAdmin(c *gin.Context) {
user, err := sessionServices.GetUserSession(c)
if err != nil {
_ = c.AbortWithError(200, apiException.NotLogin)
return
}
if user.Type != models.StudentAffairsCenter && user.Type != models.Admin {
_ = c.AbortWithError(200, apiException.NotAdmin)
return
}
c.Next()
}
11 changes: 11 additions & 0 deletions app/models/QA.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package models

import "time"

type QA struct {
ID int `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
PublishTime time.Time `json:"publish_time" gorm:"type:timestamp;"`
Publisher string `json:"publisher"`
}
78 changes: 78 additions & 0 deletions app/services/suppliesBorrowService/QA.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package suppliesBorrowService

import (
"wejh-go/app/apiException"
"wejh-go/app/models"
"wejh-go/app/services/sessionServices"
"wejh-go/config/database"

"github.com/gin-gonic/gin"
)

// 获取身份
func GetIdentity(c *gin.Context) *string {
user, err := sessionServices.GetUserSession(c)
if err != nil {
c.AbortWithError(200, apiException.NotLogin)
return nil
}

var identity string
if user.Type == models.StudentAffairsCenter {
identity = "学生事务大厅"
} else if user.Type == models.Admin {
identity = "Admin"
} else {
c.AbortWithError(200, apiException.ServerError)
return nil
}

return &identity
}

func CreateQA(QA models.QA) error {
result := database.DB.Create(&QA)
return result.Error
}

// 根据发布组织获取 QA
func GetQAListByPublisher(publisher string) ([]models.QA, error) {
var QAList []models.QA
result := database.DB.Where("publisher = ?", publisher).Find(&QAList)
if result.Error != nil {
return nil, result.Error
}
return QAList, nil
}

// 获取所有问答
func GetQAListBySuperAdmin() ([]models.QA, error) {
var QAList []models.QA
result := database.DB.Order("publisher").Find(&QAList)
if result.Error != nil {
return nil, result.Error
}
return QAList, nil
}

// 通过 id 获取 QA
func GetQAbyID(id int) (*models.QA, error) {
var QA models.QA
result := database.DB.Where("id = ?", id).First(&QA)
if result.Error != nil {
return nil, result.Error
}
return &QA, nil
}

// 更新 QA
func UpdateQA(QA models.QA) error {
result := database.DB.Model(&QA).Updates(QA)
return result.Error
}

// 删除 QA
func DeleteQA(id int) error {
result := database.DB.Delete(models.QA{ID: id})
return result.Error
}
7 changes: 5 additions & 2 deletions config/database/migrations.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package database

import (
"gorm.io/gorm"
"wejh-go/app/models"

"gorm.io/gorm"
)

func autoMigrate(db *gorm.DB) error {
Expand All @@ -18,5 +19,7 @@ func autoMigrate(db *gorm.DB) error {
&models.LostAndFoundRecord{},
&models.LostKind{},
&models.Notice{},
&models.LowBatteryQueryRecord{})
&models.LowBatteryQueryRecord{},
&models.QA{},
)
}
18 changes: 17 additions & 1 deletion config/router/adminRouter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package router

import (
"github.com/gin-gonic/gin"
"wejh-go/app/controllers/adminController"
"wejh-go/app/controllers/funcControllers/lostAndFoundRecordController"
"wejh-go/app/controllers/funcControllers/noticeController"
"wejh-go/app/controllers/funcControllers/suppliesBorrowController"
"wejh-go/app/midwares"

"github.com/gin-gonic/gin"
)

// 注册杂项路由
Expand Down Expand Up @@ -62,4 +64,18 @@ func adminRouterInit(r *gin.RouterGroup) {
notice.PUT("", noticeController.UpdateNotice)
}
}

stuAC := r.Group("/stuac", midwares.CheckSuppliesBorrowAdmin)
{
supplies := stuAC.Group("/supplies")
{
qa := supplies.Group("/qa")
{
qa.GET("", suppliesBorrowController.GetQAListByAdmin)
qa.POST("", suppliesBorrowController.CreateQA)
qa.PUT("", suppliesBorrowController.UpdateQA)
qa.DELETE("", suppliesBorrowController.DeleteQA)
}
}
}
}
10 changes: 9 additions & 1 deletion config/router/funcRouter.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package router

import (
"github.com/gin-gonic/gin"
"wejh-go/app/controllers/funcControllers/canteenController"
"wejh-go/app/controllers/funcControllers/customizeHomeController"
"wejh-go/app/controllers/funcControllers/lessonController"
"wejh-go/app/controllers/funcControllers/libraryController"
"wejh-go/app/controllers/funcControllers/lostAndFoundRecordController"
"wejh-go/app/controllers/funcControllers/noticeController"
"wejh-go/app/controllers/funcControllers/schoolBusController"
"wejh-go/app/controllers/funcControllers/suppliesBorrowController"
"wejh-go/app/controllers/funcControllers/zfController"
"wejh-go/app/controllers/yxyController/electricityController"
"wejh-go/app/controllers/yxyController/schoolCardController"
"wejh-go/app/midwares"

"github.com/gin-gonic/gin"
)

func funcRouterInit(r *gin.RouterGroup) {
Expand Down Expand Up @@ -77,9 +79,15 @@ func funcRouterInit(r *gin.RouterGroup) {
lost.GET("", lostAndFoundRecordController.GetRecords)
lost.GET("/kind_list", lostAndFoundRecordController.GetKindList)
}

notice := fun.Group("/information", midwares.CheckLogin)
{
notice.GET("", noticeController.GetNotice)
}

supplies := fun.Group("/supplies", midwares.CheckLogin)
{
supplies.GET("/qa", suppliesBorrowController.GetQAList)
}
}
}
2 changes: 1 addition & 1 deletion config/session/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func getRedisConfig() redisConfig {
Info.DB = config.Config.GetInt("redis.db")
}
if config.Config.IsSet("redis.password") {
Info.DB = config.Config.GetInt("redis.password")
Info.Password = config.Config.GetString("redis.password")
}
return Info
}
2 changes: 1 addition & 1 deletion config/session/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ type redisConfig struct {

func setRedis(r *gin.Engine, name string) {
Info := getRedisConfig()
store, _ := sessionRedis.NewStore(10, "tcp", Info.Host+":"+Info.Port, "", []byte("secret"))
store, _ := sessionRedis.NewStore(10, "tcp", Info.Host+":"+Info.Port, Info.Password, []byte("secret"))
r.Use(sessions.Sessions(name, store))
}