answer/internal/controller/answer_controller.go

305 lines
8.3 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package controller
import (
"fmt"
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service"
2022-11-02 15:50:32 +08:00
"github.com/answerdev/answer/internal/service/dashboard"
"github.com/answerdev/answer/internal/service/permission"
"github.com/answerdev/answer/internal/service/rank"
2022-09-27 17:59:05 +08:00
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
)
// AnswerController answer controller
type AnswerController struct {
2022-11-02 15:50:32 +08:00
answerService *service.AnswerService
rankService *rank.RankService
dashboardService *dashboard.DashboardService
2022-09-27 17:59:05 +08:00
}
// NewAnswerController new controller
2022-11-02 15:50:32 +08:00
func NewAnswerController(answerService *service.AnswerService,
rankService *rank.RankService,
dashboardService *dashboard.DashboardService,
) *AnswerController {
return &AnswerController{
answerService: answerService,
rankService: rankService,
dashboardService: dashboardService,
}
2022-09-27 17:59:05 +08:00
}
// RemoveAnswer delete answer
// @Summary delete answer
// @Description delete answer
// @Tags api-answer
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body schema.RemoveAnswerReq true "answer"
// @Success 200 {object} handler.RespBody
// @Router /answer/api/v1/answer [delete]
func (ac *AnswerController) RemoveAnswer(ctx *gin.Context) {
req := &schema.RemoveAnswerReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, permission.AnswerDelete, req.ID)
2022-11-24 18:00:30 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if !can {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
2022-09-27 17:59:05 +08:00
return
}
2022-11-24 18:00:30 +08:00
err = ac.answerService.RemoveAnswer(ctx, req)
2022-09-27 17:59:05 +08:00
handler.HandleResponse(ctx, err, nil)
}
// Get godoc
// @Summary Get Answer
// @Description Get Answer
// @Tags api-answer
// @Accept json
// @Produce json
// @Param id query string true "Answer TagID" default(1)
// @Router /answer/api/v1/answer/info [get]
// @Success 200 {string} string ""
func (ac *AnswerController) Get(ctx *gin.Context) {
id := ctx.Query("id")
userID := middleware.GetLoginUserIDFromContext(ctx)
2022-09-27 17:59:05 +08:00
info, questionInfo, has, err := ac.answerService.Get(ctx, id, userID)
2022-09-27 17:59:05 +08:00
if err != nil {
handler.HandleResponse(ctx, err, gin.H{})
return
}
if !has {
handler.HandleResponse(ctx, fmt.Errorf(""), gin.H{})
return
}
handler.HandleResponse(ctx, err, gin.H{
"info": info,
"question": questionInfo,
})
}
// Add godoc
// @Summary Insert Answer
// @Description Insert Answer
// @Tags api-answer
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body schema.AnswerAddReq true "AnswerAddReq"
// @Success 200 {string} string ""
// @Router /answer/api/v1/answer [post]
func (ac *AnswerController) Add(ctx *gin.Context) {
req := &schema.AnswerAddReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, permission.AnswerAdd, "")
2022-11-24 18:00:30 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if !can {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
2022-09-27 17:59:05 +08:00
return
}
answerID, err := ac.answerService.Insert(ctx, req)
2022-09-27 17:59:05 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
info, questionInfo, has, err := ac.answerService.Get(ctx, answerID, req.UserID)
2022-09-27 17:59:05 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if !has {
// todo !has
2022-09-27 17:59:05 +08:00
handler.HandleResponse(ctx, nil, nil)
return
}
2023-03-07 17:56:51 +08:00
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
permission.AnswerEdit,
permission.AnswerDelete,
})
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
objectOwner := ac.rankService.CheckOperationObjectOwner(ctx, req.UserID, info.ID)
req.CanEdit = canList[0] || objectOwner
req.CanDelete = canList[1] || objectOwner
if !can {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
return
}
info.MemberActions = permission.GetAnswerPermission(ctx, req.UserID, info.UserID, req.CanEdit, req.CanDelete)
2022-09-27 17:59:05 +08:00
handler.HandleResponse(ctx, nil, gin.H{
"info": info,
"question": questionInfo,
})
}
// Update godoc
// @Summary Update Answer
// @Description Update Answer
// @Tags api-answer
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param data body schema.AnswerUpdateReq true "AnswerUpdateReq"
// @Success 200 {string} string ""
// @Router /answer/api/v1/answer [put]
func (ac *AnswerController) Update(ctx *gin.Context) {
req := &schema.AnswerUpdateReq{}
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
2022-11-24 18:00:30 +08:00
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
permission.AnswerEdit,
permission.AnswerEditWithoutReview,
})
2022-11-24 18:00:30 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
objectOwner := ac.rankService.CheckOperationObjectOwner(ctx, req.UserID, req.ID)
req.CanEdit = canList[0] || objectOwner
req.NoNeedReview = canList[1] || objectOwner
if !req.CanEdit {
2022-11-24 18:00:30 +08:00
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
2022-09-27 17:59:05 +08:00
return
}
2022-11-24 18:00:30 +08:00
_, err = ac.answerService.Update(ctx, req)
2022-09-27 17:59:05 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
_, _, _, err = ac.answerService.Get(ctx, req.ID, req.UserID)
2022-09-27 17:59:05 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
handler.HandleResponse(ctx, nil, &schema.AnswerUpdateResp{WaitForReview: !req.NoNeedReview})
2022-09-27 17:59:05 +08:00
}
// AnswerList godoc
// @Summary AnswerList
// @Description AnswerList <br> <b>order</b> (default or updated)
// @Tags api-answer
// @Security ApiKeyAuth
// @Accept json
// @Produce json
2022-12-01 18:36:20 +08:00
// @Param question_id query string true "question_id"
// @Param order query string true "order"
// @Param page query string true "page"
// @Param page_size query string true "page_size"
2022-09-27 17:59:05 +08:00
// @Success 200 {string} string ""
2022-12-01 18:36:20 +08:00
// @Router /answer/api/v1/answer/page [get]
2022-09-28 11:20:32 +08:00
func (ac *AnswerController) AnswerList(ctx *gin.Context) {
req := &schema.AnswerListReq{}
2022-09-28 11:20:32 +08:00
if handler.BindAndCheck(ctx, req) {
2022-09-27 17:59:05 +08:00
return
}
2022-12-01 18:36:20 +08:00
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
permission.AnswerEdit,
permission.AnswerDelete,
})
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
req.CanEdit = canList[0]
req.CanDelete = canList[1]
2022-09-28 11:20:32 +08:00
list, count, err := ac.answerService.SearchList(ctx, req)
2022-09-27 17:59:05 +08:00
if err != nil {
2022-09-28 11:20:32 +08:00
handler.HandleResponse(ctx, err, nil)
2022-09-27 17:59:05 +08:00
return
}
2022-09-28 11:20:32 +08:00
handler.HandleResponse(ctx, nil, gin.H{
2022-09-27 17:59:05 +08:00
"list": list,
"count": count,
})
}
2022-12-22 11:04:19 +08:00
// Accepted godoc
// @Summary Accepted
// @Description Accepted
2022-09-27 17:59:05 +08:00
// @Tags api-answer
// @Accept json
// @Produce json
// @Security ApiKeyAuth
2022-12-22 11:04:19 +08:00
// @Param data body schema.AnswerAcceptedReq true "AnswerAcceptedReq"
2022-09-27 17:59:05 +08:00
// @Success 200 {string} string ""
// @Router /answer/api/v1/answer/acceptance [post]
2022-12-22 11:04:19 +08:00
func (ac *AnswerController) Accepted(ctx *gin.Context) {
req := &schema.AnswerAcceptedReq{}
2022-09-27 17:59:05 +08:00
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, permission.AnswerAccept, req.QuestionID)
2022-11-24 18:00:30 +08:00
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if !can {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
2022-09-27 17:59:05 +08:00
return
}
2022-12-22 11:04:19 +08:00
err = ac.answerService.UpdateAccepted(ctx, req)
2022-09-27 17:59:05 +08:00
handler.HandleResponse(ctx, err, nil)
}
// AdminSetAnswerStatus godoc
// @Summary AdminSetAnswerStatus
// @Description Status:[available,deleted]
// @Tags admin
// @Accept json
// @Produce json
// @Security ApiKeyAuth
2022-12-07 16:51:08 +08:00
// @Param data body schema.AdminSetAnswerStatusRequest true "AdminSetAnswerStatusRequest"
2022-09-27 17:59:05 +08:00
// @Router /answer/admin/api/answer/status [put]
// @Success 200 {object} handler.RespBody
func (ac *AnswerController) AdminSetAnswerStatus(ctx *gin.Context) {
req := &schema.AdminSetAnswerStatusRequest{}
2022-09-27 17:59:05 +08:00
if handler.BindAndCheck(ctx, req) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
err := ac.answerService.AdminSetAnswerStatus(ctx, req)
2022-09-27 17:59:05 +08:00
handler.HandleResponse(ctx, err, gin.H{})
}