79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/goccy/go-json"
|
|
"madsky.ru/go-tracker/internal/model/status"
|
|
"madsky.ru/go-tracker/internal/model/user"
|
|
"madsky.ru/go-tracker/internal/server/request"
|
|
"madsky.ru/go-tracker/internal/server/response"
|
|
"net/http"
|
|
)
|
|
|
|
func (app *Application) findStatuses(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
u := ctx.Value(UserContext{}).(*user.User)
|
|
fmt.Println(u)
|
|
|
|
s, err := app.Storage.Status.Find(app.Ctx)
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
response.WriteJSON(w, nil, http.StatusOK, &s)
|
|
}
|
|
|
|
func (app *Application) FindStatusById(w http.ResponseWriter, r *http.Request) {
|
|
id, err := request.Param(r, "id")
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
s, err := app.Storage.Status.FindOne(app.Ctx, id)
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
response.WriteJSON(w, nil, http.StatusOK, &s)
|
|
}
|
|
|
|
func (app *Application) CreateStatus(w http.ResponseWriter, r *http.Request) {
|
|
dec := json.NewDecoder(r.Body)
|
|
dec.DisallowUnknownFields()
|
|
|
|
var statusDto status.CreateStatusDTO
|
|
|
|
err := dec.Decode(&statusDto)
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusNotAcceptable)
|
|
return
|
|
}
|
|
|
|
s, err := app.Storage.Status.Create(app.Ctx, &statusDto)
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
response.WriteJSON(w, nil, http.StatusOK, &s)
|
|
}
|
|
|
|
func (app *Application) DeleteStatus(w http.ResponseWriter, r *http.Request) {
|
|
id, err := request.Param(r, "id")
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
count, err := app.Storage.Status.Remove(app.Ctx, id)
|
|
if err != nil {
|
|
response.Error(w, err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
response.WriteJSON(w, nil, http.StatusOK, count)
|
|
}
|