This commit is contained in:
@@ -5,6 +5,7 @@ import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.repository.PlaceRepository;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
@@ -25,6 +26,8 @@ public class BookingServiceImpl implements BookingService {
|
||||
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> findAvailableBookings(String employeeCode) {
|
||||
@@ -57,7 +60,49 @@ public class BookingServiceImpl implements BookingService {
|
||||
}
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
public Map<String, Object> createBooking(String code, Long placeId, String date) {
|
||||
|
||||
if (code == null || code.isBlank()) {
|
||||
throw new IllegalArgumentException("Employee code is empty");
|
||||
}
|
||||
|
||||
if (placeId == null || date == null || date.isBlank()) {
|
||||
throw new IllegalArgumentException("Place ID and date must be provided");
|
||||
}
|
||||
|
||||
Employee employee = employeeRepository.findByCode(code)
|
||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee not found"));
|
||||
|
||||
Place place = placeRepository.findById(placeId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Place not found"));
|
||||
|
||||
LocalDate bookingDate;
|
||||
try {
|
||||
bookingDate = LocalDate.parse(date);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Invalid date format. Use yyyy-MM-dd");
|
||||
}
|
||||
boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate)
|
||||
.stream()
|
||||
.anyMatch(b -> b.getPlace().getId() == placeId);
|
||||
if (exists) {
|
||||
throw new IllegalArgumentException("Place already booked for this date");
|
||||
}
|
||||
Booking booking = new Booking();
|
||||
booking.setEmployee((Map<String, Object>) employee);
|
||||
booking.setPlace(place);
|
||||
booking.setDate(bookingDate);
|
||||
bookingRepository.save(booking);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("id", booking.getId());
|
||||
result.put("date", booking.getDate());
|
||||
result.put("placeId", place.getId());
|
||||
result.put("employeeId", employee.getId());
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user