chore: build
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
package contactlinks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
type contactLinkResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Label string `json:"label"`
|
||||
URL string `json:"url"`
|
||||
IconKey string `json:"icon_key"`
|
||||
IconMediaID *uuid.UUID `json:"icon_media_id"`
|
||||
Color string `json:"color"`
|
||||
Position int `json:"position"`
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
func toResponse(link *ContactLink) contactLinkResponse {
|
||||
return contactLinkResponse{
|
||||
ID: link.ID,
|
||||
Label: link.Label,
|
||||
URL: link.URL,
|
||||
IconKey: link.IconKey,
|
||||
IconMediaID: link.IconMediaID,
|
||||
Color: link.Color,
|
||||
Position: link.Position,
|
||||
IsActive: link.IsActive,
|
||||
}
|
||||
}
|
||||
|
||||
func toResponseList(list []*ContactLink) []contactLinkResponse {
|
||||
resp := make([]contactLinkResponse, 0, len(list))
|
||||
for _, link := range list {
|
||||
resp = append(resp, toResponse(link))
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
// ListAdmin returns every contact link (active or not).
|
||||
func (h *Handler) ListAdmin(c *gin.Context) {
|
||||
list, err := h.service.List(c.Request.Context(), false)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list contact links"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"contact_links": toResponseList(list)})
|
||||
}
|
||||
|
||||
// ListPublic returns only active contact links, for the storefront to display.
|
||||
func (h *Handler) ListPublic(c *gin.Context) {
|
||||
list, err := h.service.List(c.Request.Context(), true)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list contact links"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"contact_links": toResponseList(list)})
|
||||
}
|
||||
|
||||
type upsertRequest struct {
|
||||
Label string `json:"label" binding:"required"`
|
||||
URL string `json:"url" binding:"required"`
|
||||
IconKey string `json:"icon_key"`
|
||||
IconMediaID *uuid.UUID `json:"icon_media_id"`
|
||||
Color string `json:"color"`
|
||||
Position int `json:"position"`
|
||||
IsActive bool `json:"is_active"`
|
||||
}
|
||||
|
||||
func (h *Handler) Create(c *gin.Context) {
|
||||
var req upsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
link, err := h.service.Create(c.Request.Context(), req.Label, req.URL, req.IconKey, req.IconMediaID, req.Color, req.Position, req.IsActive)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create contact link"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toResponse(link))
|
||||
}
|
||||
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var req upsertRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
link, err := h.service.Update(c.Request.Context(), id, req.Label, req.URL, req.IconKey, req.IconMediaID, req.Color, req.Position, req.IsActive)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "contact link not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update contact link"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toResponse(link))
|
||||
}
|
||||
|
||||
type updatePositionRequest struct {
|
||||
Position int `json:"position"`
|
||||
}
|
||||
|
||||
// UpdatePosition lets the admin reorder the contact link display order
|
||||
// (e.g. move up/move down in the admin list) without resending the whole
|
||||
// contact link payload.
|
||||
func (h *Handler) UpdatePosition(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var req updatePositionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "validation error", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
link, err := h.service.UpdatePosition(c.Request.Context(), id, req.Position)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "contact link not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update position"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toResponse(link))
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "contact link not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete contact link"})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Package contactlinks lets the admin publish an arbitrary list of
|
||||
// communication channels (WhatsApp, Telegram, Signal, Discord, email, ...)
|
||||
// for customers to reach the shop through. Each entry is a label, a URL,
|
||||
// and its own style: either a built-in brand icon (IconKey, matched against
|
||||
// the frontend's curated icon set) or a custom image picked from the media
|
||||
// library (IconMediaID), plus a color -- so no per-app integration is
|
||||
// needed to add a new one.
|
||||
package contactlinks
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ContactLink struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primaryKey"`
|
||||
Label string `gorm:"not null"`
|
||||
URL string `gorm:"not null"`
|
||||
IconKey string `gorm:"not null;default:''"`
|
||||
IconMediaID *uuid.UUID `gorm:"type:uuid"`
|
||||
Color string `gorm:"not null;default:''"`
|
||||
Position int `gorm:"not null;default:0"`
|
||||
IsActive bool `gorm:"not null;default:true"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (ContactLink) TableName() string { return "contact_links" }
|
||||
@@ -0,0 +1,70 @@
|
||||
package contactlinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("contact link not found")
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, link *ContactLink) error
|
||||
FindByID(ctx context.Context, id uuid.UUID) (*ContactLink, error)
|
||||
List(ctx context.Context, activeOnly bool) ([]*ContactLink, error)
|
||||
Update(ctx context.Context, link *ContactLink) error
|
||||
Delete(ctx context.Context, id uuid.UUID) error
|
||||
}
|
||||
|
||||
type gormRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) Repository {
|
||||
return &gormRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *gormRepository) Create(ctx context.Context, link *ContactLink) error {
|
||||
return r.db.WithContext(ctx).Create(link).Error
|
||||
}
|
||||
|
||||
func (r *gormRepository) FindByID(ctx context.Context, id uuid.UUID) (*ContactLink, error) {
|
||||
var link ContactLink
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&link).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
|
||||
func (r *gormRepository) List(ctx context.Context, activeOnly bool) ([]*ContactLink, error) {
|
||||
q := r.db.WithContext(ctx).Order("position asc, label asc")
|
||||
if activeOnly {
|
||||
q = q.Where("is_active = ?", true)
|
||||
}
|
||||
var list []*ContactLink
|
||||
if err := q.Find(&list).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (r *gormRepository) Update(ctx context.Context, link *ContactLink) error {
|
||||
return r.db.WithContext(ctx).Save(link).Error
|
||||
}
|
||||
|
||||
func (r *gormRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
res := r.db.WithContext(ctx).Delete(&ContactLink{}, "id = ?", id)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package contactlinks
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RegisterAdminRoutes(rg *gin.RouterGroup, h *Handler, requireAdmin gin.HandlerFunc) {
|
||||
group := rg.Group("/admin/contact-links", requireAdmin)
|
||||
group.GET("", h.ListAdmin)
|
||||
group.POST("", h.Create)
|
||||
group.PUT("/:id", h.Update)
|
||||
group.PATCH("/:id/position", h.UpdatePosition)
|
||||
group.DELETE("/:id", h.Delete)
|
||||
}
|
||||
|
||||
// RegisterPublicRoutes exposes the read-only, active-only contact link list
|
||||
// used by the storefront to let customers reach the shop.
|
||||
func RegisterPublicRoutes(rg *gin.RouterGroup, h *Handler) {
|
||||
rg.GET("/contact-links", h.ListPublic)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package contactlinks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, activeOnly bool) ([]*ContactLink, error) {
|
||||
return s.repo.List(ctx, activeOnly)
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, id uuid.UUID) (*ContactLink, error) {
|
||||
return s.repo.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, label, url, iconKey string, iconMediaID *uuid.UUID, color string, position int, isActive bool) (*ContactLink, error) {
|
||||
link := &ContactLink{
|
||||
ID: uuid.New(),
|
||||
Label: label,
|
||||
URL: url,
|
||||
IconKey: iconKey,
|
||||
IconMediaID: iconMediaID,
|
||||
Color: color,
|
||||
Position: position,
|
||||
IsActive: isActive,
|
||||
}
|
||||
if err := s.repo.Create(ctx, link); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return link, nil
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, id uuid.UUID, label, url, iconKey string, iconMediaID *uuid.UUID, color string, position int, isActive bool) (*ContactLink, error) {
|
||||
link, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link.Label = label
|
||||
link.URL = url
|
||||
link.IconKey = iconKey
|
||||
link.IconMediaID = iconMediaID
|
||||
link.Color = color
|
||||
link.Position = position
|
||||
link.IsActive = isActive
|
||||
if err := s.repo.Update(ctx, link); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return link, nil
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return s.repo.Delete(ctx, id)
|
||||
}
|
||||
|
||||
// UpdatePosition lets the admin reorder the displayed list without
|
||||
// resending the full contact link payload.
|
||||
func (s *Service) UpdatePosition(ctx context.Context, id uuid.UUID, position int) (*ContactLink, error) {
|
||||
link, err := s.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link.Position = position
|
||||
if err := s.repo.Update(ctx, link); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return link, nil
|
||||
}
|
||||
Reference in New Issue
Block a user