feat(ops/site-mappings): add HTTP handlers for site domain mappings
Wires list/create/update/set-active/delete onto the SiteDomainMappingService through gin handlers.
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/ops/app"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/response"
|
||||
)
|
||||
|
||||
type siteDomainMappingRequest struct {
|
||||
RegistrableDomain string `json:"registrable_domain" binding:"required"`
|
||||
SiteKey string `json:"site_key"`
|
||||
SiteName string `json:"site_name" binding:"required"`
|
||||
IsActive *bool `json:"is_active"`
|
||||
}
|
||||
|
||||
type setSiteDomainMappingActiveRequest struct {
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
func listSiteDomainMappingsHandler(svc *app.SiteDomainMappingService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
||||
result, err := svc.List(c.Request.Context(), app.SiteDomainMappingListInput{
|
||||
Keyword: c.Query("keyword"),
|
||||
IsActive: parseOptionalBoolQuery(c.Query("is_active")),
|
||||
Page: page,
|
||||
Size: size,
|
||||
})
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, result)
|
||||
}
|
||||
}
|
||||
|
||||
func createSiteDomainMappingHandler(svc *app.SiteDomainMappingService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var body siteDomainMappingRequest
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40000, "invalid_payload", err.Error()))
|
||||
return
|
||||
}
|
||||
result, err := svc.Create(c.Request.Context(), actorFromGin(c), body.toInput())
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, result)
|
||||
}
|
||||
}
|
||||
|
||||
func updateSiteDomainMappingHandler(svc *app.SiteDomainMappingService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id, err := parseIDParam(c)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
var body siteDomainMappingRequest
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40000, "invalid_payload", err.Error()))
|
||||
return
|
||||
}
|
||||
result, err := svc.Update(c.Request.Context(), actorFromGin(c), id, body.toInput())
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, result)
|
||||
}
|
||||
}
|
||||
|
||||
func setSiteDomainMappingActiveHandler(svc *app.SiteDomainMappingService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id, err := parseIDParam(c)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
var body setSiteDomainMappingActiveRequest
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40000, "invalid_payload", err.Error()))
|
||||
return
|
||||
}
|
||||
result, err := svc.SetActive(c.Request.Context(), actorFromGin(c), id, body.IsActive)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, result)
|
||||
}
|
||||
}
|
||||
|
||||
func deleteSiteDomainMappingHandler(svc *app.SiteDomainMappingService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id, err := parseIDParam(c)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
if err := svc.Delete(c.Request.Context(), actorFromGin(c), id); err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, gin.H{"id": id})
|
||||
}
|
||||
}
|
||||
|
||||
func (r siteDomainMappingRequest) toInput() app.SiteDomainMappingInput {
|
||||
active := true
|
||||
if r.IsActive != nil {
|
||||
active = *r.IsActive
|
||||
}
|
||||
return app.SiteDomainMappingInput{
|
||||
RegistrableDomain: r.RegistrableDomain,
|
||||
SiteKey: r.SiteKey,
|
||||
SiteName: r.SiteName,
|
||||
IsActive: active,
|
||||
}
|
||||
}
|
||||
|
||||
func parseOptionalBoolQuery(value string) *bool {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
parsed, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &parsed
|
||||
}
|
||||
Reference in New Issue
Block a user