Skip to content
Snippets Groups Projects
Commit d71145fd authored by Mathilde LORRAIN's avatar Mathilde LORRAIN
Browse files

feat: add custom exception

parent 69226445
No related branches found
No related tags found
No related merge requests found
......@@ -16,9 +16,8 @@ public class TravelDao {
addTravelsToMap();
}
public Travel persist(Travel travel) {
public void save(Travel travel) {
travels.put(travel.getId(), travel);
return travel;
}
public Travel update(Travel travel) {
......
......@@ -13,9 +13,8 @@ public class UserDao {
private final Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
public User persist(User user) {
public void save(User user) {
users.put(user.getId(), user);
return user;
}
public User update(User user) {
......
package io.takima.agencymanagement.exception;
public class AlreadyTravelSubscribedException extends Exception {
public AlreadyTravelSubscribedException(String message) {
super(message);
}
}
\ No newline at end of file
......@@ -110,4 +110,8 @@ public class Travel {
return stringBuilder.toString();
}
public boolean hasAvailableCapacity(Set<User> participants, int capacity) {
return participants.size() < capacity;
}
}
package io.takima.agencymanagement.service;
import io.takima.agencymanagement.dao.TravelDao;
import io.takima.agencymanagement.exception.AlreadyTravelSubscribedException;
import io.takima.agencymanagement.mapper.TravelDtoMapper;
import io.takima.agencymanagement.model.Travel;
import io.takima.agencymanagement.model.User;
......@@ -24,17 +25,17 @@ public class TravelService {
}
public TravelResponseDto create(Travel travel) {
travelDao.persist(travel);
travelDao.save(travel);
return fromTravel(travel);
}
public TravelResponseDto update(Travel travel) {
travelDao.persist(travel);
travelDao.save(travel);
return fromTravel(travel);
}
public TravelResponseDto delete(Travel travel) {
travelDao.persist(travel);
travelDao.save(travel);
return fromTravel(travel);
}
......@@ -49,6 +50,7 @@ public class TravelService {
.toList();
}
public void applyDiscounts(Travel travel) {
if (travel.getDiscounts().isEmpty()) {
System.out.println("Aucune réduction n'est applicable sur ce voyage !");
......@@ -61,19 +63,18 @@ public class TravelService {
}
}
public boolean subscribe(User user, Travel travel) throws Exception {
public void subscribe(User user, Travel travel) throws Exception {
if (travel.getParticipants().contains(user) || travel.getWaitList().contains(user)) {
//User already subscribed or in waitList
throw new Exception("L'utilisateur est déjà inscrit ou en file d'attente pour ce voyage.");
throw new AlreadyTravelSubscribedException("L'utilisateur est déjà inscrit ou en file d'attente pour ce voyage.");
}
if (travel.getParticipants().size() < travel.getCapacity()) {
travel.getParticipants().add(user);
return true;
return;
}
travel.getWaitList().add(user);
return false;
}
public void unsubscribe(User user, Travel travel) {
......@@ -106,10 +107,11 @@ public class TravelService {
List<Travel> travels = travelDao.findAll();
return travels.stream()
.filter(travel -> travel.getCapacity() > travel.getParticipants().size() && travel.getDestination().equals(destination))
.filter(travel -> travel.hasAvailableCapacity(travel.getParticipants(), travel.getCapacity()) && travel.getDestination().equals(destination))
.toList();
}
public List<Travel> findInPriceRange(double minPrice, double maxPrice) {
List<Travel> travels = travelDao.findAll();
......
......@@ -14,17 +14,17 @@ public class UserService {
UserDao userDao = new UserDao();
public UserResponseDto create(User user) {
userDao.persist(user);
userDao.save(user);
return fromUser(user);
}
public UserResponseDto update(User user) {
userDao.persist(user);
userDao.save(user);
return fromUser(user);
}
public UserResponseDto delete(User user) {
userDao.persist(user);
userDao.save(user);
return fromUser(user);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment