158 lines
4.5 KiB
Go
158 lines
4.5 KiB
Go
package org
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
// @Summary Create organization
|
|
// @Description Create a new organization
|
|
// @Tags organizations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body CreateOrgRequest true "Organization details"
|
|
// @Success 201 {object} OrgResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 409 {object} ErrorResponse
|
|
// @Router /api/organizations [post]
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
var req CreateOrgRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
|
|
org, err := h.service.Create(c.Request.Context(), req)
|
|
if err != nil {
|
|
if errors.Is(err, ErrSlugExists) {
|
|
c.JSON(http.StatusConflict, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
log.Printf("create org error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, OrgResponse{Organization: *org})
|
|
}
|
|
|
|
// @Summary Get organization by ID
|
|
// @Description Get organization details
|
|
// @Tags organizations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path string true "Organization ID"
|
|
// @Success 200 {object} OrgResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Router /api/organizations/{id} [get]
|
|
func (h *Handler) GetByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
org, err := h.service.GetByID(c.Request.Context(), id)
|
|
if err != nil {
|
|
if errors.Is(err, ErrNotFound) {
|
|
c.JSON(http.StatusNotFound, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
log.Printf("get org error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, OrgResponse{Organization: *org})
|
|
}
|
|
|
|
// @Summary List organizations
|
|
// @Description Get all organizations
|
|
// @Tags organizations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Success 200 {object} OrgListResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /api/organizations [get]
|
|
func (h *Handler) List(c *gin.Context) {
|
|
resp, err := h.service.List(c.Request.Context())
|
|
if err != nil {
|
|
log.Printf("list orgs error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
// @Summary Update organization
|
|
// @Description Update organization name
|
|
// @Tags organizations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path string true "Organization ID"
|
|
// @Param request body UpdateOrgRequest true "New organization details"
|
|
// @Success 200 {object} OrgResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Router /api/organizations/{id} [put]
|
|
func (h *Handler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
var req UpdateOrgRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
|
|
org, err := h.service.Update(c.Request.Context(), id, req)
|
|
if err != nil {
|
|
if errors.Is(err, ErrNotFound) {
|
|
c.JSON(http.StatusNotFound, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
log.Printf("update org error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, OrgResponse{Organization: *org})
|
|
}
|
|
|
|
// @Summary Delete organization
|
|
// @Description Delete an organization
|
|
// @Tags organizations
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path string true "Organization ID"
|
|
// @Success 200 {object} map[string]string
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Router /api/organizations/{id} [delete]
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
|
|
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
|
if errors.Is(err, ErrNotFound) {
|
|
c.JSON(http.StatusNotFound, ErrorResponse{Error: err.Error()})
|
|
return
|
|
}
|
|
log.Printf("delete org error: %v", err)
|
|
c.JSON(http.StatusInternalServerError, ErrorResponse{Error: "internal server error"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "organization deleted"})
|
|
}
|