auto commit
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"5803024008/internal/db"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type GroupRequest struct {
|
||||
GroupName string `json:"group_name"`
|
||||
}
|
||||
|
||||
type GroupResponse struct {
|
||||
GroupID int `json:"group_id"`
|
||||
GroupName string `json:"group_name"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// Database connection - you'll need to inject this or use a global variable
|
||||
var database *sql.DB
|
||||
|
||||
func SetDatabase(db *sql.DB) {
|
||||
database = db
|
||||
}
|
||||
|
||||
func CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
var req GroupRequest
|
||||
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.GroupName == "" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "Group name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := db.CreateGroup(database, req.GroupName); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to create group"})
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "Group created successfully"})
|
||||
}
|
||||
|
||||
func DisplayTasksByGroupHandler(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
|
||||
}
|
||||
|
||||
tasks, err := db.GetTasksByGroupID(database, groupID)
|
||||
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 RemoveGroupHandler(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
|
||||
}
|
||||
|
||||
if err := db.RemoveGroup(database, groupID); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to remove group"})
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "Group removed successfully"})
|
||||
}
|
||||
|
||||
func GetGroupHandler(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
|
||||
}
|
||||
|
||||
group, err := db.GetGroupByID(database, groupID)
|
||||
if err == sql.ErrNoRows {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "Group not found"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "Failed to retrieve group"})
|
||||
return
|
||||
}
|
||||
|
||||
response := GroupResponse{
|
||||
GroupID: group.GroupId,
|
||||
GroupName: group.GroupName,
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"5803024008/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"})
|
||||
}
|
||||
Reference in New Issue
Block a user