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