Initial commit: Homelab API
This commit is contained in:
186
internal/handler/tasks.go
Normal file
186
internal/handler/tasks.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/daniil/homelab-api/internal/middleware"
|
||||
"github.com/daniil/homelab-api/internal/model"
|
||||
"github.com/daniil/homelab-api/internal/repository"
|
||||
"github.com/daniil/homelab-api/internal/service"
|
||||
)
|
||||
|
||||
type TaskHandler struct {
|
||||
taskService *service.TaskService
|
||||
}
|
||||
|
||||
func NewTaskHandler(taskService *service.TaskService) *TaskHandler {
|
||||
return &TaskHandler{taskService: taskService}
|
||||
}
|
||||
|
||||
func (h *TaskHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
var completed *bool
|
||||
if c := r.URL.Query().Get("completed"); c != "" {
|
||||
b := c == "true"
|
||||
completed = &b
|
||||
}
|
||||
|
||||
tasks, err := h.taskService.List(userID, completed)
|
||||
if err != nil {
|
||||
writeError(w, "failed to fetch tasks", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, tasks, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Today(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
tasks, err := h.taskService.GetTodayTasks(userID)
|
||||
if err != nil {
|
||||
writeError(w, "failed to fetch today tasks", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, tasks, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
var req model.CreateTaskRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Title == "" {
|
||||
writeError(w, "title is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := h.taskService.Create(userID, &req)
|
||||
if err != nil {
|
||||
writeError(w, "failed to create task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, task, http.StatusCreated)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
taskID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, "invalid task id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := h.taskService.Get(taskID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrTaskNotFound) {
|
||||
writeError(w, "task not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeError(w, "failed to fetch task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, task, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
taskID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, "invalid task id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var req model.UpdateTaskRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := h.taskService.Update(taskID, userID, &req)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrTaskNotFound) {
|
||||
writeError(w, "task not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeError(w, "failed to update task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, task, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
taskID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, "invalid task id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.taskService.Delete(taskID, userID); err != nil {
|
||||
if errors.Is(err, repository.ErrTaskNotFound) {
|
||||
writeError(w, "task not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeError(w, "failed to delete task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Complete(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
taskID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, "invalid task id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := h.taskService.Complete(taskID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrTaskNotFound) {
|
||||
writeError(w, "task not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeError(w, "failed to complete task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, task, http.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TaskHandler) Uncomplete(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
taskID, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
if err != nil {
|
||||
writeError(w, "invalid task id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := h.taskService.Uncomplete(taskID, userID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrTaskNotFound) {
|
||||
writeError(w, "task not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeError(w, "failed to uncomplete task", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, task, http.StatusOK)
|
||||
}
|
||||
Reference in New Issue
Block a user