-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathUserService.java
47 lines (40 loc) · 1.65 KB
/
UserService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.mayank.fooddelivery.services;
import com.mayank.fooddelivery.datastore.UserData;
import com.mayank.fooddelivery.exceptions.ExceptionType;
import com.mayank.fooddelivery.exceptions.FoodDeliveryException;
import com.mayank.fooddelivery.model.User;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
private UserData userData;
@Autowired
public UserService(UserData userData) {
this.userData = userData;
}
public void addUser(@NonNull final User user) {
if (userData.getUserById().containsKey(user.getId())) {
throw new FoodDeliveryException(ExceptionType.USER_ALREADY_EXISTS, "userId already exists");
}
userData.getUserById().put(user.getId(), user);
}
public void deleteUser(@NonNull final String userId) {
if (!userData.getUserById().containsKey(userId)) {
throw new FoodDeliveryException(ExceptionType.USER_NOT_FOUND, "user not found");
}
userData.getUserById().remove(userId);
}
public void updateUser(@NonNull final User user) {
if (!userData.getUserById().containsKey(user.getId())) {
throw new FoodDeliveryException(ExceptionType.USER_NOT_FOUND, "user not found");
}
userData.getUserById().put(user.getId(), user);
}
public User getUser(@NonNull final String userId) {
if (!userData.getUserById().containsKey(userId)) {
throw new FoodDeliveryException(ExceptionType.USER_NOT_FOUND, "user not found");
}
return userData.getUserById().get(userId);
}
}