conorH22/bike-rental

View on GitHub
src/main/java/com/csharp/bikerental/service/ReservationService/ReservationServiceImpl.java

Summary

Maintainability
A
3 hrs
Test Coverage
package com.csharp.bikerental.service.ReservationService;

import com.csharp.bikerental.dto.*;
import com.csharp.bikerental.persistence.model.Customer;
import com.csharp.bikerental.persistence.model.Employee;

import com.csharp.bikerental.persistence.model.TwoWheel.TwoWheel;
import com.csharp.bikerental.persistence.model.User;
import com.csharp.bikerental.persistence.model.reservation.MaintenanceReservation;
import com.csharp.bikerental.persistence.model.reservation.OneTimeReservation;
import com.csharp.bikerental.persistence.model.reservation.RepeatingReservation;
import com.csharp.bikerental.persistence.model.reservation.Reservation;
import com.csharp.bikerental.persistence.repo.ReservationRepository;
import com.csharp.bikerental.persistence.repo.TwoWheelRepository;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public  class ReservationServiceImpl implements ReservationServiceInterface{
    @Autowired
     ReservationRepository reservationRepository;

    @Autowired
    TwoWheelRepository twoWheelRepository;

    public OneTimeReservationDto createOneTimeReservation(Customer customer, ReserveOneTimeDto reserveOneTimeDto) throws ServiceException {
        TwoWheel twoWheel = twoWheelRepository.findById(reserveOneTimeDto.getTwoWheelId().toString()).orElse(null);

        if(twoWheel == null) {
            throw new ServiceException("twoWheel not found");
        }


        if(this.checkIfTwoWheelReserved(twoWheel, reserveOneTimeDto.getStartDate()) != null) {
            throw new ServiceException("twoWheel already reserved at this date");
        }

        OneTimeReservation oneTimeReservation = new OneTimeReservation(customer,reserveOneTimeDto.getStartDate(),reserveOneTimeDto.getEndDate(),twoWheel);

        return new OneTimeReservationDto(reservationRepository.save(oneTimeReservation));
    }

    public MaintenanceReservationDto createMaintenanceReservation(Employee employee, ReserveMaintenanceDto reserveMaintenanceDto) throws ServiceException {
        TwoWheel twoWheel = twoWheelRepository.findById(reserveMaintenanceDto.getTwoWheelId()).orElse(null);

        if(twoWheel == null) {
            throw new ServiceException("twoWheel not found");
        }

        if(this.checkIfTwoWheelReserved(twoWheel, reserveMaintenanceDto.getStartDate()) != null) {
            throw new ServiceException("twoWheel already reserved at this date");
        }

        MaintenanceReservation maintenanceReservation = new MaintenanceReservation(employee, twoWheel, reserveMaintenanceDto.getStartDate(), reserveMaintenanceDto.getEndDate());

        return new MaintenanceReservationDto(reservationRepository.save(maintenanceReservation));
    }

    public RepeatingReservationDto createRepeatingReservation(User user, ReserveRepeatingDto reserveRepeatingDto) throws ServiceException {
        TwoWheel twoWheel = twoWheelRepository.findById(reserveRepeatingDto.getTwoWheelId()).orElse(null);

        if(twoWheel == null) {
            throw new ServiceException("twoWheel not found");
        }

        if(this.checkIfTwoWheelReserved(twoWheel, reserveRepeatingDto.getStartDate()) != null) {
            throw new ServiceException("twoWheel already reserved at this date");
        }

        RepeatingReservation repeatingReservation = new RepeatingReservation(twoWheel, user, reserveRepeatingDto.getReservationPeriodicity(), reserveRepeatingDto.getStartDate(), reserveRepeatingDto.getEndDate());

        return new RepeatingReservationDto(reservationRepository.save(repeatingReservation));
    }

    public Reservation checkIfTwoWheelReserved(TwoWheel twoWheel, Date date){
        List<Reservation> reservations = reservationRepository.findAllByTwoWheelAndCancelledFalse(twoWheel);

        for(Reservation reservation: reservations) {
            if(!date.before(reservation.getStartDate()) && !date.after(reservation.getEndDate())) {
                // A reservation already exists
                return reservation;
            }

            if(reservation instanceof RepeatingReservation) {
                Long increaseTimeAmount = 0L;
                switch(((RepeatingReservation) reservation).getReservationPeriodicity()) {
                    case DAILY:
                        increaseTimeAmount = 86400000L;
                        break;
                    case WEEKLY:
                        increaseTimeAmount = 604800000L;
                        break;
                    case MONTHLY:
                        increaseTimeAmount = 2419200000L;
                        break;

                }
                Date startDate = reservation.getStartDate();
                Date endDate = reservation.getEndDate();

                while (date.after(endDate)) {
                    if(!date.before(startDate) && !date.after(endDate)) {
                        // A reservation already exists
                        return reservation;
                    }
                    startDate.setTime(startDate.getTime() + increaseTimeAmount);
                    endDate.setTime(endDate.getTime() + increaseTimeAmount);
                }
            }
        }
        return null;
    }


}