1234
This commit is contained in:
@@ -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<String, Object> 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<String, Object> 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<String, Object> 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");
|
||||
}
|
||||
|
||||
@@ -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<Booking, Long> {
|
||||
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
||||
List<Booking> findByEmployeeId(long id);
|
||||
}
|
||||
|
||||
@@ -6,4 +6,5 @@ import java.util.Map;
|
||||
@Service
|
||||
public interface BookingService {
|
||||
Map<String, Object> findAvailableBookings(String employeeCode);
|
||||
String createBooking(String code, Long placeId, String date);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<String, Object> findAvailableBookings(String employeeCode) {
|
||||
if (employeeCode == null || employeeCode.isEmpty()) {
|
||||
@@ -47,8 +43,7 @@ public class BookingServiceImpl implements BookingService {
|
||||
LocalDate date = today.plusDays(i);
|
||||
List<Map<String, Object>> 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<String, Object> placeInfo = new LinkedHashMap<>();
|
||||
placeInfo.put("id", place.getId());
|
||||
@@ -60,31 +55,23 @@ public class BookingServiceImpl implements BookingService {
|
||||
}
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
public Map<String, Object> 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";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user