diff --git a/app/controllers/funcControllers/suppliesBorrowController/QA.go b/app/controllers/funcControllers/suppliesBorrowController/QA.go new file mode 100644 index 0000000..d44902f --- /dev/null +++ b/app/controllers/funcControllers/suppliesBorrowController/QA.go @@ -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) +} diff --git a/app/midwares/checkSuppliesBorrowAdmin.go b/app/midwares/checkSuppliesBorrowAdmin.go new file mode 100644 index 0000000..daad8d2 --- /dev/null +++ b/app/midwares/checkSuppliesBorrowAdmin.go @@ -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() +} diff --git a/app/models/QA.go b/app/models/QA.go new file mode 100644 index 0000000..dd11c04 --- /dev/null +++ b/app/models/QA.go @@ -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"` +} diff --git a/app/services/suppliesBorrowService/QA.go b/app/services/suppliesBorrowService/QA.go new file mode 100644 index 0000000..0171029 --- /dev/null +++ b/app/services/suppliesBorrowService/QA.go @@ -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 +} diff --git a/config/database/migrations.go b/config/database/migrations.go index d78adae..be7ef9c 100755 --- a/config/database/migrations.go +++ b/config/database/migrations.go @@ -1,8 +1,9 @@ package database import ( - "gorm.io/gorm" "wejh-go/app/models" + + "gorm.io/gorm" ) func autoMigrate(db *gorm.DB) error { @@ -18,5 +19,7 @@ func autoMigrate(db *gorm.DB) error { &models.LostAndFoundRecord{}, &models.LostKind{}, &models.Notice{}, - &models.LowBatteryQueryRecord{}) + &models.LowBatteryQueryRecord{}, + &models.QA{}, + ) } diff --git a/config/router/adminRouter.go b/config/router/adminRouter.go index 137a0f7..8e4bd74 100755 --- a/config/router/adminRouter.go +++ b/config/router/adminRouter.go @@ -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" ) // 注册杂项路由 @@ -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) + } + } + } } diff --git a/config/router/funcRouter.go b/config/router/funcRouter.go index 8978dc7..49cf7f9 100755 --- a/config/router/funcRouter.go +++ b/config/router/funcRouter.go @@ -1,7 +1,6 @@ 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" @@ -9,10 +8,13 @@ import ( "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) { @@ -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) + } } } diff --git a/config/session/config.go b/config/session/config.go index 585f968..5066e59 100644 --- a/config/session/config.go +++ b/config/session/config.go @@ -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 } diff --git a/config/session/redis.go b/config/session/redis.go index 8ea8730..034dad9 100644 --- a/config/session/redis.go +++ b/config/session/redis.go @@ -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)) }