initial commit
This commit is contained in:
commit
719e856bc4
36
cmd/server/main.go
Normal file
36
cmd/server/main.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"5803024003/prak-03-Calvin/internal/handlers"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
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", handlers.CreateGroupHandler).Methods("POST")
|
||||||
|
r.HandleFunc("/groups/{groupId}", handlers.GetGroupHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/groups/{groupId}", handlers.RemoveGroupHandler).Methods("DELETE")
|
||||||
|
|
||||||
|
// Task management routes
|
||||||
|
r.HandleFunc("/groups/{groupId}/tasks", handlers.CreateTaskHandler).Methods("POST")
|
||||||
|
r.HandleFunc("/groups/{groupId}/tasks", handlers.DisplayTasksByGroupHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/tasks", handlers.DisplayTasksHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/tasks/{taskId}", handlers.GetTaskHandler).Methods("GET")
|
||||||
|
r.HandleFunc("/tasks/{taskId}", handlers.UpdateTaskHandler).Methods("PUT")
|
||||||
|
r.HandleFunc("/tasks/{taskId}", handlers.RemoveTaskHandler).Methods("DELETE")
|
||||||
|
r.HandleFunc("/tasks/{taskId}/done", handlers.MarkTaskDoneHandler).Methods("PUT")
|
||||||
|
|
||||||
|
log.Print("Starting server on :4000")
|
||||||
|
err := http.ListenAndServe(":4000", r)
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
8
go.mod
Normal file
8
go.mod
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module 5803024017/prak-03-Raymond
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gorilla/mux v1.8.1 // indirect
|
||||||
|
github.com/lib/pq v1.10.9 // indirect
|
||||||
|
)
|
4
go.sum
Normal file
4
go.sum
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||||
|
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
40
internal/db/connection.go
Normal file
40
internal/db/connection.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
host = "202.46.28.160"
|
||||||
|
port = 45432
|
||||||
|
user = "5803024003"
|
||||||
|
password = "pw5803024003"
|
||||||
|
dbname = "5803024003_db"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitDB() {
|
||||||
|
// connection string
|
||||||
|
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
|
||||||
|
|
||||||
|
// open database
|
||||||
|
db, err := sql.Open("postgres", psqlconn)
|
||||||
|
CheckError(err)
|
||||||
|
|
||||||
|
// close database
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// check db
|
||||||
|
err = db.Ping()
|
||||||
|
CheckError(err)
|
||||||
|
|
||||||
|
fmt.Println("Connected!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
21
internal/db/models.go
Normal file
21
internal/db/models.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Region represents a geographical region (e.g., "West Campus", "Downtown")
|
||||||
|
type Groups struct {
|
||||||
|
GroupID int `json:"regionId" db:"region_id"`
|
||||||
|
GroupName string `json:"name" db:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Building represents a building within a region
|
||||||
|
type Tasks struct {
|
||||||
|
TaskID int `json:"buildingId" db:"building_id"`
|
||||||
|
TaskName string `json:"name" db:"name"`
|
||||||
|
TaskDescription int `json:"floorCount,omitempty" db:"floor_count"`
|
||||||
|
GroupID int `json:"regionId" db:"region_id"`
|
||||||
|
IsDone bool `json:"isDone" db:"is_done"`
|
||||||
|
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
||||||
|
}
|
26
internal/handlers/group_handlers.go
Normal file
26
internal/handlers/group_handlers.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
w.Write([]byte("Group Created"))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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))
|
||||||
|
}
|
57
internal/handlers/tasks_handlers.go
Normal file
57
internal/handlers/tasks_handlers.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
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
Normal file
109
main.go
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
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