-
Notifications
You must be signed in to change notification settings - Fork 3
Initialise CRUD routes for user service #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayjay19630
wants to merge
3
commits into
CS3219-AY2526Sem1:master
Choose a base branch
from
jayjay19630:feat/jo/init-user-service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"peerprep/user/internal/models" | ||
"peerprep/user/internal/repositories" | ||
) | ||
|
||
type UserHandler struct { | ||
Repo *repositories.UserRepository | ||
} | ||
|
||
// CreateUserHandler handles user creation | ||
func (h *UserHandler) CreateUserHandler(w http.ResponseWriter, r *http.Request) { | ||
var user models.User | ||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil { | ||
http.Error(w, "Invalid request payload", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Save user using the repository | ||
if err := h.Repo.CreateUser(&user); err != nil { | ||
http.Error(w, "Failed to create user", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
json.NewEncoder(w).Encode(user) | ||
} | ||
|
||
// GetUserHandler retrieves a user by ID | ||
func (h *UserHandler) GetUserHandler(w http.ResponseWriter, r *http.Request) { | ||
userID := r.URL.Query().Get("id") | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if userID == "" { | ||
http.Error(w, "User ID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
user, err := h.Repo.GetUserByID(userID) | ||
if err != nil { | ||
if err == repositories.ErrUserNotFound { | ||
http.Error(w, "User not found", http.StatusNotFound) | ||
} else { | ||
http.Error(w, "Failed to retrieve user", http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
json.NewEncoder(w).Encode(user) | ||
} | ||
|
||
// UpdateUserHandler updates user details | ||
func (h *UserHandler) UpdateUserHandler(w http.ResponseWriter, r *http.Request) { | ||
userID := r.URL.Query().Get("id") | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if userID == "" { | ||
http.Error(w, "User ID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var updates models.User | ||
if err := json.NewDecoder(r.Body).Decode(&updates); err != nil { | ||
http.Error(w, "Invalid request payload", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
user, err := h.Repo.UpdateUser(userID, &updates) | ||
if err != nil { | ||
if err == repositories.ErrUserNotFound { | ||
http.Error(w, "User not found", http.StatusNotFound) | ||
} else { | ||
http.Error(w, "Failed to update user", http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
json.NewEncoder(w).Encode(user) | ||
} | ||
|
||
// DeleteUserHandler deletes a user by ID | ||
func (h *UserHandler) DeleteUserHandler(w http.ResponseWriter, r *http.Request) { | ||
userID := r.URL.Query().Get("id") | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if userID == "" { | ||
http.Error(w, "User ID is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err := h.Repo.DeleteUser(userID); err != nil { | ||
if err == repositories.ErrUserNotFound { | ||
http.Error(w, "User not found", http.StatusNotFound) | ||
} else { | ||
http.Error(w, "Failed to delete user", http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package models | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
// User represents a registered user in the system. | ||
type User struct { | ||
gorm.Model | ||
Username string `gorm:"unique;not null" json:"username"` | ||
Email string `gorm:"unique;not null" json:"email"` | ||
} | ||
jayjay19630 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package repositories | ||
|
||
import ( | ||
"errors" | ||
"peerprep/user/internal/models" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
var ErrUserNotFound = errors.New("user not found") | ||
|
||
type UserRepository struct { | ||
DB *gorm.DB | ||
} | ||
|
||
func (r *UserRepository) CreateUser(user *models.User) error { | ||
return r.DB.Create(user).Error | ||
} | ||
|
||
func (r *UserRepository) GetUserByID(userID string) (*models.User, error) { | ||
var user models.User | ||
err := r.DB.First(&user, "user_id = ?", userID).Error | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, ErrUserNotFound | ||
} | ||
return &user, err | ||
} | ||
|
||
func (r *UserRepository) UpdateUser(userID string, updates *models.User) (*models.User, error) { | ||
var user models.User | ||
if err := r.DB.First(&user, "user_id = ?", userID).Error; err != nil { | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return nil, ErrUserNotFound | ||
} | ||
return nil, err | ||
} | ||
|
||
if err := r.DB.Model(&user).Updates(updates).Error; err != nil { | ||
return nil, err | ||
} | ||
return &user, nil | ||
} | ||
|
||
func (r *UserRepository) DeleteUser(userID string) error { | ||
result := r.DB.Delete(&models.User{}, "user_id = ?", userID) | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if result.RowsAffected == 0 { | ||
return ErrUserNotFound | ||
} | ||
return result.Error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package routers | ||
|
||
import ( | ||
handlers "peerprep/user/internal/handlers" | ||
|
||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
func UserRoutes(r *chi.Mux, userHandler *handlers.UserHandler) { | ||
r.Route("/api/v1/users", func(r chi.Router) { | ||
r.Post("/", userHandler.CreateUserHandler) // Create user | ||
r.Get("/", userHandler.GetUserHandler) // Get user by ID | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
r.Put("/", userHandler.UpdateUserHandler) // Update user | ||
r.Delete("/", userHandler.DeleteUserHandler) // Delete user | ||
jayjay19630 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.