Spaces:
Sleeping
Sleeping
| package controllers | |
| import ( | |
| "abdanhafidz.com/go-boilerplate/models/dto" | |
| entity "abdanhafidz.com/go-boilerplate/models/entity" | |
| "abdanhafidz.com/go-boilerplate/services" | |
| "github.com/gin-gonic/gin" | |
| "github.com/google/uuid" | |
| ) | |
| type SuperAdminOrganizationController interface { | |
| CreateOrganization(ctx *gin.Context) | |
| ListOrganizations(ctx *gin.Context) | |
| UpdateOrganization(ctx *gin.Context) | |
| DeleteOrganization(ctx *gin.Context) | |
| } | |
| type superAdminOrganizationController struct { | |
| orgService services.OrganizationService | |
| } | |
| func NewSuperAdminOrganizationController(orgService services.OrganizationService) SuperAdminOrganizationController { | |
| return &superAdminOrganizationController{orgService: orgService} | |
| } | |
| func (c *superAdminOrganizationController) CreateOrganization(ctx *gin.Context) { | |
| req := RequestJSON[dto.CreateOrganizationRequest](ctx) | |
| callerID := ParseAccountId(ctx) | |
| org, err := c.orgService.Create(ctx.Request.Context(), callerID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, dto.OrganizationResponse{}, err) | |
| return | |
| } | |
| res := toOrganizationResponse(org) | |
| ResponseJSON(ctx, req, res, nil) | |
| } | |
| func (c *superAdminOrganizationController) ListOrganizations(ctx *gin.Context) { | |
| callerID := ParseAccountId(ctx) | |
| orgs, err := c.orgService.GetAll(ctx.Request.Context(), callerID) | |
| if err != nil { | |
| ResponseJSON(ctx, gin.H{}, []dto.OrganizationResponse{}, err) | |
| return | |
| } | |
| res := make([]dto.OrganizationResponse, len(orgs)) | |
| for i, org := range orgs { | |
| res[i] = toOrganizationResponse(org) | |
| } | |
| ResponseJSON(ctx, gin.H{}, res, nil) | |
| } | |
| func (c *superAdminOrganizationController) UpdateOrganization(ctx *gin.Context) { | |
| idParam := ctx.Param("organization_id") | |
| orgID, err := uuid.Parse(idParam) | |
| if err != nil { | |
| ResponseJSON(ctx, gin.H{"id": idParam}, dto.OrganizationResponse{}, err) | |
| return | |
| } | |
| req := RequestJSON[dto.UpdateOrganizationRequest](ctx) | |
| callerID := ParseAccountId(ctx) | |
| org, err := c.orgService.Update(ctx.Request.Context(), callerID, orgID, req) | |
| if err != nil { | |
| ResponseJSON(ctx, req, dto.OrganizationResponse{}, err) | |
| return | |
| } | |
| res := toOrganizationResponse(org) | |
| ResponseJSON(ctx, req, res, nil) | |
| } | |
| func (c *superAdminOrganizationController) DeleteOrganization(ctx *gin.Context) { | |
| idParam := ctx.Param("organization_id") | |
| orgID, err := uuid.Parse(idParam) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": idParam}, nil, err) | |
| return | |
| } | |
| callerID := ParseAccountId(ctx) | |
| err = c.orgService.Delete(ctx.Request.Context(), callerID, orgID) | |
| if err != nil { | |
| ResponseJSON[any](ctx, gin.H{"id": orgID}, nil, err) | |
| return | |
| } | |
| ResponseJSON(ctx, gin.H{"id": orgID}, gin.H{"message": "Organization deleted successfully"}, nil) | |
| } | |
| func toOrganizationResponse(org entity.Organization) dto.OrganizationResponse { | |
| return dto.OrganizationResponse{ | |
| Id: org.Id, | |
| Name: org.Name, | |
| Slug: org.Slug, | |
| CreatedAt: org.CreatedAt.Format("2006-01-02T15:04:05Z07:00"), | |
| UpdatedAt: org.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"), | |
| } | |
| } | |