Spaces:
Runtime error
Runtime error
Download handler/schema_handler.go from alberdjuniawan/anput-api: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/spaces/alberdjuniawan/anput-api/resolve/main/handler/schema_handler.go
- Command line
-
hf download hf://spaces/alberdjuniawan/anput-api/handler/schema_handler.go
-
curl -L -o schema_handler.go https://huggingface.co/spaces/alberdjuniawan/anput-api/resolve/main/handler/schema_handler.go
4.48 kB
| 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) | |
| } | |