Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Es/#74 car booking #111

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
8323261
add abstract to address entity
Esmaeeilenani Jun 27, 2023
67ef865
Merge branch 'main' of https://github.com/amigoscode/cohort-2-c
Esmaeeilenani Jun 28, 2023
24f2c39
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jun 28, 2023
03d3680
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jun 29, 2023
ce4078c
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jun 30, 2023
b3c7cbf
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 2, 2023
7ee31b5
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 3, 2023
157add6
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 8, 2023
5939c14
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 12, 2023
f08de42
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 15, 2023
a4852fe
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 15, 2023
7c23f03
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 16, 2023
b45235e
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 17, 2023
7e4a07e
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Jul 18, 2023
2eb5899
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Aug 6, 2023
606adb1
finish car booking
Esmaeeilenani Aug 18, 2023
e3e5eea
Merge remote-tracking branch 'origin/main'
Esmaeeilenani Sep 8, 2023
9ad3bc6
Merge branch 'main' into es/#74-car-booking
Esmaeeilenani Sep 8, 2023
9b41f9d
add discount Percentage to the car provider coupon
Esmaeeilenani Sep 8, 2023
8a3071d
add discount Percentage to the car provider coupon
Esmaeeilenani Sep 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.amigoscode.chohort2.carRental.annotation.TransactionalService;
import com.amigoscode.chohort2.carRental.car.VM.CarVM;
import com.amigoscode.chohort2.carRental.carProviderUser.CarProviderUserService;
import com.amigoscode.chohort2.carRental.carUser.CarUser;
import com.amigoscode.chohort2.carRental.constants.ErrorConstants;
import com.amigoscode.chohort2.carRental.exception.ApiRequestException;
import com.amigoscode.chohort2.carRental.lookupCode.LookupCodes;
Expand Down Expand Up @@ -45,8 +46,11 @@ public CarDTO update(Long carId, CarVM carVM) {
}

public CarDTO getCarById(Long id) {
return CarMapper.INSTANCE.toDto(findCarById(id));
}

public Car findCarById(Long id) {
return carRepository.findById(id)
.map(CarMapper.INSTANCE::toDto)
.orElseThrow(() -> new ApiRequestException(ErrorConstants.CAR_NOT_FOUND));
}

