Added "create list" handler, service + repository
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/robindittmar/dttmr-api/internal/api/request"
|
||||
"github.com/robindittmar/dttmr-api/internal/api/response"
|
||||
"github.com/robindittmar/dttmr-api/internal/domain"
|
||||
)
|
||||
|
||||
type ListHandler struct {
|
||||
ListService *domain.ListService
|
||||
}
|
||||
|
||||
func (h *ListHandler) CreateList(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
payload, err := request.DecodeCreateList(r)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "Failed to decode create list payload", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusBadRequest, "Failed to decode request body")
|
||||
return
|
||||
}
|
||||
|
||||
list, err := h.ListService.Create(ctx, payload.Name, payload.UserIDs)
|
||||
if err != nil {
|
||||
slog.ErrorContext(ctx, "Failed to create list", slog.Any("error", err))
|
||||
response.Error(ctx, w, http.StatusInternalServerError, "Failed to create list")
|
||||
return
|
||||
}
|
||||
|
||||
slog.InfoContext(ctx, "Created list successfully", slog.Any("list_id", list.ID))
|
||||
response.JSON(ctx, w, http.StatusCreated, list)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CreateListPayload struct {
|
||||
Name string `json:"name"`
|
||||
UserIDs []string `json:"user_ids"`
|
||||
}
|
||||
|
||||
func DecodeCreateList(r *http.Request) (CreateListPayload, error) {
|
||||
var payload CreateListPayload
|
||||
|
||||
r.Body = http.MaxBytesReader(nil, r.Body, 1024*64)
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(&payload); err != nil {
|
||||
return payload, fmt.Errorf("error decoding create list payload: %w", err)
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
@@ -1,20 +1,32 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/robindittmar/dttmr-api/internal/api/handler"
|
||||
"github.com/robindittmar/dttmr-api/internal/api/middleware"
|
||||
"github.com/robindittmar/dttmr-api/internal/domain"
|
||||
"github.com/robindittmar/dttmr-api/internal/repository"
|
||||
)
|
||||
|
||||
type Config struct{}
|
||||
type Config struct {
|
||||
Database *sql.DB
|
||||
}
|
||||
|
||||
func NewMux(cfg Config) http.Handler {
|
||||
listRepo := repository.NewListRepo(cfg.Database)
|
||||
listService := domain.NewListService(listRepo)
|
||||
|
||||
listHandler := handler.ListHandler{ListService: listService}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/", handler.DefaultHandler)
|
||||
mux.HandleFunc("GET /health", handler.HealthHandler)
|
||||
|
||||
mux.HandleFunc("POST /lists", listHandler.CreateList)
|
||||
|
||||
var httpHandler http.Handler = mux
|
||||
httpHandler = middleware.WithTelemetry(httpHandler)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user