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)
|
|
|
|
if can, err := ac.rankService.CheckRankPermission(ctx, req.UserID, rank.AnswerDeleteRank); err != nil || !can {
|
|
|
|
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
|
|
|
|
return
|
|
|
|
}
|
2022-11-22 16:54:28 +08:00
|
|
|
userinfo := middleware.GetUserInfoFromContext(ctx)
|
|
|
|
req.IsAdmin = userinfo.IsAdmin
|
2022-11-04 17:10:20 +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)
|
|
|
|
|
|
|
|
if can, err := ac.rankService.CheckRankPermission(ctx, req.UserID, rank.AnswerAddRank); err != nil || !can {
|
|
|
|
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
|
|
|
|
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-22 16:54:28 +08:00
|
|
|
userinfo := middleware.GetUserInfoFromContext(ctx)
|
|
|
|
req.IsAdmin = userinfo.IsAdmin
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
if can, err := ac.rankService.CheckRankPermission(ctx, req.UserID, rank.AnswerEditRank); err != nil || !can {
|
|
|
|
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ac.answerService.Update(ctx, req)
|
|
|
|
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
|
|
|
|
// @Param data body schema.AnswerList true "AnswerList"
|
|
|
|
// @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) {
|
|
|
|
req := &schema.AnswerList{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
2022-09-28 11:20:32 +08:00
|
|
|
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-11-22 17:35:41 +08:00
|
|
|
userinfo := middleware.GetUserInfoFromContext(ctx)
|
|
|
|
req.IsAdmin = userinfo.IsAdmin
|
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)
|
|
|
|
if can, err := ac.rankService.CheckRankPermission(ctx, req.UserID, rank.AnswerAcceptRank); err != nil || !can {
|
|
|
|
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ac.answerService.UpdateAdopted(ctx, req)
|
|
|
|
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{})
|
|
|
|
}
|