diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 7a092de..d89fed4 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,8 +1,5 @@ package com.example.nto.controller; -import com.example.nto.entity.Booking; -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.PlaceRepository; @@ -12,9 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - -import java.time.LocalDate; -import java.util.LinkedHashMap; import java.util.Map; /** @@ -34,10 +28,12 @@ public class BookingController { private BookingRepository bookingRepository; @Autowired private EmployeeService employeeService; - @GetMapping("/{code}/booking") public ResponseEntity getAvailableBookings(@PathVariable String code) { try { + if (code == null || code.trim().isEmpty()) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Employee code cannot be null or empty"); + } Map response = bookingService.findAvailableBookings(code); return ResponseEntity.ok(response); } catch (IllegalArgumentException e) { @@ -48,61 +44,23 @@ public class BookingController { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); } } - @PostMapping("/{code}/book") public ResponseEntity createBooking( @PathVariable String code, @RequestBody Map body ) { try { - Long placeId = body.get("placeId") != null - ? Long.valueOf(body.get("placeId").toString()) - : null; - - String date = body.get("date") != null - ? body.get("date").toString() - : null; + Long placeId = body.get("placeId") != null ? Long.valueOf(body.get("placeId").toString()) : null; + String date = body.get("date") != null ? body.get("date").toString() : null; if (placeId == null || date == null || date.isEmpty()) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Place ID and date must be provided"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Place ID and date must be provided"); } - Employee employee = employeeService.getEmployeeByCode(code); - if (employee == null) { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Employee not found"); - } - Place place = placeRepository.findById(placeId).orElse(null); - if (place == null) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Place not found"); - } - LocalDate bookingDate; - try { - bookingDate = LocalDate.parse(date); - } catch (Exception ex) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Invalid date format. Use yyyy-MM-dd"); - } - boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate) - .stream() - .anyMatch(b -> b.getPlace().getId() == placeId); - if (exists) { - return ResponseEntity.status(HttpStatus.CONFLICT) - .body("Place already booked for this date"); - } - Booking booking = new Booking(); - booking.setEmployee(employee); - booking.setPlace(place); - booking.setDate(bookingDate); - bookingRepository.save(booking); - Map response = new LinkedHashMap<>(); - response.put("id", booking.getId()); - response.put("date", booking.getDate()); - response.put("placeId", place.getId()); - response.put("employeeId", employee.getId()); - return ResponseEntity.status(HttpStatus.CREATED).body(response); - } catch (java.lang.IllegalArgumentException e) { + String message = bookingService.createBooking(code, placeId, date); + return ResponseEntity.ok(message); + } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); } diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 0061eca..7f13184 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -1,7 +1,6 @@ package com.example.nto.repository; import com.example.nto.entity.Booking; -import com.example.nto.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -17,4 +16,5 @@ import java.util.List; @Repository public interface BookingRepository extends JpaRepository { List findByDateBetween(LocalDate start, LocalDate end); + List findByEmployeeId(long id); } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index c1c7d39..1d144b9 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -6,4 +6,5 @@ import java.util.Map; @Service public interface BookingService { Map findAvailableBookings(String employeeCode); + String createBooking(String code, Long placeId, String date); } diff --git a/src/main/java/com/example/nto/service/PlaceServise.java b/src/main/java/com/example/nto/service/PlaceServise.java index 782f312..53771b0 100644 --- a/src/main/java/com/example/nto/service/PlaceServise.java +++ b/src/main/java/com/example/nto/service/PlaceServise.java @@ -2,15 +2,13 @@ package com.example.nto.service; import com.example.nto.entity.Place; import com.example.nto.repository.PlaceRepository; import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PlaceServise { - private final PlaceRepository placeRepository; - public PlaceServise(PlaceRepository placeRepository) { - this.placeRepository = placeRepository; - } - + @Autowired + private PlaceRepository placeRepository; @PostConstruct public void Places() { if(placeRepository.count() == 0) { diff --git a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java index 5471c21..ae7a47d 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -17,18 +17,14 @@ import java.util.*; @Service public class BookingServiceImpl implements BookingService { - @Autowired private BookingRepository bookingRepository; - @Autowired private PlaceRepository placeRepository; - @Autowired private EmployeeService employeeService; @Autowired private EmployeeRepository employeeRepository; - @Override public Map findAvailableBookings(String employeeCode) { if (employeeCode == null || employeeCode.isEmpty()) { @@ -47,8 +43,7 @@ public class BookingServiceImpl implements BookingService { LocalDate date = today.plusDays(i); List> freePlaces = new ArrayList<>(); for (Place place : places) { - boolean isBooked = bookings.stream() - .anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId()); + boolean isBooked = bookings.stream().anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId()); if (!isBooked) { Map placeInfo = new LinkedHashMap<>(); placeInfo.put("id", place.getId()); @@ -60,31 +55,23 @@ public class BookingServiceImpl implements BookingService { } return Collections.unmodifiableMap(result); } - public Map createBooking(String code, Long placeId, String date) { + public String 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")); - + 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); + boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate).stream().anyMatch(b -> b.getPlace().getId() == placeId); if (exists) { throw new IllegalArgumentException("Place already booked for this date"); } @@ -98,8 +85,7 @@ public class BookingServiceImpl implements BookingService { result.put("date", booking.getDate()); result.put("placeId", place.getId()); result.put("employeeId", employee.getId()); - - return result; + return "the booking was created successfully"; } } diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java index 3fc0dce..00a30f9 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,13 +1,10 @@ package com.example.nto.service.impl; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; -import com.example.nto.repository.BookingRepository; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -22,13 +19,11 @@ import org.springframework.stereotype.Service; public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeRepository employeeRepository; - @Override public Employee getEmployeeByCode(String code) { if (code == null || code.isEmpty()) { throw new IllegalArgumentException("Employee code is required"); } - return employeeRepository.findByCode(code) - .orElseThrow(() -> new EmployeeNotFoundException("Employee not found")); + return employeeRepository.findByCode(code).orElseThrow(() -> new EmployeeNotFoundException("Employee not found")); } } \ No newline at end of file