140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
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)
|
|
}
|
|
|