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