111 lines
3.3 KiB
Go
111 lines
3.3 KiB
Go
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() {
|
|
// Use mux.NewRouter() to initialize Gorilla Mux router
|
|
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")
|
|
|
|
// Use the http.ListenAndServe() function to start a new web server.
|
|
log.Print("Starting server on :4000")
|
|
err := http.ListenAndServe(":4000", r)
|
|
log.Fatal(err)
|
|
} |