194 lines
5.3 KiB
Go
194 lines
5.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"5803024003/internal/db"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type TaskRequest struct {
|
|
TaskName string `json:"task_name"`
|
|
TaskDescription string `json:"task_desc"` // Changed from *string to string to match SQL NOT NULL
|
|
}
|
|
|
|
type TaskUpdateRequest struct {
|
|
TaskName string `json:"task_name"`
|
|
TaskDescription string `json:"task_desc"` // Changed from *string to string
|
|
}
|
|
|
|
func CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
groupIDStr := vars["groupId"]
|
|
|
|
groupID, err := strconv.Atoi(groupIDStr)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid group ID"})
|
|
return
|
|
}
|
|
|
|
var req TaskRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid JSON format"})
|
|
return
|
|
}
|
|
|
|
if req.TaskName == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Task name is required"})
|
|
return
|
|
}
|
|
|
|
if req.TaskDescription == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Task description is required"})
|
|
return
|
|
}
|
|
|
|
if err := db.CreateTask(database, req.TaskName, req.TaskDescription, groupID); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to create task"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "Task created successfully"})
|
|
}
|
|
|
|
func DisplayTasksHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
tasks, err := db.GetAllTasks(database)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to retrieve tasks"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(tasks)
|
|
}
|
|
|
|
func GetTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
taskIDStr := vars["taskId"]
|
|
|
|
taskID, err := strconv.Atoi(taskIDStr)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid task ID"})
|
|
return
|
|
}
|
|
|
|
task, err := db.GetTaskByID(database, taskID)
|
|
if err == sql.ErrNoRows {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Task not found"})
|
|
return
|
|
}
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to retrieve task"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(task)
|
|
}
|
|
|
|
func MarkTaskDoneHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
taskIDStr := vars["taskId"]
|
|
|
|
taskID, err := strconv.Atoi(taskIDStr)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid task ID"})
|
|
return
|
|
}
|
|
|
|
if err := db.MarkTaskAsDone(database, taskID); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to mark task as done"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "Task marked as done successfully"})
|
|
}
|
|
|
|
func RemoveTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
taskIDStr := vars["taskId"]
|
|
|
|
taskID, err := strconv.Atoi(taskIDStr)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid task ID"})
|
|
return
|
|
}
|
|
|
|
if err := db.RemoveTask(database, taskID); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to remove task"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "Task removed successfully"})
|
|
}
|
|
|
|
func UpdateTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
vars := mux.Vars(r)
|
|
taskIDStr := vars["taskId"]
|
|
|
|
taskID, err := strconv.Atoi(taskIDStr)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid task ID"})
|
|
return
|
|
}
|
|
|
|
var req TaskUpdateRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Invalid JSON format"})
|
|
return
|
|
}
|
|
|
|
if req.TaskName == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Task name is required"})
|
|
return
|
|
}
|
|
|
|
if req.TaskDescription == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Task description is required"})
|
|
return
|
|
}
|
|
|
|
if err := db.UpdateTask(database, taskID, req.TaskName, req.TaskDescription); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to update task"})
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(map[string]string{"message": "Task updated successfully"})
|
|
} |