Skip to content

Commit

Permalink
fix(admin):修改超级管理员可以直接修改问卷内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Penryn committed Oct 15, 2024
1 parent e87c9b5 commit ebd887f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 86 deletions.
Binary file added .DS_Store
Binary file not shown.
122 changes: 38 additions & 84 deletions internal/handler/admin/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ func CreateSurvey(c *gin.Context) {
}
questionMap := make(map[string]bool)
for _, question := range data.Questions {
if question.Subject == "" {
if question.Subject == "" {
c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "标题为空"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
if questionMap[question.Subject] {
c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"题目"+question.Subject+"重复"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "题目" + question.Subject + "重复"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyContentRepeat)
return
}
questionMap[question.Subject] = true
if question.QuestionType == 1 || question.QuestionType == 2 {
if len(question.Options) < 1 {
c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"选项数量太少"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "选项数量太少"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
optionMap := make(map[string]bool)
for _, option := range question.Options {
if option.Content == "" {
c.Error(&gin.Error{Err: errors.New("选项"+strconv.Itoa(option.SerialNum)+"内容为空"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("选项" + strconv.Itoa(option.SerialNum) + "内容为空"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
if optionMap[option.Content] {
c.Error(&gin.Error{Err: errors.New("选项内容"+option.Content+"重复"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("选项内容" + option.Content + "重复"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyContentRepeat)
return
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func UpdateSurveyStatus(c *gin.Context) {
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
Expand Down Expand Up @@ -179,12 +179,12 @@ func UpdateSurveyStatus(c *gin.Context) {
questionMap := make(map[string]bool)
for _, question := range questions {
if question.Subject == "" {
c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.SerialNum)+"内容填写为空"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.SerialNum) + "内容填写为空"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
if questionMap[question.Subject] {
c.Error(&gin.Error{Err: errors.New("问题题目"+question.Subject+"重复"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("问题题目" + question.Subject + "重复"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyContentRepeat)
return
}
Expand All @@ -197,19 +197,19 @@ func UpdateSurveyStatus(c *gin.Context) {
return
}
if len(options) < 1 {
c.Error(&gin.Error{Err: errors.New("问题"+strconv.Itoa(question.ID)+"选项太少"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("问题" + strconv.Itoa(question.ID) + "选项太少"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
optionMap := make(map[string]bool)
for _, option := range options {
if option.Content == "" {
c.Error(&gin.Error{Err: errors.New("选项"+strconv.Itoa(option.SerialNum)+"内容未填"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("选项" + strconv.Itoa(option.SerialNum) + "内容未填"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyIncomplete)
return
}
if optionMap[option.Content] {
c.Error(&gin.Error{Err: errors.New("选项内容"+option.Content+"重复"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New("选项内容" + option.Content + "重复"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyContentRepeat)
return
}
Expand Down Expand Up @@ -261,20 +261,28 @@ func UpdateSurvey(c *gin.Context) {
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
//判断问卷状态
if survey.Status != 1 {
c.Error(&gin.Error{Err: errors.New("问卷状态不为未发布"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.StatusRepeatError)
return
}
// 判断问卷的填写数量是否为零
if survey.Num != 0 {
c.Error(&gin.Error{Err: errors.New("问卷已有填写数量"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyNumError)
if user.AdminType == 2 {
// 超管可以直接修改问卷
} else if user.AdminType == 1 && survey.UserID == user.ID {
//判断问卷状态
if survey.Status != 1 {
c.Error(&gin.Error{Err: errors.New("问卷状态不为未发布"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.StatusRepeatError)
return
}
// 判断问卷的填写数量是否为零
if survey.Num != 0 {
c.Error(&gin.Error{Err: errors.New("问卷已有填写数量"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.SurveyNumError)
return
}
} else {
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
//解析时间转换为中国时间(UTC+8)
Expand Down Expand Up @@ -310,59 +318,6 @@ func UpdateSurvey(c *gin.Context) {
utils.JsonSuccessResponse(c, nil)
}

type UpdateSurveyPartData struct {
ID int `json:"id" binding:"required"`
Title string `json:"title"`
Desc string `json:"desc" `
Img string `json:"img" `
Time string `json:"time"`
}

func UpdateSurveyPart(c *gin.Context) {
var data UpdateSurveyPartData
err := c.ShouldBindJSON(&data)
if err != nil {
c.Error(&gin.Error{Err: errors.New("获取参数失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.ParamError)
return
}
//鉴权
user, err := service.GetUserSession(c)
if err != nil {
c.Error(&gin.Error{Err: errors.New("获取用户缓存信息失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NotLogin)
return
}
// 获取问卷
survey, err := service.GetSurveyByID(data.ID)
if err != nil {
c.Error(&gin.Error{Err: errors.New("获取问卷信息失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.ServerError)
return
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
//解析时间转换为中国时间(UTC+8)
ddlTime, err := time.Parse(time.RFC3339, data.Time)
if err != nil {
c.Error(&gin.Error{Err: errors.New("时间解析失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.ServerError)
return
}
//修改问卷
err = service.UpdateSurveyPart(data.ID, data.Title, data.Desc, data.Img, ddlTime)
if err != nil {
c.Error(&gin.Error{Err: errors.New("修改问卷失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.ServerError)
return
}
utils.JsonSuccessResponse(c, nil)
}

// 删除问卷
type DeleteSurveyData struct {
ID int `form:"id" binding:"required"`
Expand Down Expand Up @@ -396,7 +351,7 @@ func DeleteSurvey(c *gin.Context) {
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
Expand All @@ -414,7 +369,7 @@ func DeleteSurvey(c *gin.Context) {
type GetSurveyAnswersData struct {
ID int `form:"id" binding:"required"`
Text string `form:"text"`
Unique bool `form:"unique"`
Unique bool `form:"unique"`
PageNum int `form:"page_num" binding:"required"`
PageSize int `form:"page_size" binding:"required"`
}
Expand Down Expand Up @@ -447,13 +402,13 @@ func GetSurveyAnswers(c *gin.Context) {
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
//获取问卷收集数据
var num *int64
answers, num, err := service.GetSurveyAnswers(data.ID, data.PageNum, data.PageSize, data.Text,data.Unique)
answers, num, err := service.GetSurveyAnswers(data.ID, data.PageNum, data.PageSize, data.Text, data.Unique)
if err != nil {
c.Error(&gin.Error{Err: errors.New("获取问卷收集数据失败原因: " + err.Error()), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.ServerError)
Expand Down Expand Up @@ -568,7 +523,7 @@ func GetSurvey(c *gin.Context) {
}
//判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
Expand Down Expand Up @@ -652,7 +607,7 @@ func DownloadFile(c *gin.Context) {
}
// 判断权限
if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
Expand Down Expand Up @@ -715,7 +670,7 @@ func GetSurveyStatistics(c *gin.Context) {
}

if (user.AdminType != 2) && (user.AdminType != 1 || survey.UserID != user.ID) && !service.UserInManage(user.ID, survey.ID) {
c.Error(&gin.Error{Err: errors.New(user.Username+"无权限"), Type: gin.ErrorTypeAny})
c.Error(&gin.Error{Err: errors.New(user.Username + "无权限"), Type: gin.ErrorTypeAny})
utils.JsonErrorResponse(c, code.NoPermission)
return
}
Expand Down Expand Up @@ -835,7 +790,7 @@ func GetSurveyStatistics(c *gin.Context) {
if start > end {
start = end
}

// 按序号排序
sort.Slice(response, func(i, j int) bool {
return response[i].SerialNum < response[j].SerialNum
Expand All @@ -845,7 +800,6 @@ func GetSurveyStatistics(c *gin.Context) {
resp := response[start:end]
totalSumPage := math.Ceil(float64(len(response)) / float64(data.PageSize))


utils.JsonSuccessResponse(c, gin.H{
"statistics": resp,
"total": len(answersheets),
Expand Down
3 changes: 1 addition & 2 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Init(r *gin.Engine) {
{
api.POST("/admin/reg", a.Register)
api.POST("/admin/login", a.Login)
user:= api.Group("/user")
user := api.Group("/user")
{
user.POST("/submit", u.SubmitSurvey)
user.GET("/get", u.GetSurvey)
Expand All @@ -30,7 +30,6 @@ func Init(r *gin.Engine) {
admin.POST("/create", a.CreateSurvey)
admin.PUT("/update/status", a.UpdateSurveyStatus)
admin.PUT("/update/questions", a.UpdateSurvey)
admin.PUT("/update/part", a.UpdateSurveyPart)
admin.GET("/list/answers", a.GetSurveyAnswers)
admin.GET("/statics/answers", a.GetSurveyStatistics)
admin.DELETE("/delete", a.DeleteSurvey)
Expand Down

0 comments on commit ebd887f

Please sign in to comment.