Skip to content

Commit bc85ac2

Browse files
feat: implemented basic logic for UserService
UserService handles the operations on the databases.
1 parent d2a3845 commit bc85ac2

2 files changed

Lines changed: 326 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package meet_at_mensa.user.exception;
2+
3+
public class UserNotFoundException extends RuntimeException {
4+
5+
public UserNotFoundException() {
6+
super("User not found");
7+
}
8+
9+
public UserNotFoundException(String message) {
10+
super(message);
11+
}
12+
13+
public UserNotFoundException(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
}
Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
package meet_at_mensa.user.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
6+
import org.openapitools.model.User;
7+
import org.openapitools.model.UserNew;
8+
import org.openapitools.model.UserUpdate;
9+
10+
import meet_at_mensa.user.repository.*;
11+
import meet_at_mensa.user.exception.UserNotFoundException;
12+
import meet_at_mensa.user.model.*;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.UUID;
17+
18+
19+
@Service
20+
public class UserService {
21+
22+
// UserRepository manages the userdb database
23+
@Autowired
24+
private UserRepository userRepository;
25+
26+
// IntererstRepository manages the interests database
27+
@Autowired
28+
InterestRepository interestRepository;
29+
30+
31+
/**
32+
* Searches the database for a user with id {userID}
33+
*
34+
* Generates a User object from the database entity abstractions
35+
*
36+
* @param userID UUID of the user being searched for
37+
* @return User object if a user was found
38+
* @throws UserNotFoundException if no user with id {userID} is found
39+
*/
40+
public User getUser(UUID userID) {
41+
42+
// search the database for a user
43+
// throws a UserNotFoundException if no user is found
44+
UserEntity userEntity = userRepository.findById(userID)
45+
.orElseThrow(() -> new UserNotFoundException(String.format("User with ID %s not found", userID)));
46+
47+
// seach the database for a user's interests
48+
List<String> interests = getInterests(userID);
49+
50+
// construct new User object
51+
User user = new User(
52+
userID,
53+
userEntity.getEmail(), // email
54+
userEntity.getFirstname(), // firstname
55+
userEntity.getLastname(), // lastname
56+
userEntity.getBirthday(), // birthday
57+
userEntity.getGender(), // gender
58+
userEntity.getDegree(), // degree
59+
userEntity.getDegreeStart(), // degreeStart
60+
interests, // interests
61+
userEntity.getBio() // bio
62+
);
63+
64+
// return
65+
return user;
66+
}
67+
68+
/**
69+
* Creates database entries for a new user based on a UserNew object
70+
*
71+
* Generates UUIDs
72+
*
73+
* @param userNew UserNew object containing information about the new user
74+
* @return User object of the user that has been created
75+
*/
76+
public User registerUser(UserNew userNew) {
77+
78+
// construct new UserEntity object
79+
UserEntity userEntity = new UserEntity(
80+
userNew.getEmail(), // email
81+
userNew.getFirstname(), // firstname
82+
userNew.getLastname(), // lastname
83+
userNew.getBirthday(), // birthday
84+
userNew.getGender(), // gender
85+
userNew.getDegree(), // degree
86+
userNew.getDegreeStart(), // degreeStart
87+
userNew.getBio() // bio
88+
);
89+
90+
// save user entity to database
91+
userEntity = userRepository.save(userEntity);
92+
93+
// register interests
94+
registerInterests(userEntity.getUserID(), userNew.getInterests());
95+
96+
// fetch User object and return
97+
return getUser(userEntity.getUserID());
98+
}
99+
100+
/**
101+
* Updates the database entries for a user with id {UserID} based on a UserUpdate object.
102+
*
103+
* Keeps old value if corresponding value in UserUpdate is null.
104+
* Overwrites previously existing values otherwise.
105+
*
106+
* @param userID UUID of the user being updated
107+
* @return User object of the user that has been updated
108+
* @throws UserNotFoundException if no user with id {userID} is found
109+
*/
110+
public User updateUser(UUID userID, UserUpdate userUpdate) {
111+
112+
// search the database for a user
113+
// throws a UserNotFoundException if no user is found
114+
UserEntity userEntity = userRepository.findById(userID)
115+
.orElseThrow(() -> new UserNotFoundException(String.format("User with ID %s not found", userID)));
116+
117+
// update Email
118+
if (userUpdate.getEmail() != null){
119+
userEntity.setEmail(userUpdate.getEmail());
120+
}
121+
122+
// update Firstname
123+
if (userUpdate.getFirstname() != null){
124+
userEntity.setFirstname(userUpdate.getFirstname());
125+
}
126+
127+
// update Lastname
128+
if (userUpdate.getLastname() != null){
129+
userEntity.setLastname(userUpdate.getLastname());
130+
}
131+
132+
// update Birthday
133+
if (userUpdate.getBirthday() != null){
134+
userEntity.setBirthday(userUpdate.getBirthday());
135+
}
136+
137+
// update Gender
138+
if (userUpdate.getGender() != null){
139+
userEntity.setGender(userUpdate.getGender());
140+
}
141+
142+
// update Degree
143+
if (userUpdate.getDegree() != null){
144+
userEntity.setDegree(userUpdate.getDegree());
145+
}
146+
147+
// update DegreeStart
148+
if (userUpdate.getDegreeStart() != null){
149+
userEntity.setDegreeStart(userUpdate.getDegreeStart());
150+
}
151+
152+
// update Bio
153+
if (userUpdate.getBio() != null){
154+
userEntity.setBio(userUpdate.getBio());
155+
}
156+
157+
// save changes to the database
158+
userRepository.save(userEntity);
159+
160+
// update Interests
161+
if (userUpdate.getBio() != null){
162+
163+
// remove old interests
164+
deleteInterests(userID);
165+
166+
// add new interest list
167+
registerInterests(userID, userUpdate.getInterests());
168+
169+
}
170+
171+
// return User object fetched new from the database
172+
return getUser(userID);
173+
174+
}
175+
176+
/**
177+
* Removes all database entries for a user with id {UserID}.
178+
*
179+
*
180+
* @param userID UUID of the user being deleted
181+
* @throws UserNotFoundException if no user with id {userID} is found
182+
*/
183+
public void deleteUser(UUID userID) {
184+
185+
// Check that user exists
186+
// throws a UserNotFoundException if no user is found
187+
userRepository.findById(userID)
188+
.orElseThrow(() -> new UserNotFoundException(String.format("User with ID %s not found", userID)));
189+
190+
// delete userEntity from database
191+
userRepository.deleteById(userID);
192+
193+
// delete corresponding interests
194+
deleteInterests(userID);
195+
}
196+
197+
/**
198+
* Searches the interests database for all interests corresponding to user with id {userID}
199+
*
200+
* Generates a list of Strings to return
201+
*
202+
* @param userID UUID of the user who's interests are being searched for
203+
* @return List of interests corresponding to that user
204+
*/
205+
private List<String> getInterests(UUID userID) {
206+
207+
// search the database for interestEntity entries
208+
Iterable<InterestEntity> interestEntities = interestRepository.findByUserID(userID);
209+
210+
// create an empty list to store interests
211+
List<String> interests = new ArrayList<>();
212+
213+
// extract string values into new list
214+
for (InterestEntity interestEntity : interestEntities) {
215+
interests.add(interestEntity.getInterest());
216+
}
217+
218+
// return
219+
return interests;
220+
}
221+
222+
/**
223+
* Deletes all entries from interests database for user with id {userID}
224+
*
225+
* @param userID UUID of the user who's interests are being deleted
226+
*/
227+
private void deleteInterests(UUID userID) {
228+
229+
// search the database for interestEntity entries
230+
Iterable<InterestEntity> interestEntities = interestRepository.findByUserID(userID);
231+
232+
// delete all entries from list
233+
for (InterestEntity interestEntity : interestEntities) {
234+
interestRepository.deleteById(interestEntity.getListID());
235+
}
236+
237+
}
238+
239+
/**
240+
* Creates database entries for interests corresponding to a user with id {userID}
241+
*
242+
* Deletes all previous entries for that user and generates new ones
243+
*
244+
* @param userID UUID of the user being searched for
245+
* @param interests List<String> String values of the user's interests
246+
* @return The list of interests corresponding to the user after registering
247+
*/
248+
private List<String> registerInterests(UUID userID, List<String> interests) {
249+
250+
// for each interest
251+
for (String interest : interests) {
252+
253+
// create an entity object and add it to the database
254+
interestRepository.save(new InterestEntity(userID, interest));
255+
}
256+
257+
// return the interests for this userID
258+
return getInterests(userID);
259+
260+
}
261+
262+
/**
263+
* DEBUG METHOD ONLY. DO NOT USE IN PRODUCTION
264+
*
265+
* Generate a list of all UserEntity objects managed by the databases
266+
*
267+
* @return List of all UserEntity objects in the database
268+
*/
269+
public Iterable<UserEntity> debugGetAllUserEntities() {
270+
return userRepository.findAll();
271+
}
272+
273+
/**
274+
* DEBUG METHOD ONLY. DO NOT USE IN PRODUCTION
275+
*
276+
* Generate a list of all InterestEntity objects managed by the databases
277+
*
278+
* @return List of all InterestEntity objects in the database
279+
*/
280+
public Iterable<InterestEntity> debugGetAllInterestEntities() {
281+
return interestRepository.findAll();
282+
}
283+
284+
/**
285+
* DEBUG METHOD ONLY. DO NOT USE IN PRODUCTION
286+
*
287+
* Generate a list of all User Objects managed by the database
288+
*
289+
* @return List of all User objects in the database
290+
*/
291+
public Iterable<User> debugGetAllUsers() {
292+
293+
// get list of userentities
294+
Iterable<UserEntity> allUserEntities = userRepository.findAll();
295+
296+
List<User> users = new ArrayList<>();
297+
298+
// generate User objects for each
299+
for (UserEntity userEntity : allUserEntities) {
300+
users.add(
301+
getUser(userEntity.getUserID())
302+
);
303+
}
304+
305+
return users;
306+
307+
}
308+
309+
}

0 commit comments

Comments
 (0)