first commit
Some checks failed
Android Test / validate-and-test (pull_request) Has been cancelled

This commit is contained in:
Yurchik-gitter
2025-12-03 18:47:49 +03:00
parent 776400306f
commit 358c0996bb
4 changed files with 89 additions and 59 deletions

View File

@@ -1,35 +1,65 @@
package com.example.nto.entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
/**
* Booking entity
*/
@Entity
@Table(name = "booking")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Long id;
@Column(nullable = false)
private LocalDate date;
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
@JoinColumn(name = "place_id")
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "place_id", nullable = false)
private Place place;
@ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id", nullable = false)
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.Place;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
@Repository
public interface BookingRepository extends JpaRepository<Booking, Long> {
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;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Place;
import java.time.LocalDate;
@@ -8,7 +7,8 @@ import java.util.List;
import java.util.Map;
public interface BookingService {
Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate fromInclusive, int days);
Booking createBooking(long employeeId, long placeId, LocalDate date) throws IllegalStateException;
List<Booking> getBookingsForEmployee(long employeeId);
Map<LocalDate, List<Place>> getAvailablePlacesForRange(LocalDate start, int days);
void createBooking(String code, LocalDate date, Long placeId);
}

View File

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