111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
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 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)
|
|
}
|
|
}
|