first commit #7

Closed
yurchik-k0028 wants to merge 3 commits from (deleted):main into main
4 changed files with 89 additions and 59 deletions
Showing only changes of commit 358c0996bb - Show all commits

View File

@@ -1,35 +1,65 @@
package com.example.nto.entity; package com.example.nto.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate; import java.time.LocalDate;
/**
* Booking entity
*/
@Entity @Entity
@Table(name = "booking") @Table(name = "booking")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Booking { public class Booking {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private Long id;
@Column(nullable = false)
private LocalDate date; private LocalDate date;
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "place_id") @JoinColumn(name = "place_id", nullable = false)
private Place place; private Place place;
@ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id") @JoinColumn(name = "employee_id", nullable = false)
private Employee employee; private Employee employee;
public Booking() {
}
public Booking(LocalDate date, Place place, Employee employee) {
this.date = date;
this.place = place;
this.employee = employee;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
} }

View File

@@ -3,14 +3,16 @@ package com.example.nto.repository;
import com.example.nto.entity.Booking; import com.example.nto.entity.Booking;
import com.example.nto.entity.Place; import com.example.nto.entity.Place;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Optional;
@Repository
public interface BookingRepository extends JpaRepository<Booking, Long> { public interface BookingRepository extends JpaRepository<Booking, Long> {
List<Booking> findByDate(LocalDate date); List<Booking> findByDate(LocalDate date);
List<Booking> findByPlaceAndDate(Place place, LocalDate date);
List<Booking> findByEmployeeId(long employeeId); Optional<Booking> findByPlaceAndDate(Place place, LocalDate date);
List<Booking> findByEmployeeId(Long employeeId);
} }

View File

@@ -1,6 +1,5 @@
package com.example.nto.service; package com.example.nto.service;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Place; import com.example.nto.entity.Place;
import java.time.LocalDate; import java.time.LocalDate;
@@ -8,7 +7,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public interface BookingService { public interface BookingService {
Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate fromInclusive, int days);
Booking createBooking(long employeeId, long placeId, LocalDate date) throws IllegalStateException; Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate start, int days);
List<Booking> getBookingsForEmployee(long employeeId);
void createBooking(String code, LocalDate date, Long placeId);
} }

View File

@@ -1,14 +1,13 @@
package com.example.nto.service.impl; package com.example.nto.service.impl;
import com.example.nto.entity.Booking; import com.example.nto.entity.Booking;
import com.example.nto.entity.Place;
import com.example.nto.entity.Employee; import com.example.nto.entity.Employee;
import com.example.nto.entity.Place;
import com.example.nto.repository.BookingRepository; import com.example.nto.repository.BookingRepository;
import com.example.nto.repository.PlaceRepository;
import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.EmployeeRepository;
import com.example.nto.repository.PlaceRepository;
import com.example.nto.service.BookingService; import com.example.nto.service.BookingService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.*; import java.util.*;
@@ -21,56 +20,55 @@ public class BookingServiceImpl implements BookingService {
private final PlaceRepository placeRepository; private final PlaceRepository placeRepository;
private final EmployeeRepository employeeRepository; private final EmployeeRepository employeeRepository;
public BookingServiceImpl(BookingRepository bookingRepository, public BookingServiceImpl(
BookingRepository bookingRepository,
PlaceRepository placeRepository, PlaceRepository placeRepository,
EmployeeRepository employeeRepository) { EmployeeRepository employeeRepository
) {
this.bookingRepository = bookingRepository; this.bookingRepository = bookingRepository;
this.placeRepository = placeRepository; this.placeRepository = placeRepository;
this.employeeRepository = employeeRepository; this.employeeRepository = employeeRepository;
} }
@Override @Override
public Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate fromInclusive, int days) { public Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate start, int days) {
List<Place> allPlaces = placeRepository.findAll();
Map<LocalDate, List<Place>> result = new LinkedHashMap<>(); Map<LocalDate, List<Place>> result = new LinkedHashMap<>();
List<Place> allPlaces = placeRepository.findAll();
for (int i = 0; i < days; i++) { for (int i = 0; i < days; i++) {
LocalDate date = fromInclusive.plusDays(i); LocalDate date = start.plusDays(i);
List<Long> bookedPlaceIds = bookingRepository.findByDate(date).stream()
.map(b -> b.getPlace().getId()).collect(Collectors.toList()); List<Booking> booked = bookingRepository.findByDate(date);
Set<Long> bookedIds = booked.stream()
.map(b -> b.getPlace().getId())
.collect(Collectors.toSet());
List<Place> free = allPlaces.stream() List<Place> free = allPlaces.stream()
.filter(p -> !bookedPlaceIds.contains(p.getId())) .filter(p -> !bookedIds.contains(p.getId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
result.put(date, free); result.put(date, free);
} }
return result; return result;
} }
@Override @Override
@Transactional public void createBooking(String code, LocalDate date, Long placeId) {
public Booking createBooking(long employeeId, long placeId, LocalDate date) throws IllegalStateException {
// check employee
Optional<Employee> employeeOpt = employeeRepository.findById(employeeId);
if (employeeOpt.isEmpty()) throw new IllegalStateException("Employee not found");
Optional<Place> placeOpt = placeRepository.findById(placeId); Employee employee = employeeRepository.findByCode(code)
if (placeOpt.isEmpty()) throw new IllegalStateException("Place not found"); .orElseThrow(() -> new IllegalArgumentException("Employee not found"));
// check if already booked Place place = placeRepository.findById(placeId)
List<Booking> exists = bookingRepository.findByPlaceAndDate(placeOpt.get(), date); .orElseThrow(() -> new IllegalArgumentException("Place not found"));
if (!exists.isEmpty()) {
bookingRepository.findByPlaceAndDate(place, date)
.ifPresent(b -> {
throw new IllegalStateException("Already booked"); throw new IllegalStateException("Already booked");
} });
Booking booking = Booking.builder() Booking booking = new Booking(date, place, employee);
.date(date)
.employee(employeeOpt.get())
.place(placeOpt.get())
.build();
return bookingRepository.save(booking);
}
@Override bookingRepository.save(booking);
public List<Booking> getBookingsForEmployee(long employeeId) {
return bookingRepository.findByEmployeeId(employeeId);
} }
} }