Skip to content

Commit

Permalink
implemented function-for-checking-overdue-rentals (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasnov-Maksim authored Mar 11, 2024
1 parent 857ecea commit 4c04fd0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class CarSharingAppApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mate.academy.carsharing.repository.rental;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import mate.academy.carsharing.model.Rental;
import org.springframework.data.domain.Page;
Expand All @@ -11,4 +13,6 @@ public interface RentalRepository extends JpaRepository<Rental, Long> {
Optional<Rental> findByIdAndUserId(Long rentalId, Long userId);

Page<Rental> findAll(Specification<Rental> spec, Pageable pageable);

List<Rental> findAllByReturnDateBeforeAndActualReturnDateIsNull(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import mate.academy.carsharing.service.RentalService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class RentalServiceImpl implements RentalService {
private static final Integer MIN_REQUIRED_CAR_AVAILABLE = 1;
private static final String THERE_IS_NO_CAR_AVAILABLE_WITH_ID =
"There is no car available with id: ";
private final RentalRepository rentalRepository;
private final UserRepository userRepository;
private final CarRepository carRepository;
Expand All @@ -42,17 +45,14 @@ public class RentalServiceImpl implements RentalService {
@Transactional
public RentalResponseDto save(CreateRentalRequestDto requestDto) {
Car car = getCarById(requestDto.carId());
int inventory = car.getInventory();
if (inventory < MIN_REQUIRED_CAR_AVAILABLE) {
throw new RentalException("There is no car available with id: " + requestDto.carId());
}
car.setInventory(inventory - 1);
checkIsCarAvailable(requestDto, car);
car.setInventory(car.getInventory() - 1);
Rental newRental = rentalMapper.toModel(requestDto);
newRental.setCar(car);
newRental.setUser(getUserById(requestDto.userId()));
Rental savedRental = rentalRepository.save(newRental);
RentalResponseDto savedRentalDto = rentalMapper.toDto(savedRental);
notifyUser("Your rental created!\\n", savedRentalDto);
notifyUserWithRentalInfo("Your rental created!\\n", savedRentalDto);
return savedRentalDto;
}

Expand Down Expand Up @@ -83,14 +83,43 @@ public RentalResponseDto returnRental(Long id) {
Car car = rental.getCar();
car.setInventory(car.getInventory() + 1);
rental.setActualReturnDate(LocalDate.now());

Rental savedRental = rentalRepository.save(rental);
RentalResponseDto savedRentalDto = rentalMapper.toDto(savedRental);
notifyUser("you have just returned the rental!\\n", savedRentalDto);
notifyUserWithRentalInfo("you have just returned the rental!\\n", savedRentalDto);
return savedRentalDto;
}

private void notifyUser(String message, RentalResponseDto savedRentalDto) {
@Scheduled(cron = "0 0 10 * * *")
public void checkOverdueRentals() {
LocalDate today = LocalDate.now();
List<Rental> overdueRentals =
rentalRepository.findAllByReturnDateBeforeAndActualReturnDateIsNull(today);
if (overdueRentals.isEmpty()) {
notificationService.sendGlobalNotification("No rentals overdue today!");
return;
}
for (Rental rental : overdueRentals) {
String message = createOverdueRentalMessage(rental);
notificationService.sendNotification(rental.getUser().getId(), message);
}
}

private void checkIsCarAvailable(CreateRentalRequestDto requestDto, Car car) {
if (car.getInventory() < MIN_REQUIRED_CAR_AVAILABLE) {
notificationService.sendNotification(requestDto.userId(),
THERE_IS_NO_CAR_AVAILABLE_WITH_ID + requestDto.carId());
throw new RentalException(THERE_IS_NO_CAR_AVAILABLE_WITH_ID + requestDto.carId());
}
}

private String createOverdueRentalMessage(Rental rental) {
return "Overdue rental alert! Rental ID: " + rental.getId()
+ ", User ID: " + rental.getUser().getId()
+ ", Car ID: " + rental.getCar().getId()
+ ", Return Date: " + rental.getReturnDate();
}

private void notifyUserWithRentalInfo(String message, RentalResponseDto savedRentalDto) {
String stringWithRentalDto;
try {
stringWithRentalDto = objectMapper.writerWithDefaultPrettyPrinter()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package mate.academy.carsharing;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
//@SpringBootTest
class CarSharingAppApplicationTests {

@Test
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.profiles.active=testcontainers, telegram_bot
#spring.profiles.active=localdb, telegram_bot
spring.profiles.active=testcontainers
#spring.profiles.active=localdb
#spring.profiles.active=h2db

spring.config.import=developer.properties
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/db/changelog/changelog-test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
databaseChangeLog:
- include:
file: db/changelog/changes/01-create-cars-table.yaml
- include:
file: db/changelog/changes/02-create-roles-table.yaml
- include:
file: db/changelog/changes/03-create-users-table.yaml
- include:
file: db/changelog/changes/04-create-users_roles-table.yaml
- include:
file: db/changelog/changes/05-fill-roles-table.yaml
- include:
file: db/changelog/changes/06-create-rentals-table.yaml
- include:
file: db/changelog/changes/07-create-telegram_user_info-table.yaml

0 comments on commit 4c04fd0

Please sign in to comment.