You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.5 KiB
Go
146 lines
3.5 KiB
Go
package article
|
|
|
|
import (
|
|
"cls/internal/application/article"
|
|
middleware "cls/internal/infrastructure/middleware/auth"
|
|
"cls/internal/interfaces"
|
|
"cls/pkg/logger"
|
|
"cls/pkg/util/page"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type ArticleHandler struct {
|
|
service *article.ArticleService
|
|
authMiddleware *middleware.AuthMiddleware
|
|
log logger.Logger
|
|
}
|
|
|
|
var _ interfaces.Handler = (*ArticleHandler)(nil)
|
|
|
|
func NewArticleHandle(service *article.ArticleService, authMiddleware *middleware.AuthMiddleware, log logger.New) *ArticleHandler {
|
|
return &ArticleHandler{service, authMiddleware, log("cls:interfaces:articleHandler")}
|
|
}
|
|
|
|
func (a *ArticleHandler) RegisterRouters(router gin.IRouter) {
|
|
articleHandler := router.Group("/article")
|
|
{
|
|
articleHandler.GET("/all", a.findAll)
|
|
articleHandler.GET("/unlock", a.unlock)
|
|
articleHandler.GET("/free", a.free)
|
|
articleHandler.GET("/detail/:id", a.detail)
|
|
articleHandler.POST("/unlock-article", a.unlockArticle)
|
|
}
|
|
}
|
|
func (a *ArticleHandler) findAll(c *gin.Context) {
|
|
p := &page.Page{}
|
|
err := c.ShouldBindQuery(p)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
ePhone, err := a.authMiddleware.DecodeToken(c)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
}
|
|
|
|
err = a.service.Find(ePhone, p, map[string]string{
|
|
"search_like_class": c.Query("search_like_class")})
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusOK, p)
|
|
}
|
|
|
|
}
|
|
func (a *ArticleHandler) unlock(c *gin.Context) {
|
|
p := &page.Page{}
|
|
err := c.ShouldBindQuery(p)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
ePhone, err := a.authMiddleware.DecodeToken(c)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
err = a.service.FindUnLock(ePhone, p, map[string]string{
|
|
"search_eq_class": c.Query("search_eq_class")})
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusOK, p)
|
|
}
|
|
|
|
}
|
|
|
|
func (a *ArticleHandler) free(c *gin.Context) {
|
|
p := &page.Page{}
|
|
err := c.ShouldBindQuery(p)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
err = a.service.FindFree(p, map[string]string{
|
|
"search_like_class": c.Query("search_like_class")})
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusOK, p)
|
|
}
|
|
|
|
}
|
|
|
|
func (a *ArticleHandler) unlockArticle(c *gin.Context) {
|
|
dto := &article.ArticleDto{}
|
|
err := c.ShouldBindJSON(dto)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
fmt.Printf("%+v\n", dto)
|
|
ePhone, err := a.authMiddleware.DecodeToken(c)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data, err := a.service.UnLockArticle(ePhone, dto.EventId)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusOK, data)
|
|
}
|
|
}
|
|
|
|
func (a *ArticleHandler) detail(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
a.log.Error(err)
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
ePhone, err := a.authMiddleware.DecodeToken(c)
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
data, err := a.service.Detail(ePhone, uint64(id))
|
|
if err != nil {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
} else {
|
|
c.AbortWithStatusJSON(http.StatusOK, data)
|
|
}
|
|
|
|
}
|