Format code and add automatic formatting with spotless
This commit is contained in:
parent
868e98d65d
commit
264b1e07ec
12
pom.xml
12
pom.xml
@ -37,6 +37,18 @@
|
|||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.diffplug.spotless</groupId>
|
||||||
|
<artifactId>spotless-maven-plugin</artifactId>
|
||||||
|
<version>2.43.0</version>
|
||||||
|
<configuration>
|
||||||
|
<java>
|
||||||
|
<googleJavaFormat>
|
||||||
|
<style>AOSP</style>
|
||||||
|
</googleJavaFormat>
|
||||||
|
</java>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|||||||
@ -3,15 +3,14 @@ package com.example.nto.controller;
|
|||||||
import com.example.nto.controller.dto.BookingCreateDto;
|
import com.example.nto.controller.dto.BookingCreateDto;
|
||||||
import com.example.nto.controller.dto.PlaceDto;
|
import com.example.nto.controller.dto.PlaceDto;
|
||||||
import com.example.nto.service.BookingService;
|
import com.example.nto.service.BookingService;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Validated
|
@Validated
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("api")
|
@RequestMapping("api")
|
||||||
@ -31,5 +30,4 @@ public class BookingController {
|
|||||||
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto) {
|
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto) {
|
||||||
bookingService.create(code, bookingCreateDto);
|
bookingService.create(code, bookingCreateDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -25,5 +24,4 @@ public class EmployeeController {
|
|||||||
public EmployeeDto getByCode(@PathVariable String code) {
|
public EmployeeDto getByCode(@PathVariable String code) {
|
||||||
return employeeService.getByCode(code);
|
return employeeService.getByCode(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,20 +2,17 @@ package com.example.nto.controller.dto;
|
|||||||
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
import jakarta.validation.constraints.Positive;
|
import jakarta.validation.constraints.Positive;
|
||||||
|
import java.time.LocalDate;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class BookingCreateDto {
|
public class BookingCreateDto {
|
||||||
@NotNull
|
@NotNull private LocalDate date;
|
||||||
private LocalDate date;
|
@Positive private long placeId;
|
||||||
@Positive
|
|
||||||
private long placeId;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,15 +2,14 @@ package com.example.nto.controller.dto;
|
|||||||
|
|
||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import java.time.LocalDate;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@Builder
|
@Builder
|
||||||
|
|||||||
@ -1,13 +1,12 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@Builder
|
@Builder
|
||||||
|
|||||||
@ -16,7 +16,8 @@ public class GlobalExceptionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(BookingAlreadyExistsException.class)
|
@ExceptionHandler(BookingAlreadyExistsException.class)
|
||||||
public ResponseEntity<String> handleBookingAlreadyExistsException(BookingAlreadyExistsException e) {
|
public ResponseEntity<String> handleBookingAlreadyExistsException(
|
||||||
|
BookingAlreadyExistsException e) {
|
||||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,11 +3,10 @@ package com.example.nto.repository;
|
|||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.Place;
|
import com.example.nto.entity.Place;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
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);
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import java.util.Optional;
|
||||||
import org.springframework.data.jpa.repository.EntityGraph;
|
import org.springframework.data.jpa.repository.EntityGraph;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
@EntityGraph(attributePaths = {"bookingList", "bookingList.place"})
|
@EntityGraph(attributePaths = {"bookingList", "bookingList.place"})
|
||||||
Optional<Employee> findByCode(String code);
|
Optional<Employee> findByCode(String code);
|
||||||
|
|||||||
@ -3,5 +3,4 @@ package com.example.nto.repository;
|
|||||||
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;
|
||||||
|
|
||||||
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
public interface PlaceRepository extends JpaRepository<Place, Long> {}
|
||||||
}
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package com.example.nto.service;
|
|||||||
import com.example.nto.controller.dto.BookingCreateDto;
|
import com.example.nto.controller.dto.BookingCreateDto;
|
||||||
import com.example.nto.controller.dto.PlaceDto;
|
import com.example.nto.controller.dto.PlaceDto;
|
||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.entity.Booking;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|||||||
@ -13,15 +13,14 @@ import com.example.nto.repository.EmployeeRepository;
|
|||||||
import com.example.nto.repository.PlaceRepository;
|
import com.example.nto.repository.PlaceRepository;
|
||||||
import com.example.nto.service.BookingService;
|
import com.example.nto.service.BookingService;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -47,11 +46,13 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
|
|
||||||
List<Booking> bookings = bookingRepository.findByDateBetween(today, end);
|
List<Booking> bookings = bookingRepository.findByDateBetween(today, end);
|
||||||
|
|
||||||
Map<LocalDate, Set<Long>> busyByDate = bookings.stream()
|
Map<LocalDate, Set<Long>> busyByDate =
|
||||||
.collect(Collectors.groupingBy(
|
bookings.stream()
|
||||||
|
.collect(
|
||||||
|
Collectors.groupingBy(
|
||||||
Booking::getDate,
|
Booking::getDate,
|
||||||
Collectors.mapping(b -> b.getPlace().getId(), Collectors.toSet())
|
Collectors.mapping(
|
||||||
));
|
b -> b.getPlace().getId(), Collectors.toSet())));
|
||||||
|
|
||||||
Map<LocalDate, List<PlaceDto>> result = new LinkedHashMap<>();
|
Map<LocalDate, List<PlaceDto>> result = new LinkedHashMap<>();
|
||||||
|
|
||||||
@ -59,7 +60,8 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
LocalDate currentDate = today.plusDays(i);
|
LocalDate currentDate = today.plusDays(i);
|
||||||
Set<Long> busyPlaces = busyByDate.getOrDefault(currentDate, Collections.emptySet());
|
Set<Long> busyPlaces = busyByDate.getOrDefault(currentDate, Collections.emptySet());
|
||||||
|
|
||||||
List<PlaceDto> freePlaces = allPlaces.stream()
|
List<PlaceDto> freePlaces =
|
||||||
|
allPlaces.stream()
|
||||||
.filter(place -> !busyPlaces.contains(place.getId()))
|
.filter(place -> !busyPlaces.contains(place.getId()))
|
||||||
.map(place -> new PlaceDto(place.getId(), place.getPlace()))
|
.map(place -> new PlaceDto(place.getId(), place.getPlace()))
|
||||||
.toList();
|
.toList();
|
||||||
@ -79,26 +81,33 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
throw new IllegalArgumentException("Date is out of booking window");
|
throw new IllegalArgumentException("Date is out of booking window");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = employeeRepository.findByCode(code)
|
Employee employee =
|
||||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
employeeRepository
|
||||||
|
.findByCode(code)
|
||||||
|
.orElseThrow(
|
||||||
|
() ->
|
||||||
|
new EmployeeNotFoundException(
|
||||||
|
"Employee with " + code + " code not found!"));
|
||||||
|
|
||||||
long placeId = bookingCreateDto.getPlaceId();
|
long placeId = bookingCreateDto.getPlaceId();
|
||||||
Place place = placeRepository.findById(placeId)
|
Place place =
|
||||||
.orElseThrow(() -> new PlaceNotFoundException("Place with " + placeId + " id not found!"));
|
placeRepository
|
||||||
|
.findById(placeId)
|
||||||
|
.orElseThrow(
|
||||||
|
() ->
|
||||||
|
new PlaceNotFoundException(
|
||||||
|
"Place with " + placeId + " id not found!"));
|
||||||
|
|
||||||
if (bookingRepository.findByDateAndPlace(date, place).isPresent()) {
|
if (bookingRepository.findByDateAndPlace(date, place).isPresent()) {
|
||||||
throw new BookingAlreadyExistsException("Booking already exists");
|
throw new BookingAlreadyExistsException("Booking already exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bookingRepository.findByDateAndEmployee(date, employee).isPresent()) {
|
if (bookingRepository.findByDateAndEmployee(date, employee).isPresent()) {
|
||||||
throw new BookingAlreadyExistsException("This employee already has another booking on " + date);
|
throw new BookingAlreadyExistsException(
|
||||||
|
"This employee already has another booking on " + date);
|
||||||
}
|
}
|
||||||
|
|
||||||
Booking booking = Booking.builder()
|
Booking booking = Booking.builder().date(date).employee(employee).place(place).build();
|
||||||
.date(date)
|
|
||||||
.employee(employee)
|
|
||||||
.place(place)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
return bookingRepository.save(booking);
|
return bookingRepository.save(booking);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,8 +17,13 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public EmployeeDto getByCode(String code) {
|
public EmployeeDto getByCode(String code) {
|
||||||
return employeeRepository.findByCode(code).map(EmployeeDto::toDto)
|
return employeeRepository
|
||||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
.findByCode(code)
|
||||||
|
.map(EmployeeDto::toDto)
|
||||||
|
.orElseThrow(
|
||||||
|
() ->
|
||||||
|
new EmployeeNotFoundException(
|
||||||
|
"Employee with " + code + " code not found!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user