|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "peerprep/user/internal/models" |
| 7 | + "peerprep/user/internal/repositories" |
| 8 | +) |
| 9 | + |
| 10 | +type UserHandler struct { |
| 11 | + Repo *repositories.UserRepository |
| 12 | +} |
| 13 | + |
| 14 | +// CreateUserHandler handles user creation |
| 15 | +func (h *UserHandler) CreateUserHandler(w http.ResponseWriter, r *http.Request) { |
| 16 | + var user models.User |
| 17 | + if err := json.NewDecoder(r.Body).Decode(&user); err != nil { |
| 18 | + http.Error(w, "Invalid request payload", http.StatusBadRequest) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Save user using the repository |
| 23 | + if err := h.Repo.CreateUser(&user); err != nil { |
| 24 | + http.Error(w, "Failed to create user", http.StatusInternalServerError) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + w.WriteHeader(http.StatusCreated) |
| 29 | + json.NewEncoder(w).Encode(user) |
| 30 | +} |
| 31 | + |
| 32 | +// GetUserHandler retrieves a user by ID |
| 33 | +func (h *UserHandler) GetUserHandler(w http.ResponseWriter, r *http.Request) { |
| 34 | + userID := r.URL.Query().Get("id") |
| 35 | + if userID == "" { |
| 36 | + http.Error(w, "User ID is required", http.StatusBadRequest) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + user, err := h.Repo.GetUserByID(userID) |
| 41 | + if err != nil { |
| 42 | + if err == repositories.ErrUserNotFound { |
| 43 | + http.Error(w, "User not found", http.StatusNotFound) |
| 44 | + } else { |
| 45 | + http.Error(w, "Failed to retrieve user", http.StatusInternalServerError) |
| 46 | + } |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + json.NewEncoder(w).Encode(user) |
| 51 | +} |
| 52 | + |
| 53 | +// UpdateUserHandler updates user details |
| 54 | +func (h *UserHandler) UpdateUserHandler(w http.ResponseWriter, r *http.Request) { |
| 55 | + userID := r.URL.Query().Get("id") |
| 56 | + if userID == "" { |
| 57 | + http.Error(w, "User ID is required", http.StatusBadRequest) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + var updates models.User |
| 62 | + if err := json.NewDecoder(r.Body).Decode(&updates); err != nil { |
| 63 | + http.Error(w, "Invalid request payload", http.StatusBadRequest) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + user, err := h.Repo.UpdateUser(userID, &updates) |
| 68 | + if err != nil { |
| 69 | + if err == repositories.ErrUserNotFound { |
| 70 | + http.Error(w, "User not found", http.StatusNotFound) |
| 71 | + } else { |
| 72 | + http.Error(w, "Failed to update user", http.StatusInternalServerError) |
| 73 | + } |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + json.NewEncoder(w).Encode(user) |
| 78 | +} |
| 79 | + |
| 80 | +// DeleteUserHandler deletes a user by ID |
| 81 | +func (h *UserHandler) DeleteUserHandler(w http.ResponseWriter, r *http.Request) { |
| 82 | + userID := r.URL.Query().Get("id") |
| 83 | + if userID == "" { |
| 84 | + http.Error(w, "User ID is required", http.StatusBadRequest) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + if err := h.Repo.DeleteUser(userID); err != nil { |
| 89 | + if err == repositories.ErrUserNotFound { |
| 90 | + http.Error(w, "User not found", http.StatusNotFound) |
| 91 | + } else { |
| 92 | + http.Error(w, "Failed to delete user", http.StatusInternalServerError) |
| 93 | + } |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + w.WriteHeader(http.StatusNoContent) |
| 98 | +} |
0 commit comments