Expand All @@ -63,7 +67,7 @@ private Car createCar(CarVM carVM) {

private Car updateCar(Long carId, CarVM carVM) {
Long providerId = getCurrentCarProviderId();
Car car = carRepository.findById(carId).orElseThrow(() -> new ApiRequestException(ErrorConstants.CAR_NOT_FOUND));
Car car = findCarById(carId);
Validator.invalidateIfFalse(() -> car.getCarProviderId().equals(providerId),
ErrorConstants.CAR_PROVIDER_USER,
"car doesn't belong to the provider");
Expand All @@ -90,4 +94,13 @@ public Page<CarDTO> getSearchCars(Specification<Car> carSearch, Pageable pageabl
return carRepository.findAll(carSearch, pageable)
.map(CarMapper.INSTANCE::toDto);
}

public Car updateCarBookingStatus(Car car,Integer bookingStatus){
car.setBookingStatusCode(bookingStatus);
return carRepository.save(car);
}

public void save(Car car) {
carRepository.save(car);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.amigoscode.chohort2.carRental.carBooking;

import com.amigoscode.chohort2.carRental.authority.AuthorityConstants;
import com.amigoscode.chohort2.carRental.carBooking.vm.UserBookingVM;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -10,4 +18,14 @@
public class CarBookingController {
private final CarBookingService carBookingService;


@PostMapping
@Secured({AuthorityConstants.CLIENT})
public ResponseEntity<CarBookingDTO> createUserBooking(@RequestBody @Valid UserBookingVM userBookingVM){

return ResponseEntity
.status(HttpStatus.CREATED)
.body(carBookingService.createUserBooking(userBookingVM));
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
package com.amigoscode.chohort2.carRental.carBooking;

import com.amigoscode.chohort2.carRental.annotation.TransactionalService;
import com.amigoscode.chohort2.carRental.car.Car;
import com.amigoscode.chohort2.carRental.car.CarService;
import com.amigoscode.chohort2.carRental.carBooking.vm.UserBookingVM;
import com.amigoscode.chohort2.carRental.carProviderCoupon.CarProviderCoupon;
import com.amigoscode.chohort2.carRental.carProviderCoupon.CarProviderCouponService;
import com.amigoscode.chohort2.carRental.carUser.CarUser;
import com.amigoscode.chohort2.carRental.carUser.CarUserService;
import com.amigoscode.chohort2.carRental.constants.ErrorConstants;
import com.amigoscode.chohort2.carRental.lookupCode.LookupCodes;
import com.amigoscode.chohort2.carRental.user.User;
import com.amigoscode.chohort2.carRental.user.UserService;
import com.amigoscode.chohort2.carRental.validation.Validator;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@TransactionalService
@RequiredArgsConstructor
public class CarBookingService {

private final CarBookingRepository carBookingRepository;
private final CarBookingRepository carBookingRepository;
private final UserService userService;

private final CarUserService carUserService;

private final CarService carService;

private final CarProviderCouponService carProviderCouponService;

public CarBookingDTO createUserBooking(UserBookingVM userBookingVM) {
User loggedInUser = userService.getLoggedInUser();

Car car = carService.findCarById(userBookingVM.getCarId());

Validator.invalidateIfTrue(() -> !car.getBookingStatusCode().equals(LookupCodes.CarBookingStatus.available), ErrorConstants.CAR_NOT_AVAILABLE);


CarUser carUser = carUserService.createCarUserFromCar(car);

carService.updateCarBookingStatus(car, LookupCodes.CarBookingStatus.inUse);

long daysOfBooking = ChronoUnit.DAYS.between(LocalDate.now(), userBookingVM.getCheckOut());
float price = daysOfBooking * car.getPrice();
float totalPrice = price - calculateCoupon(userBookingVM.getCouponCode(), price);

CarBooking carBooking = new CarBooking().setUserId(loggedInUser.getId()).setCarUserId(carUser.getCarId()).setCheckInDate(LocalDate.now()).setCheckOutDate(userBookingVM.getCheckOut()).setStatusCode(LookupCodes.UserBookingStatus.inUse).setFinalPrice(totalPrice);


return CarBookingMapper.INSTANCE.toDto(carBookingRepository.saveAndRefresh(carBooking));
}


private float calculateCoupon(String couponCode, float price) {
if (StringUtils.isBlank(couponCode)) {
return 0.0f;
}


CarProviderCoupon coupon = carProviderCouponService.findCouponByCode(couponCode);
Validator.invalidateIfTrue(() -> coupon.getEndDate().isBefore(LocalDate.now()), "coupon is expired");

Validator.invalidateIfTrue(() -> !coupon.getIsAvailable(), "coupon is not available for use");

//TODO: add userCoupon entity

return price * (coupon.getDiscountPercentage() / 100);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amigoscode.chohort2.carRental.carBooking.vm;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Setter
@Getter
public class UserBookingVM {

@NotNull
private Long carId;
@NotNull
private LocalDate checkIn;
@NotNull
private LocalDate checkOut;

private String couponCode;


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ public class CarProviderCoupon extends AbstractAuditingEntity {
@Column (name = "is_available", nullable = false)
private Boolean isAvailable = true;

@Column (name = "discount_percentage", nullable = false)
private Float discountPercentage;


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class CarProviderCouponDTO implements Serializable {
private LocalDate endDate;
private Integer numOfUsePerUser;
private Boolean isAvailable;

private Float discountPercentage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.amigoscode.chohort2.carRental.abstracts.repository.CRJpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface CarProviderCouponRepository extends CRJpaRepository<CarProviderCoupon, Long> {
@Query("select u from CarProviderCoupon u where u.couponCode=:couponCode")
Optional<CarProviderCoupon> findByCouponCode(String couponCode);
Optional<CarProviderCoupon> findByCouponCode(@Param("couponCode") String couponCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import com.amigoscode.chohort2.carRental.validation.Validator;
import lombok.RequiredArgsConstructor;

import java.util.Optional;


@TransactionalService
@RequiredArgsConstructor
Expand All @@ -28,7 +26,8 @@ public CarProviderCouponDTO save(CarProviderCouponVM carProviderCouponVM) {
.setStartDate(carProviderCouponVM.getStartDate())
.setEndDate(carProviderCouponVM.getEndDate())
.setNumOfUsePerUser(carProviderCouponVM.getNumOfUsePerUser())
.setIsAvailable(carProviderCouponVM.getIsAvailable());
.setIsAvailable(carProviderCouponVM.getIsAvailable())
.setDiscountPercentage(carProviderCouponVM.getDiscountPercentage());
return CarProviderCouponMapper.INSTANCE.toDto(carProviderCouponRepository.save(carProviderCoupon));
}

Expand All @@ -55,4 +54,10 @@ private Long getCarProvideId() {
CarProviderUser carProviderUser = carProviderUserService.findCarProviderUserByUserId(user.getId());
return carProviderUser.getCarProviderId();
}

public CarProviderCoupon findCouponByCode(String couponCode) {

return carProviderCouponRepository.findByCouponCode(couponCode)
.orElseThrow(() -> new ApiRequestException("Coupon not found"));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.amigoscode.chohort2.carRental.carProviderCoupon.VM;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
Expand All @@ -28,4 +30,10 @@ public class CarProviderCouponVM implements Serializable {
@NotEmpty
private Boolean isAvailable = true;

@NotNull
@Min(0)
private Float discountPercentage;



}
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.amigoscode.chohort2.carRental.carUser;

import com.amigoscode.chohort2.carRental.annotation.TransactionalService;
import com.amigoscode.chohort2.carRental.car.Car;
import lombok.RequiredArgsConstructor;

@TransactionalService
@RequiredArgsConstructor
public class CarUserService {

private final CarUserRepository userRepository;
private final CarUserRepository carUserRepository;


public void save(CarUser carUser) {
carUserRepository.save(carUser);
}


public CarUser createCarUserFromCar(Car car) {
CarUser carUser = new CarUser()
.setCarId(car.getId())
.setRegistrationNumber(car.getRegistrationNumber())
.setBrandCode(car.getBrandCode())
.setBrandModelCode(car.getBrandModelCode())
.setProductionYear(car.getProductionYear())
.setMaxSpeed(car.getMaxSpeed())
.setHorsePower(car.getHorsePower())
.setRgbCode(car.getRgbCode())
.setDescription(car.getDescription())
.setCategoryCode(car.getCategoryCode())
.setBookingStatusCode(car.getBookingStatusCode())
.setPrice(car.getPrice())
.setImgUrl(car.getImgUrl());
return carUserRepository.save(carUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class ErrorConstants {
public static final String NOT_VALID_LICENSE = String
.format("%s%s", BASE_ERROR_KEY, "notValidLicense");

public static final String CAR_NOT_AVAILABLE = String
.format("%s%s", BASE_ERROR_KEY, "carNotAvailableForRent");



//-----------Not found errors key---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@
</changeSet>


<changeSet id="202309081305-1-car-provider-coupon--discountPercentage" author="esmaeeil">
<addColumn tableName="car_provider_coupon">
<column name="discount_percentage" type="float">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>


</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void tearUp() {
.setStartDate(LocalDate.of(2023, 11, 3))
.setEndDate(LocalDate.of(2023, 12, 2))
.setNumOfUsePerUser(1)
.setIsAvailable(true);
.setIsAvailable(true)
.setDiscountPercentage(10F);
updatedItem = new CarProviderCouponAvailabilityVM()
.setIsAvailable(false);

Expand Down