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