Spaces:
Runtime error
Runtime error
Commit ·
4dc09b8
1
Parent(s): 1d9bafe
feat: add delete schema handler
Browse filesFiles changed (4)
94 unmodified lines95969794 unmodified linesw.WriteHeader(http.StatusOK)json.NewEncoder(w).Encode(response)}94 unmodified lines959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013194 unmodified linesw.WriteHeader(http.StatusOK)json.NewEncoder(w).Encode(response)}func (h *SchemaHandler) DeleteSchema(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "application/json")userIDVal := r.Context().Value(middleware.UserIDKey)if userIDVal == nil {w.WriteHeader(http.StatusUnauthorized)json.NewEncoder(w).Encode(map[string]string{"error": "unauthorized"})return}userID := userIDVal.(string)slug := r.URL.Query().Get("slug")if slug == "" {w.WriteHeader(http.StatusBadRequest)json.NewEncoder(w).Encode(map[string]string{"error": "slug is required"})return}if err := h.Repo.DeleteSchema(r.Context(), userID, slug); err != nil {if err.Error() == "no schema found with slug: "+slug { // Sesuaikan string-nya atau pakai strings.Containsw.WriteHeader(http.StatusNotFound)json.NewEncoder(w).Encode(map[string]string{"error": "schema not found or unauthorized"})return}w.WriteHeader(http.StatusInternalServerError)json.NewEncoder(w).Encode(map[string]string{"error": "failed to delete schema: " + err.Error()})return}w.WriteHeader(http.StatusOK)json.NewEncoder(w).Encode(map[string]string{"message": "schema deleted successfully"})}
74 unmodified lines75767778798081828384858687888990919293949596979 unmodified lines10710810911074 unmodified linesworkDir, _ := os.Getwd()filesDir := http.Dir(workDir + "/public")r.Get("/", func(w http.ResponseWriter, r *http.Request) {http.ServeFile(w, r, workDir+"/public/index.html")})fileServer(r, "/public", filesDir)r.Group(func(r chi.Router) {r.Use(authMiddleware.Handle)r.Post("/v1/schemas", schemaHandler.CreateSchema)r.Get("/v1/schemas", schemaHandler.ListSchemas)r.Post("/v1/transcribe", httpHandler.UploadAudioFile)r.Get("/v1/stream", wsHandler.HandleStream)})port := "8080"if p := os.Getenv("PORT"); p != "" { port = p }fmt.Printf("\nANPUT API running on http://localhost:%s\n", port)http.ListenAndServe(":"+port, r)9 unmodified linesfs := http.StripPrefix(pathPrefix, http.FileServer(root))fs.ServeHTTP(w, r)})}74 unmodified lines757677787980818283848586878889909192939495969798991001011021031049 unmodified lines11411511611774 unmodified linesworkDir, _ := os.Getwd()filesDir := http.Dir(workDir + "/public")r.Get("/", func(w http.ResponseWriter, r *http.Request) {http.ServeFile(w, r, workDir+"/public/index.html")})r.Get("/app.js", func(w http.ResponseWriter, r *http.Request) {http.ServeFile(w, r, workDir+"/public/app.js")})fileServer(r, "/public", filesDir)r.Group(func(r chi.Router) {r.Use(authMiddleware.Handle)r.Post("/v1/schemas", schemaHandler.CreateSchema)r.Get("/v1/schemas", schemaHandler.ListSchemas)r.Delete("/v1/schemas/{slug}", schemaHandler.DeleteSchema)r.Post("/v1/transcribe", httpHandler.UploadAudioFile)r.Get("/v1/stream", wsHandler.HandleStream)})port := "8080"if p := os.Getenv("PORT"); p != "" {port = p}fmt.Printf("\nANPUT API running on http://localhost:%s\n", port)http.ListenAndServe(":"+port, r)9 unmodified linesfs := http.StripPrefix(pathPrefix, http.FileServer(root))fs.ServeHTTP(w, r)})}
28 unmodified lines29303132333428 unmodified linesGetSchemaBySlug(ctx context.Context, userID string, slug string) (*Schema, error)CreateSchema(ctx context.Context, s *Schema) errorListSchemas(ctx context.Context, UserID string) ([]*Schema, error)}type PostgresRepo struct {28 unmodified lines2930313233343528 unmodified linesGetSchemaBySlug(ctx context.Context, userID string, slug string) (*Schema, error)CreateSchema(ctx context.Context, s *Schema) errorListSchemas(ctx context.Context, UserID string) ([]*Schema, error)DeleteSchema(ctx context.Context, userID string, slug string) error}type PostgresRepo struct {
112 unmodified lines113114115112 unmodified linesreturn schemas, nil}112 unmodified lines113114115116117118119120121122123124125126127128129130131112 unmodified linesreturn schemas, nil}func (r *PostgresRepo) DeleteSchema(ctx context.Context, userID, slug string) error {query := `DELETE FROM schemas WHERE user_id = $1 AND slug = $2`result, err := r.Pool.Exec(ctx, query, userID, slug)if err != nil {return fmt.Errorf("failed to delete schema: %w", err)}rowsAffected := result.RowsAffected()if rowsAffected == 0 {return fmt.Errorf("no schema found with slug: %s", slug)}return nil}
handler/schema_handler.go+34
main.go+11-4
repository/models.go+1
repository/queries.go+16