From 83429fefecb072b1616657fbf40158372f3b6163 Mon Sep 17 00:00:00 2001 From: Uday Yendava Date: Mon, 17 Nov 2025 11:18:31 +0530 Subject: [PATCH 1/3] Added Changes to To Do App --- Dockerfile | 2 +- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ handlers.go | 10 +++++----- main.go | 2 +- models.go | 3 --- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 923e30f..bd54903 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ COPY go.mod go.sum ./ RUN go mod download -COPY . . +COPY . .git RUN go build -o /usr/local/bin/todo-app ./... diff --git a/README.md b/README.md index e69de29..536b0fb 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,44 @@ +This project is a simple REST API built using Go. +It allows you to manage **Users** and their **To-Do Tasks** with basic CRUD operations. + +--- + + + +📘 Go User & Task Management API + +A lightweight, concurrency-safe REST API built using pure Go, designed for managing Users and their associated To-Do Tasks. +This project demonstrates clean API structuring, mutex-based synchronization, JSON handling, routing logic, and in-memory data storage. + +⭐ Features +👤 User Management + +Create a new user + +Retrieve a user by ID + +Delete a user + +📝 Task (To-Do) Management + +Create a task for a specific user + +Fetch all tasks of a user + +Tasks stored user-wise + +Auto-increment task IDs + +🔒 Thread-Safe Operations + +Uses sync.Mutex and sync.RWMutex + +Prevents race conditions under concurrent requests + +⚡ Lightweight + No Dependencies + +Standard library only (net/http, encoding/json) + +No database — uses in-memory maps + +Fast for demos, learning, and prototypes \ No newline at end of file diff --git a/handlers.go b/handlers.go index f83e13c..7d1b1b3 100644 --- a/handlers.go +++ b/handlers.go @@ -27,7 +27,7 @@ func createUsers(w http.ResponseWriter, r *http.Request) { } -func getUsers(w http.ResponseWriter, r *http.Request) { +func getUsers(w http.ResponseWriter, r *http.Request) { // to get users created id, err := strconv.Atoi(r.PathValue("id")) if err != nil { @@ -49,7 +49,7 @@ func getUsers(w http.ResponseWriter, r *http.Request) { } -func deleteUsers(w http.ResponseWriter, r *http.Request) { +func deleteUsers(w http.ResponseWriter, r *http.Request) { // to delete users based on id id, err := strconv.Atoi(r.PathValue("id")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -70,7 +70,7 @@ func deleteUsers(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) } -func getAllUsers(w http.ResponseWriter, r *http.Request) { +func getAllUsers(w http.ResponseWriter, r *http.Request) { // to get all users UserMutex.RLock() defer UserMutex.RUnlock() @@ -83,7 +83,7 @@ func getAllUsers(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(users) } -func createToDoForUser(w http.ResponseWriter, r *http.Request) { +func createToDoForUser(w http.ResponseWriter, r *http.Request) { // to create tasks to users created userID, err := strconv.Atoi(r.PathValue("userID")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) @@ -128,7 +128,7 @@ func createToDoForUser(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(todo) } -func getTasksForUser(w http.ResponseWriter, r *http.Request) { +func getTasksForUser(w http.ResponseWriter, r *http.Request) { // to get tasks for the users userID, err := strconv.Atoi(r.PathValue("userID")) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/main.go b/main.go index 522b50b..869582c 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func main() { fmt.Println(" GET /users/{id}/tasks -> List tasks for user") fmt.Println("=====================================") - log.Println("⚡ Starting server on http://localhost:8081") + log.Println("⚡ Starting server on http://localhost:8081\n") if err := server.ListenAndServe(); err != nil { log.Fatalf("Could not listen on :8081: %v\n", err) diff --git a/models.go b/models.go index fadd544..89e25d6 100644 --- a/models.go +++ b/models.go @@ -21,9 +21,6 @@ var UserList = make(map[int]User) var TaskList = make(map[int][]Tasks) var NextUser = 1 - var NextTask = 1 - var UserMutex = sync.RWMutex{} - var TaskMutex = sync.RWMutex{} From e71dabec5a7a3d9fdaabaaf34b38d377ddd3183d Mon Sep 17 00:00:00 2001 From: Uday Yendava Date: Mon, 17 Nov 2025 11:27:46 +0530 Subject: [PATCH 2/3] Added latest changes to todo app --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 536b0fb..4e80bef 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,3 @@ Prevents race conditions under concurrent requests Standard library only (net/http, encoding/json) -No database — uses in-memory maps - -Fast for demos, learning, and prototypes \ No newline at end of file From bf5ed27e221bb2b0cd0b086fa641a6507b9ff939 Mon Sep 17 00:00:00 2001 From: Uday Yendava Date: Mon, 17 Nov 2025 11:34:20 +0530 Subject: [PATCH 3/3] added latest changes to To-Do application --- Dockerfile | 2 +- README.md | 1 + handlers.go | 2 +- models.go | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index bd54903..9f5f858 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,6 @@ WORKDIR /root/ COPY --from=builder /usr/local/bin/todo-app . -EXPOSE 8080 +EXPOSE 8081 CMD ["./todo-app"] diff --git a/README.md b/README.md index 4e80bef..615090f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> This project is a simple REST API built using Go. It allows you to manage **Users** and their **To-Do Tasks** with basic CRUD operations. diff --git a/handlers.go b/handlers.go index 7d1b1b3..7541a99 100644 --- a/handlers.go +++ b/handlers.go @@ -103,7 +103,7 @@ func createToDoForUser(w http.ResponseWriter, r *http.Request) { // to create ta err = json.NewDecoder(r.Body).Decode(&todo) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, err.Error(), http.StatusInternalServerError) return } if todo.Task == "" { diff --git a/models.go b/models.go index 89e25d6..4dc464a 100644 --- a/models.go +++ b/models.go @@ -11,7 +11,7 @@ type User struct { type Tasks struct { ID int `json:"id"` - UserID int `json:"userId"` + UserID int `json:"usersId"` Task string `json:"task"` Status bool `json:"status"` }