2022-09-27 17:59:05 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"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/entity"
|
|
|
|
"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"
|
2022-10-24 16:51:05 +08:00
|
|
|
"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)
|
2022-11-24 15:24:04 +08:00
|
|
|
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
|
2022-11-24 18:00:30 +08:00
|
|
|
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, rank.AnswerDeleteRank, req.ID)
|
|
|
|
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 15:24:04 +08:00
|
|
|
|
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")
|
2022-11-01 15:25:44 +08:00
|
|
|
userID := middleware.GetLoginUserIDFromContext(ctx)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-11-01 15:25:44 +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)
|
|
|
|
|
2022-11-24 18:00:30 +08:00
|
|
|
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, rank.AnswerAddRank, "")
|
|
|
|
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-01 15:25:44 +08:00
|
|
|
answerID, err := ac.answerService.Insert(ctx, req)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
2022-11-01 15:25:44 +08:00
|
|
|
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 {
|
2022-11-01 15:25:44 +08:00
|
|
|
// todo !has
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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 15:24:04 +08:00
|
|
|
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-11-24 18:00:30 +08:00
|
|
|
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
|
|
|
rank.AnswerEditRank,
|
|
|
|
rank.AnswerEditWithoutReviewRank,
|
|
|
|
}, "")
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !canList[0] {
|
|
|
|
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
|
|
|
req.NoNeedReview = canList[1]
|
2022-09-27 17:59:05 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
info, questionInfo, has, err := ac.answerService.Get(ctx, req.ID, req.UserID)
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !has {
|
2022-11-01 15:25:44 +08:00
|
|
|
// todo !has
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, nil, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler.HandleResponse(ctx, nil, gin.H{
|
|
|
|
"info": info,
|
|
|
|
"question": questionInfo,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnswerList godoc
|
|
|
|
// @Summary AnswerList
|
|
|
|
// @Description AnswerList <br> <b>order</b> (default or updated)
|
|
|
|
// @Tags api-answer
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2022-11-25 12:07:56 +08:00
|
|
|
// @Param data body schema.AnswerListReq true "AnswerListReq"
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Success 200 {string} string ""
|
2022-09-28 11:20:32 +08:00
|
|
|
// @Router /answer/api/v1/answer/list [get]
|
|
|
|
func (ac *AnswerController) AnswerList(ctx *gin.Context) {
|
2022-11-25 12:07:56 +08:00
|
|
|
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-11-25 12:07:56 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-11-24 15:24:04 +08:00
|
|
|
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
|
2022-11-25 12:07:56 +08:00
|
|
|
|
|
|
|
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
|
|
|
rank.AnswerEditRank,
|
|
|
|
rank.AnswerDeleteRank,
|
|
|
|
}, "")
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adopted godoc
|
|
|
|
// @Summary Adopted
|
|
|
|
// @Description Adopted
|
|
|
|
// @Tags api-answer
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.AnswerAdoptedReq true "AnswerAdoptedReq"
|
|
|
|
// @Success 200 {string} string ""
|
|
|
|
// @Router /answer/api/v1/answer/acceptance [post]
|
|
|
|
func (ac *AnswerController) Adopted(ctx *gin.Context) {
|
|
|
|
req := &schema.AnswerAdoptedReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-11-24 18:00:30 +08:00
|
|
|
can, err := ac.rankService.CheckOperationPermission(ctx, req.UserID, rank.AnswerAcceptRank, req.QuestionID)
|
|
|
|
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.UpdateAdopted(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
|
|
|
|
// @Param data body entity.AdminSetAnswerStatusRequest true "AdminSetAnswerStatusRequest"
|
|
|
|
// @Router /answer/admin/api/answer/status [put]
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
func (ac *AnswerController) AdminSetAnswerStatus(ctx *gin.Context) {
|
|
|
|
req := &entity.AdminSetAnswerStatusRequest{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := ac.answerService.AdminSetAnswerStatus(ctx, req.AnswerID, req.StatusStr)
|
|
|
|
handler.HandleResponse(ctx, err, gin.H{})
|
|
|
|
}
|