package handlers import ( "context" "github.com/goccy/go-json" dto "madsky.ru/go-finance/internal/project" "madsky.ru/go-finance/internal/repository/project" "madsky.ru/go-finance/internal/server/request" "madsky.ru/go-finance/internal/server/response" "net/http" ) func RegisterProjectRoutes(mux *http.ServeMux, ctx context.Context, repository project.Repository) { mux.HandleFunc("GET /api/projects", FindProjects(ctx, repository)) mux.HandleFunc("GET /api/projects/{id}", FindProjectByID(ctx, repository)) mux.HandleFunc("POST /api/projects", CreateProject(ctx, repository)) mux.HandleFunc("PUT /api/projects/{id}", UpdateProject(ctx, repository)) mux.HandleFunc("DELETE /api/projects/{id}", DeleteProject(ctx, repository)) } func FindProjects(ctx context.Context, repository project.Repository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { p, err := repository.Find(ctx) if err != nil { response.Error(w, err, http.StatusBadRequest) return } response.WriteJSON(w, nil, http.StatusOK, &p) } } func FindProjectByID(ctx context.Context, repository project.Repository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := request.Param(r, "id") if err != nil { response.Error(w, err, http.StatusBadRequest) return } p, err := repository.FindOne(ctx, id) if err != nil { response.Error(w, err, http.StatusBadRequest) return } response.WriteJSON(w, nil, http.StatusOK, &p) } } func CreateProject(ctx context.Context, repository project.Repository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { //ct := r.Header.Get("Content-Type") dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() var projectDto dto.CreateProjectDTO if err := dec.Decode(&projectDto); err != nil { response.Error(w, err, http.StatusBadRequest) return } p, err := repository.Create(ctx, &projectDto) if err != nil { response.Error(w, err, http.StatusBadRequest) return } response.WriteJSON(w, nil, http.StatusCreated, &p) } } func UpdateProject(ctx context.Context, repository project.Repository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := request.Param(r, "id") if err != nil { response.Error(w, err, http.StatusBadRequest) return } dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() var projectDto dto.UpdateProjectDTO if err = dec.Decode(&projectDto); err != nil { response.Error(w, err, http.StatusBadRequest) return } p, err := repository.Update(ctx, id, &projectDto) if err != nil { response.Error(w, err, http.StatusBadRequest) return } response.WriteJSON(w, nil, http.StatusOK, &p) } } func DeleteProject(ctx context.Context, repository project.Repository) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := request.Param(r, "id") if err != nil { response.Error(w, err, http.StatusBadRequest) return } count, err := repository.Remove(ctx, id) if err != nil { response.Error(w, err, http.StatusBadRequest) return } response.WriteJSON(w, nil, http.StatusOK, count) } }