Compare commits
No commits in common. "main" and "my-changes" have entirely different histories.
main
...
my-changes
@ -1,7 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"5803024003/prak-03-Calvin/internal/handlers"
|
"5803024003/internal/db"
|
||||||
|
"5803024003/internal/handlers"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -9,10 +10,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func home(w http.ResponseWriter, r *http.Request) {
|
func home(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("Hello from Task Management API"))
|
w.Write([]byte("Hello broskie"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Initialize database connection
|
||||||
|
database, err := db.InitDB()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to connect to database:", err)
|
||||||
|
}
|
||||||
|
defer database.Close()
|
||||||
|
|
||||||
|
// Set database connection for handlers
|
||||||
|
handlers.SetDatabase(database)
|
||||||
|
|
||||||
|
// Use mux.NewRouter() to initialize Gorilla Mux router
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", home).Methods("GET")
|
r.HandleFunc("/", home).Methods("GET")
|
||||||
|
|
||||||
@ -22,15 +34,16 @@ func main() {
|
|||||||
r.HandleFunc("/groups/{groupId}", handlers.RemoveGroupHandler).Methods("DELETE")
|
r.HandleFunc("/groups/{groupId}", handlers.RemoveGroupHandler).Methods("DELETE")
|
||||||
|
|
||||||
// Task management routes
|
// Task management routes
|
||||||
r.HandleFunc("/groups/{groupId}/tasks", handlers.CreateTaskHandler).Methods("POST")
|
r.HandleFunc("/groups/{groupId}/task", handlers.CreateTaskHandler).Methods("POST")
|
||||||
r.HandleFunc("/groups/{groupId}/tasks", handlers.DisplayTasksByGroupHandler).Methods("GET")
|
r.HandleFunc("/groups/{groupId}/task", handlers.DisplayTasksByGroupHandler).Methods("GET")
|
||||||
r.HandleFunc("/tasks", handlers.DisplayTasksHandler).Methods("GET")
|
r.HandleFunc("/task", handlers.DisplayTasksHandler).Methods("GET")
|
||||||
r.HandleFunc("/tasks/{taskId}", handlers.GetTaskHandler).Methods("GET")
|
r.HandleFunc("/task/{taskId}", handlers.GetTaskHandler).Methods("GET")
|
||||||
r.HandleFunc("/tasks/{taskId}", handlers.UpdateTaskHandler).Methods("PUT")
|
r.HandleFunc("/task/{taskId}", handlers.UpdateTaskHandler).Methods("PUT")
|
||||||
r.HandleFunc("/tasks/{taskId}", handlers.RemoveTaskHandler).Methods("DELETE")
|
r.HandleFunc("/task/{taskId}", handlers.RemoveTaskHandler).Methods("DELETE")
|
||||||
r.HandleFunc("/tasks/{taskId}/done", handlers.MarkTaskDoneHandler).Methods("PUT")
|
r.HandleFunc("/task/{taskId}/done", handlers.MarkTaskDoneHandler).Methods("PUT")
|
||||||
|
|
||||||
|
// Use the http.ListenAndServe() function to start a new web server.
|
||||||
log.Print("Starting server on :4000")
|
log.Print("Starting server on :4000")
|
||||||
err := http.ListenAndServe(":4000", r)
|
err = http.ListenAndServe(":4000", r)
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
9
go.mod
9
go.mod
@ -1,8 +1,7 @@
|
|||||||
module 5803024017/prak-03-Raymond
|
module 5803024003
|
||||||
|
|
||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require github.com/gorilla/mux v1.8.1
|
||||||
github.com/gorilla/mux v1.8.1 // indirect
|
|
||||||
github.com/lib/pq v1.10.9 // indirect
|
require github.com/lib/pq v1.10.9
|
||||||
)
|
|
||||||
|
@ -12,25 +12,28 @@ const (
|
|||||||
port = 45432
|
port = 45432
|
||||||
user = "5803024003"
|
user = "5803024003"
|
||||||
password = "pw5803024003"
|
password = "pw5803024003"
|
||||||
dbname = "5803024003_db"
|
dbname = "tgs01_5803024003"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDB() {
|
// InitDB returns a database connection
|
||||||
|
func InitDB() (*sql.DB, error) {
|
||||||
// connection string
|
// connection string
|
||||||
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
|
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
|
||||||
|
|
||||||
// open database
|
// open database
|
||||||
db, err := sql.Open("postgres", psqlconn)
|
db, err := sql.Open("postgres", psqlconn)
|
||||||
CheckError(err)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
// close database
|
}
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// check db
|
// check db
|
||||||
err = db.Ping()
|
err = db.Ping()
|
||||||
CheckError(err)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Connected!")
|
fmt.Println("Connected to database!")
|
||||||
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
|
49
internal/db/group_queries.go
Normal file
49
internal/db/group_queries.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateGroup inserts a new group into the database
|
||||||
|
func CreateGroup(db *sql.DB, groupName string) error {
|
||||||
|
query := `INSERT INTO groups (group_name) VALUES ($1)`
|
||||||
|
_, err := db.Exec(query, groupName)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetGroupByID retrieves a group by its ID
|
||||||
|
func GetGroupByID(db *sql.DB, groupID int) (*Groups, error) {
|
||||||
|
query := `SELECT group_id, group_name FROM groups WHERE group_id = $1`
|
||||||
|
row := db.QueryRow(query, groupID)
|
||||||
|
|
||||||
|
var group Groups
|
||||||
|
err := row.Scan(&group.GroupId, &group.GroupName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &group, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveGroup deletes a group and all its associated tasks
|
||||||
|
func RemoveGroup(db *sql.DB, groupID int) error {
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
// Delete all tasks in the group first
|
||||||
|
_, err = tx.Exec("DELETE FROM tasks WHERE group_id = $1", groupID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the group
|
||||||
|
_, err = tx.Exec("DELETE FROM groups WHERE group_id = $1", groupID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
@ -4,18 +4,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Region represents a geographical region (e.g., "West Campus", "Downtown")
|
|
||||||
type Groups struct{
|
type Groups struct{
|
||||||
GroupID int `json:"regionId" db:"region_id"`
|
GroupId int `json:"group_id"`
|
||||||
GroupName string `json:"name" db:"name"`
|
GroupName string `json:"group_name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Building represents a building within a region
|
|
||||||
type Tasks struct{
|
type Tasks struct{
|
||||||
TaskID int `json:"buildingId" db:"building_id"`
|
TaskID int `json:"task_id"`
|
||||||
TaskName string `json:"name" db:"name"`
|
TaskName string `json:"task_name"` // Maps to "task" column in SQL
|
||||||
TaskDescription int `json:"floorCount,omitempty" db:"floor_count"`
|
TaskDescription string `json:"task_desc"` // Maps to "task_desc" column in SQL (changed from *int to string)
|
||||||
GroupID int `json:"regionId" db:"region_id"`
|
GroupID int `json:"group_id"`
|
||||||
IsDone bool `json:"isDone" db:"is_done"`
|
IsDone bool `json:"is_done"` // Maps to "isdone" column in SQL
|
||||||
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
}
|
99
internal/db/task_queries.go
Normal file
99
internal/db/task_queries.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateTask inserts a new task into the database
|
||||||
|
func CreateTask(db *sql.DB, taskName string, taskDescription string, groupID int) error {
|
||||||
|
query := `INSERT INTO tasks (task_name, task_desc, group_id, is_done, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)`
|
||||||
|
_, err := db.Exec(query, taskName, taskDescription, groupID, false, time.Now())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllTasks retrieves all tasks from the database
|
||||||
|
func GetAllTasks(db *sql.DB) ([]Tasks, error) {
|
||||||
|
query := `SELECT task_id, task_name, task_desc, group_id, is_done, created_at
|
||||||
|
FROM tasks ORDER BY created_at DESC`
|
||||||
|
rows, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tasks []Tasks
|
||||||
|
for rows.Next() {
|
||||||
|
var task Tasks
|
||||||
|
err := rows.Scan(&task.TaskID, &task.TaskName, &task.TaskDescription,
|
||||||
|
&task.GroupID, &task.IsDone, &task.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tasks = append(tasks, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTaskByID retrieves a specific task by its ID
|
||||||
|
func GetTaskByID(db *sql.DB, taskID int) (*Tasks, error) {
|
||||||
|
query := `SELECT task_id, task_name, task_desc, group_id, is_done, created_at
|
||||||
|
FROM tasks WHERE task_id = $1`
|
||||||
|
row := db.QueryRow(query, taskID)
|
||||||
|
|
||||||
|
var task Tasks
|
||||||
|
err := row.Scan(&task.TaskID, &task.TaskName, &task.TaskDescription,
|
||||||
|
&task.GroupID, &task.IsDone, &task.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &task, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTasksByGroupID retrieves all tasks for a specific group
|
||||||
|
func GetTasksByGroupID(db *sql.DB, groupID int) ([]Tasks, error) {
|
||||||
|
query := `SELECT task_id, task_name, task_desc, group_id, is_done, created_at
|
||||||
|
FROM tasks WHERE group_id = $1`
|
||||||
|
rows, err := db.Query(query, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var tasks []Tasks
|
||||||
|
for rows.Next() {
|
||||||
|
var task Tasks
|
||||||
|
err := rows.Scan(&task.TaskID, &task.TaskName, &task.TaskDescription,
|
||||||
|
&task.GroupID, &task.IsDone, &task.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tasks = append(tasks, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkTaskAsDone updates a task's status to completed
|
||||||
|
func MarkTaskAsDone(db *sql.DB, taskID int) error {
|
||||||
|
query := `UPDATE tasks SET is_done = true WHERE task_id = $1`
|
||||||
|
_, err := db.Exec(query, taskID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveTask deletes a task from the database
|
||||||
|
func RemoveTask(db *sql.DB, taskID int) error {
|
||||||
|
query := `DELETE FROM tasks WHERE task_id = $1`
|
||||||
|
_, err := db.Exec(query, taskID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateTask updates task name and description
|
||||||
|
func UpdateTask(db *sql.DB, taskID int, taskName string, taskDescription string) error {
|
||||||
|
query := `UPDATE tasks SET task_name = $1, task_desc = $2 WHERE task_id = $3`
|
||||||
|
_, err := db.Exec(query, taskName, taskDescription, taskID)
|
||||||
|
return err
|
||||||
|
}
|
@ -1,26 +1,139 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"5803024003/internal/db"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"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) {
|
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)
|
w.WriteHeader(http.StatusCreated)
|
||||||
w.Write([]byte("Group Created"))
|
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) {
|
func RemoveGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
groupID := vars["groupId"]
|
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)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("Group ID " + groupID + " Removed"))
|
json.NewEncoder(w).Encode(map[string]string{"message": "Group removed successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroupHandler(w http.ResponseWriter, r *http.Request) {
|
func GetGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
groupID := vars["groupId"]
|
groupIDStr := vars["groupId"]
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Group ID: " + 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
194
internal/handlers/task_handlers.go
Normal file
194
internal/handlers/task_handlers.go
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
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"})
|
||||||
|
}
|
@ -1,57 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
w.Write([]byte("Task Created"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func DisplayTasksHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying All Tasks"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func DisplayTasksByGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
groupID := vars["groupId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Tasks for Group ID: " + groupID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Task ID: " + taskID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func MarkTaskDoneHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Marked as Done"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func RemoveTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Removed"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Updated"))
|
|
||||||
}
|
|
109
main.go
109
main.go
@ -1,109 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
)
|
|
||||||
|
|
||||||
func createGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
w.Write([]byte("Group Created"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func createTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
groupID := vars["groupId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
w.Write([]byte("Task Created in Group ID: " + groupID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func displayTasksHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying All Tasks"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func displayTasksByGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
groupID := vars["groupId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Tasks for Group ID: " + groupID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Task ID: " + taskID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func markTaskDoneHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Marked as Done"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Removed"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func removeGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
groupID := vars["groupId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Group ID " + groupID + " Removed"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateTaskHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
taskID := vars["taskId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Task ID " + taskID + " Updated"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func getGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
groupID := vars["groupId"]
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write([]byte("Displaying Group ID: " + groupID))
|
|
||||||
}
|
|
||||||
|
|
||||||
func home(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write([]byte("Hello from Task Management API"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
r := mux.NewRouter()
|
|
||||||
r.HandleFunc("/", home).Methods("GET")
|
|
||||||
|
|
||||||
// Group management routes
|
|
||||||
r.HandleFunc("/groups", createGroupHandler).Methods("POST")
|
|
||||||
r.HandleFunc("/groups/{groupId}", getGroupHandler).Methods("GET")
|
|
||||||
r.HandleFunc("/groups/{groupId}", removeGroupHandler).Methods("DELETE")
|
|
||||||
|
|
||||||
// Task management routes
|
|
||||||
r.HandleFunc("/groups/{groupId}/tasks", createTaskHandler).Methods("POST")
|
|
||||||
r.HandleFunc("/groups/{groupId}/tasks", displayTasksByGroupHandler).Methods("GET")
|
|
||||||
r.HandleFunc("/tasks", displayTasksHandler).Methods("GET")
|
|
||||||
r.HandleFunc("/tasks/{taskId}", getTaskHandler).Methods("GET")
|
|
||||||
r.HandleFunc("/tasks/{taskId}", updateTaskHandler).Methods("PUT")
|
|
||||||
r.HandleFunc("/tasks/{taskId}", removeTaskHandler).Methods("DELETE")
|
|
||||||
r.HandleFunc("/tasks/{taskId}/done", markTaskDoneHandler).Methods("PUT")
|
|
||||||
|
|
||||||
log.Print("Starting server on :4000")
|
|
||||||
err := http.ListenAndServe(":4000", r)
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user