Spaces:
Runtime error
Runtime error
File size: 4,484 Bytes
72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 33937ee 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 8a5f1ff 72f69ae 33937ee 72f69ae 8a5f1ff 72f69ae 4dc09b8 8a5f1ff 4dc09b8 8a5f1ff 4dc09b8 8a5f1ff 4dc09b8 b546831 8a5f1ff 4dc09b8 8a5f1ff 4dc09b8 8a5f1ff 4dc09b8 b546831 8a5f1ff b546831 8a5f1ff b546831 8a5f1ff b546831 8a5f1ff b546831 8a5f1ff b546831 8a5f1ff b546831 8a5f1ff 33937ee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | package handler
import (
"encoding/json"
"net/http"
"time"
"github.com/alberdjuniawan/anput-api/middleware"
"github.com/alberdjuniawan/anput-api/model"
"github.com/alberdjuniawan/anput-api/repository"
"github.com/alberdjuniawan/anput-api/util"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
)
type SchemaHandler struct {
Repo repository.Repository
}
func NewSchemaHandler(repo repository.Repository) *SchemaHandler {
return &SchemaHandler{Repo: repo}
}
func (h *SchemaHandler) CreateSchema(w http.ResponseWriter, r *http.Request) {
reqID := chiMiddleware.GetReqID(r.Context())
userIDVal := r.Context().Value(middleware.UserIDKey)
if userIDVal == nil {
util.WriteError(w, http.StatusUnauthorized, "Unauthorized", reqID)
return
}
userID := userIDVal.(string)
var req model.CreateSchemaRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
util.WriteError(w, http.StatusBadRequest, "Invalid JSON format", reqID)
return
}
if req.Slug == "" || req.Definition == nil {
util.WriteError(w, http.StatusBadRequest, "Slug and Definition are required", reqID)
return
}
schema := &model.Schema{
UserID: userID,
Slug: req.Slug,
Description: req.Description,
Definition: req.Definition,
}
if err := h.Repo.CreateSchema(r.Context(), schema); err != nil {
util.WriteError(w, http.StatusInternalServerError, "Failed to create schema: "+err.Error(), reqID)
return
}
util.WriteSuccess(w, http.StatusCreated, map[string]string{
"message": "schema created successfully",
"slug": req.Slug,
}, reqID)
}
func (h *SchemaHandler) ListSchemas(w http.ResponseWriter, r *http.Request) {
reqID := chiMiddleware.GetReqID(r.Context())
userIDVal := r.Context().Value(middleware.UserIDKey)
if userIDVal == nil {
util.WriteError(w, http.StatusUnauthorized, "Unauthorized", reqID)
return
}
userID := userIDVal.(string)
schemas, err := h.Repo.ListSchemas(r.Context(), userID)
if err != nil {
util.WriteError(w, http.StatusInternalServerError, "Failed to list schemas: "+err.Error(), reqID)
return
}
var response []model.SchemaResponse
for _, s := range schemas {
response = append(response, model.SchemaResponse{
ID: s.ID,
Slug: s.Slug,
Description: s.Description,
Definition: s.Definition,
CreatedAt: time.Now(),
})
}
if response == nil {
response = []model.SchemaResponse{}
}
util.WriteSuccess(w, http.StatusOK, response, reqID)
}
func (h *SchemaHandler) DeleteSchema(w http.ResponseWriter, r *http.Request) {
reqID := chiMiddleware.GetReqID(r.Context())
userIDVal := r.Context().Value(middleware.UserIDKey)
if userIDVal == nil {
util.WriteError(w, http.StatusUnauthorized, "Unauthorized", reqID)
return
}
userID := userIDVal.(string)
slug := r.URL.Query().Get("slug")
if slug == "" {
util.WriteError(w, http.StatusBadRequest, "Slug is required", reqID)
return
}
if err := h.Repo.DeleteSchema(r.Context(), userID, slug); err != nil {
if err.Error() == "no schema found with slug: "+slug {
util.WriteError(w, http.StatusNotFound, "Schema not found", reqID)
return
}
util.WriteError(w, http.StatusInternalServerError, "Failed to delete schema: "+err.Error(), reqID)
return
}
util.WriteSuccess(w, http.StatusOK, map[string]string{
"message": "schema deleted successfully",
}, reqID)
}
func (h *SchemaHandler) UpdateSchema(w http.ResponseWriter, r *http.Request) {
reqID := chiMiddleware.GetReqID(r.Context())
userIDVal := r.Context().Value(middleware.UserIDKey)
if userIDVal == nil {
util.WriteError(w, http.StatusUnauthorized, "Unauthorized", reqID)
return
}
userID := userIDVal.(string)
slug := r.URL.Query().Get("slug")
if slug == "" {
util.WriteError(w, http.StatusBadRequest, "Slug is required", reqID)
return
}
var req model.CreateSchemaRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
util.WriteError(w, http.StatusBadRequest, "Invalid JSON format", reqID)
return
}
err := h.Repo.UpdateSchema(r.Context(), userID, slug, req.Description, req.Definition)
if err != nil {
if err.Error() == "no schema found with slug: "+slug {
util.WriteError(w, http.StatusNotFound, "Schema not found", reqID)
return
}
util.WriteError(w, http.StatusInternalServerError, "Failed to update schema: "+err.Error(), reqID)
return
}
util.WriteSuccess(w, http.StatusOK, map[string]string{
"message": "schema updated successfully",
"slug": slug,
}, reqID)
}
|