1
1
package com .example .demo .controller ;
2
2
3
-
4
3
import java .util .Date ;
5
4
import java .util .List ;
6
5
9
8
import org .bson .types .ObjectId ;
10
9
import org .springframework .beans .factory .annotation .Autowired ;
11
10
import org .springframework .web .bind .annotation .CrossOrigin ;
12
- import org .springframework .web .bind .annotation .RequestBody ;
11
+ import org .springframework .web .bind .annotation .PathVariable ;
13
12
import org .springframework .web .bind .annotation .RequestMapping ;
14
13
import org .springframework .web .bind .annotation .RequestMethod ;
15
14
import org .springframework .web .bind .annotation .RequestParam ;
@@ -37,85 +36,95 @@ public class UserController {
37
36
private UserManager userManager ;
38
37
@ Autowired
39
38
private StoreManager storeManager ;
40
-
41
- @ RequestMapping (value = "/list" , method = RequestMethod .GET )
42
- List <User > listAllUsers () {
43
-
44
- return userManager .listAllUsers ();
45
- }
46
-
47
-
39
+
40
+ @ RequestMapping (value = "/list" , method = RequestMethod .GET )
41
+ List <User > listAllUsers () {
42
+ return userManager .listAllUsers ();
43
+ }
44
+
45
+ @ RequestMapping (value = "/{user_id}" , method = RequestMethod .POST )
46
+ User updateUser (@ RequestParam ("username" ) String username , @ RequestParam ("password" ) String password ,
47
+ @ RequestParam ("role" ) final String role , @ RequestParam ("contactNumber" ) String contactNumber ,
48
+ @ RequestParam ("store_id" ) String store_id , @ RequestParam ("email" ) String email )
49
+ throws UserExistedException {
50
+
51
+ User user = new User (ObjectId .get (), username , password , store_id , contactNumber , role , email );
52
+ userManager .updateUser (user );
53
+ return user ;
54
+ }
55
+
56
+ @ RequestMapping (value = "/{user_id}" , method = RequestMethod .DELETE )
57
+ void deleteUser (@ PathVariable ("user_id" ) ObjectId id ) throws UserDoesntExistedException {
58
+
59
+ userManager .deleteUser (id );
60
+ }
61
+
62
+ @ RequestMapping (value = "/{user_id}" , method = RequestMethod .GET )
63
+ User getUser (@ PathVariable ("{user_id}" ) ObjectId id ) throws UserDoesntExistedException {
64
+
65
+ User user = userManager .findUserById (id );
66
+ return user ;
67
+ }
68
+
69
+ @ RequestMapping (value = "/add/{username}" , method = RequestMethod .PUT )
70
+ User addUser (@ PathVariable ("username" ) String username , @ RequestParam ("password" ) String password ,
71
+ @ RequestParam ("role" ) final String role , @ RequestParam ("contactNumber" ) String contactNumber ,
72
+ @ RequestParam ("store_id" ) String store_id , @ RequestParam ("name" ) String name ,
73
+ @ RequestParam ("email" ) String email )
74
+ throws ServletException , UserExistedException , UserDoesntExistedException , UserPasswordMismatchedException {
75
+
76
+ User user = new User (ObjectId .get (), username , password , store_id , contactNumber , role , email );
77
+ userManager .addUser (user );
78
+ return user ;
79
+ }
80
+
48
81
@ RequestMapping (value = "/register/" , method = RequestMethod .PUT )
49
- String register (@ RequestParam ("username" ) String username ,
50
- @ RequestParam ("password" ) String password ,
51
- @ RequestParam ("role" ) final String role ,
52
- @ RequestParam ("contactNumber" ) String contactNumber ,
53
- @ RequestParam ("name" ) String name ,
54
- @ RequestParam ("pictureFileName" ) String pictureFileName ,
55
- @ RequestParam ("address" ) String address ,
56
- @ RequestParam ("zipcode" ) String zipcode ,
57
- @ RequestParam ("city" ) String city ,
58
- @ RequestParam ("state" ) String state ,
59
- @ RequestParam ("email" ) String email ,
60
- @ RequestParam ("latitude" ) float latitude ,
61
- @ RequestParam ("longitude" ) float longitude ) throws ServletException , UserExistedException , UserDoesntExistedException , UserPasswordMismatchedException {
82
+ String register (@ RequestParam ("username" ) String username , @ RequestParam ("password" ) String password ,
83
+ @ RequestParam ("role" ) final String role , @ RequestParam ("contactNumber" ) String contactNumber ,
84
+ @ RequestParam ("name" ) String name , @ RequestParam ("pictureFileName" ) String pictureFileName ,
85
+ @ RequestParam ("address" ) String address , @ RequestParam ("zipcode" ) String zipcode ,
86
+ @ RequestParam ("city" ) String city , @ RequestParam ("state" ) String state , @ RequestParam ("email" ) String email ,
87
+ @ RequestParam ("latitude" ) float latitude , @ RequestParam ("longitude" ) float longitude )
88
+ throws ServletException , UserExistedException , UserDoesntExistedException , UserPasswordMismatchedException {
62
89
63
90
Geolocation geolocation = new Geolocation ();
64
91
geolocation .setLatitude (latitude );
65
92
geolocation .setLongitude (longitude );
66
93
ObjectId store_id = ObjectId .get ();
67
94
Store store = new Store (store_id , name , pictureFileName , geolocation , address , zipcode , city , state );
68
95
storeManager .addStore (store );
69
- User user = new User (ObjectId .get (),
70
- username ,
71
- password ,
72
- store .get_id (),
73
- contactNumber ,
74
- role ,
75
- email );
76
-
96
+ User user = new User (ObjectId .get (), username , password , store .get_id (), contactNumber , role , email );
97
+
77
98
userManager .addUser (user );
78
99
return login (user .getUsername (), user .getPassword ());
79
100
}
80
-
81
-
101
+
82
102
@ RequestMapping (value = "/add/" , method = RequestMethod .PUT )
83
- void addUser (@ RequestParam ("username" ) String username ,
84
- @ RequestParam ("password" ) String password ,
85
- @ RequestParam ("role" ) final String role ,
86
- @ RequestParam ("contactNumber" ) String contactNumber ,
87
- @ RequestParam ("name" ) String name ,
88
- @ RequestParam ("pictureFileName" ) String pictureFileName ,
89
- @ RequestParam ("address" ) String address ,
90
- @ RequestParam ("zipcode" ) String zipcode ,
91
- @ RequestParam ("city" ) String city ,
92
- @ RequestParam ("state" ) String state ,
93
- @ RequestParam ("email" ) String email ,
94
- @ RequestParam ("latitude" ) float latitude ,
95
- @ RequestParam ("longitude" ) float longitude ) throws UserExistedException {
103
+ void addUser (@ RequestParam ("username" ) String username , @ RequestParam ("password" ) String password ,
104
+ @ RequestParam ("role" ) final String role , @ RequestParam ("contactNumber" ) String contactNumber ,
105
+ @ RequestParam ("name" ) String name , @ RequestParam ("pictureFileName" ) String pictureFileName ,
106
+ @ RequestParam ("address" ) String address , @ RequestParam ("zipcode" ) String zipcode ,
107
+ @ RequestParam ("city" ) String city , @ RequestParam ("state" ) String state , @ RequestParam ("email" ) String email ,
108
+ @ RequestParam ("latitude" ) float latitude , @ RequestParam ("longitude" ) float longitude )
109
+ throws UserExistedException {
96
110
97
111
Geolocation geolocation = new Geolocation ();
98
112
geolocation .setLatitude (latitude );
99
113
geolocation .setLongitude (longitude );
100
114
ObjectId store_id = ObjectId .get ();
101
115
Store store = new Store (store_id , name , pictureFileName , geolocation , address , zipcode , city , state );
102
116
storeManager .addStore (store );
103
- User user = new User (ObjectId .get (),
104
- username ,
105
- password ,
106
- store .get_id (),
107
- contactNumber ,
108
- role ,
109
- email );
110
- userManager .addUser (user );
117
+ User user = new User (ObjectId .get (), username , password , store .get_id (), contactNumber , role , email );
118
+ userManager .addUser (user );
111
119
}
112
-
120
+
113
121
@ RequestMapping (value = "/login" , method = RequestMethod .POST )
114
- public String login (@ RequestParam ("username" ) final String username ,
115
- @ RequestParam ("password" ) final String password ) throws ServletException , UserDoesntExistedException , UserPasswordMismatchedException {
122
+ public String login (@ RequestParam ("username" ) final String username ,
123
+ @ RequestParam ("password" ) final String password )
124
+ throws ServletException , UserDoesntExistedException , UserPasswordMismatchedException {
116
125
String jwtToken = "" ;
117
-
118
- if ( username == null || password == null ) {
126
+
127
+ if (username == null || password == null ) {
119
128
throw new ServletException ("Please fill in username and password" );
120
129
}
121
130
@@ -131,5 +140,5 @@ public String login(@RequestParam("username") final String username,
131
140
132
141
return jwtToken ;
133
142
}
134
-
143
+
135
144
}
0 commit comments