41f3060e00
- Introduced BrandLibrarySummary type to encapsulate brand library limits and usage. - Updated Brand interface to include keyword_count and question_count fields. - Implemented brand library limits in the backend, including max brands, keywords, and questions per keyword. - Added API endpoint to retrieve brand library summary. - Enhanced BrandsView.vue to display brand library usage and limits. - Implemented computed properties to manage brand, keyword, and question limits in the UI. - Updated mutation handlers to invalidate relevant queries upon creating/updating brands, keywords, and questions. - Added visual indicators for brand, keyword, and question limits in the UI. - Enhanced error handling for exceeding brand and keyword limits during creation.
264 lines
7.3 KiB
Go
264 lines
7.3 KiB
Go
package transport
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
"github.com/geo-platform/tenant-api/internal/tenant/app"
|
|
)
|
|
|
|
type BrandHandler struct {
|
|
svc *app.BrandService
|
|
}
|
|
|
|
func NewBrandHandler(a *bootstrap.App) *BrandHandler {
|
|
return &BrandHandler{svc: app.NewBrandService(a.DB, a.MonitoringDB, a.AuditLogs, a.Config.BrandLibrary).WithCache(a.Cache)}
|
|
}
|
|
|
|
func (h *BrandHandler) List(c *gin.Context) {
|
|
data, err := h.svc.List(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Summary(c *gin.Context) {
|
|
data, err := h.svc.Summary(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Create(c *gin.Context) {
|
|
var req app.BrandRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.Create(c.Request.Context(), req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Detail(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
data, err := h.svc.Detail(c.Request.Context(), id)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) Update(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
var req app.BrandRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.Update(c.Request.Context(), id, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Keywords
|
|
func (h *BrandHandler) ListKeywords(c *gin.Context) {
|
|
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
data, err := h.svc.ListKeywords(c.Request.Context(), brandID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) CreateKeyword(c *gin.Context) {
|
|
brandID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "brand id must be a number"))
|
|
return
|
|
}
|
|
var req app.KeywordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateKeyword(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateKeyword(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
keywordID, _ := strconv.ParseInt(c.Param("kid"), 10, 64)
|
|
var req app.KeywordRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateKeyword(c.Request.Context(), brandID, keywordID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteKeyword(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
keywordID, _ := strconv.ParseInt(c.Param("kid"), 10, 64)
|
|
if err := h.svc.DeleteKeyword(c.Request.Context(), brandID, keywordID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Questions
|
|
func (h *BrandHandler) ListQuestions(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var keywordID *int64
|
|
if v := c.Query("keyword_id"); v != "" {
|
|
if id, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
keywordID = &id
|
|
}
|
|
}
|
|
data, err := h.svc.ListQuestions(c.Request.Context(), brandID, keywordID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) CreateQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req app.QuestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateQuestion(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
questionID, _ := strconv.ParseInt(c.Param("qid"), 10, 64)
|
|
var req app.UpdateQuestionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateQuestion(c.Request.Context(), brandID, questionID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteQuestion(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
questionID, _ := strconv.ParseInt(c.Param("qid"), 10, 64)
|
|
if err := h.svc.DeleteQuestion(c.Request.Context(), brandID, questionID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Competitors
|
|
func (h *BrandHandler) ListCompetitors(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
data, err := h.svc.ListCompetitors(c.Request.Context(), brandID)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, data)
|
|
}
|
|
|
|
func (h *BrandHandler) CreateCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req app.CompetitorRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
data, err := h.svc.CreateCompetitor(c.Request.Context(), brandID, req)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.SuccessWithStatus(c, 201, data)
|
|
}
|
|
|
|
func (h *BrandHandler) UpdateCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
competitorID, _ := strconv.ParseInt(c.Param("cid"), 10, 64)
|
|
var req app.CompetitorRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.UpdateCompetitor(c.Request.Context(), brandID, competitorID, req); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *BrandHandler) DeleteCompetitor(c *gin.Context) {
|
|
brandID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
competitorID, _ := strconv.ParseInt(c.Param("cid"), 10, 64)
|
|
if err := h.svc.DeleteCompetitor(c.Request.Context(), brandID, competitorID); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|