Skip to content

Commit 3494b7e

Browse files
author
John Wang
committed
add functions to UserController
1 parent 413eee9 commit 3494b7e

File tree

3 files changed

+72
-220
lines changed

3 files changed

+72
-220
lines changed

src/main/java/com/example/demo/controller/StoreController.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88

99
import com.example.demo.DemoApplication;
1010
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.http.ResponseEntity;
1211
import org.springframework.web.bind.annotation.PathVariable;
13-
import org.springframework.web.bind.annotation.RequestBody;
1412
import org.springframework.web.bind.annotation.RequestMapping;
1513
import org.springframework.web.bind.annotation.RequestMethod;
1614
import org.springframework.web.bind.annotation.RequestParam;
1715

18-
import org.springframework.web.bind.annotation.ResponseBody;
16+
1917
import org.springframework.web.bind.annotation.RestController;
20-
import org.springframework.web.servlet.ModelAndView;
2118
import com.example.demo.data.Geolocation;
22-
import com.example.demo.data.Product;
2319
import com.example.demo.data.Store;
2420
import com.example.demo.data.provider.StoreManager;
2521

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.example.demo.controller;
22

3-
43
import java.util.Date;
54
import java.util.List;
65

@@ -9,7 +8,7 @@
98
import org.bson.types.ObjectId;
109
import org.springframework.beans.factory.annotation.Autowired;
1110
import org.springframework.web.bind.annotation.CrossOrigin;
12-
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.PathVariable;
1312
import org.springframework.web.bind.annotation.RequestMapping;
1413
import org.springframework.web.bind.annotation.RequestMethod;
1514
import org.springframework.web.bind.annotation.RequestParam;
@@ -37,85 +36,95 @@ public class UserController {
3736
private UserManager userManager;
3837
@Autowired
3938
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+
4881
@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 {
6289

6390
Geolocation geolocation = new Geolocation();
6491
geolocation.setLatitude(latitude);
6592
geolocation.setLongitude(longitude);
6693
ObjectId store_id = ObjectId.get();
6794
Store store = new Store(store_id, name, pictureFileName, geolocation, address, zipcode, city, state);
6895
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+
7798
userManager.addUser(user);
7899
return login(user.getUsername(), user.getPassword());
79100
}
80-
81-
101+
82102
@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 {
96110

97111
Geolocation geolocation = new Geolocation();
98112
geolocation.setLatitude(latitude);
99113
geolocation.setLongitude(longitude);
100114
ObjectId store_id = ObjectId.get();
101115
Store store = new Store(store_id, name, pictureFileName, geolocation, address, zipcode, city, state);
102116
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);
111119
}
112-
120+
113121
@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 {
116125
String jwtToken = "";
117-
118-
if ( username == null || password== null) {
126+
127+
if (username == null || password == null) {
119128
throw new ServletException("Please fill in username and password");
120129
}
121130

@@ -131,5 +140,5 @@ public String login(@RequestParam("username") final String username,
131140

132141
return jwtToken;
133142
}
134-
143+
135144
}
Lines changed: 2 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,16 @@
11
package com.example.demo.controller;
22

3-
import java.io.FileWriter;
4-
import java.io.IOException;
5-
import java.io.Writer;
6-
import java.util.ArrayList;
7-
import java.util.Date;
8-
import java.util.HashMap;
9-
import java.util.List;
10-
import java.util.Map;
11-
import java.util.Set;
12-
import java.net.InetAddress;
13-
14-
15-
import com.example.demo.DemoApplication;
16-
import org.springframework.beans.factory.annotation.Autowired;
17-
import org.springframework.http.ResponseEntity;
18-
import org.springframework.web.bind.annotation.PathVariable;
19-
import org.springframework.web.bind.annotation.RequestBody;
203
import org.springframework.web.bind.annotation.RequestMapping;
214
import org.springframework.web.bind.annotation.RequestMethod;
22-
import org.springframework.web.bind.annotation.RequestParam;
235

24-
import org.springframework.web.bind.annotation.ResponseBody;
256
import org.springframework.web.bind.annotation.RestController;
26-
import org.springframework.web.servlet.ModelAndView;
27-
28-
29-
import com.example.demo.data.Geolocation;
30-
import com.example.demo.data.Store;
31-
import com.example.demo.data.provider.StoreManager;
32-
33-
import org.bson.types.ObjectId;
34-
35-
36-
37-
38-
39-
/**
40-
* This is the controller used by Spring framework.
41-
* <p>
42-
* The basic function of this controller is to map
43-
* each HTTP API Path to the correspondent method.
44-
*
45-
*/
467

478
@RestController
489
public class WebController {
49-
50-
/**
51-
* When the class instance is annotated with
52-
* {@link Autowired}, it will be looking for the actual
53-
* instance from the defined beans.
54-
* <p>
55-
* In our project, all the beans are defined in
56-
* the {@link DemoApplication} class.
57-
*/
58-
// @Autowired
59-
//// private StoreRepository storeRepository;
60-
// @Autowired
61-
// private StoreManager storeManager;
62-
63-
10+
6411

6512
@RequestMapping(value = "/server/ping", method = RequestMethod.GET)
6613
String healthCheck() {
67-
// You can replace this with other string,
68-
// and run the application locally to check your changes
69-
// with the URL: http://localhost:5000/ if run local server
70-
// with the URL: http://[aws url]:5000/ if run on aws elastic beanstalk
7114
return "OK";
7215
}
7316
@RequestMapping(value = "/server/hello", method = RequestMethod.GET)
@@ -76,103 +19,7 @@ String getGreeting()
7619
return "Hello, world!";
7720
}
7821

79-
/**
80-
* This is a simple example of how to use a data manager
81-
* to retrieve the data and return it as an HTTP response.
82-
* <p>
83-
* Note, when it returns from the Spring, it will be
84-
* automatically converted to JSON format.
85-
* <p>
86-
* Try it in your web browser:
87-
* http://localhost:5000/john/store/store101
88-
*/
89-
// @RequestMapping(value = "/json/store/{storeId}", method = RequestMethod.GET)
90-
// Store getStore(@PathVariable("storeId") String id) {
91-
// //json file database method
92-
// Store store = storeManager.getStore(id);
93-
//
94-
// return store;
95-
// }
96-
97-
98-
/**
99-
* This is an example of sending an HTTP POST request to
100-
* update a store's information (or create the user if not
101-
* exists before).
102-
*
103-
* You can test this with a HTTP client by sending
104-
* http://localhost:5000/john/store/user101
105-
* name=store1 pictureFileName=file1 latitude=1.23 longitude=3.23
106-
*
107-
* Note, the URL will not work directly in browser, because
108-
* it is not a GET request. You need to use a tool such as
109-
* curl.
110-
*
111-
* @param id
112-
* @param name
113-
* @param pictureFileName
114-
* @param latitude
115-
* @param longitude
116-
* @return
117-
*/
118-
// @RequestMapping(value = "/json/store/{storeId}", method = RequestMethod.POST)
119-
// Store updateStore(
120-
// @PathVariable("storeId") String id,
121-
// @RequestParam("name") String name,
122-
// @RequestParam("pictureFileName") String pictureFileName,
123-
// @RequestParam("latitude") float latitude,
124-
// @RequestParam("longitude") float longitude){
125-
// Store store = new Store();
126-
// //ObjectId = require('mongodb').ObjectID;
127-
//
128-
// store.setString_id(id);;
129-
// //store.set_id(ObjectId.get());
130-
// store.setName(name);
131-
// store.setPictureFileName(pictureFileName);
132-
// Geolocation loc = new Geolocation();
133-
// loc.setLatitude(latitude);
134-
// loc.setLongitude(longitude);
135-
// store.setLocation(loc);
136-
//
137-
// //json file database method
138-
// storeManager.updateStore(store);
139-
//
140-
// return store;
141-
// }
142-
143-
144-
/**
145-
* This API deletes the store. It uses HTTP DELETE method.
146-
*
147-
* @param storeId
148-
*/
149-
// @RequestMapping(value = "/json/store/{storeId}", method = RequestMethod.DELETE)
150-
// void deleteStore(
151-
// @PathVariable("storeId") String id) {
152-
//
153-
// //json file database method
154-
// storeManager.deleteStore(id);
155-
// }
156-
157-
/**
158-
* This API lists all the stores in the current database.
159-
*
160-
* @return
161-
*/
162-
// @RequestMapping(value = "/json/store/list", method = RequestMethod.GET)
163-
// List<Store> listAllStores() {
164-
// //json file database method
165-
// return storeManager.listAllStores();
166-
// }
167-
168-
169-
170-
171-
172-
173-
174-
175-
22+
17623

17724

17825
}

0 commit comments

Comments
 (0)