Skip to content

Commit

Permalink
resolved all of conflicts and updated the branch for implementing pay…
Browse files Browse the repository at this point in the history
…ment features
  • Loading branch information
ivan0dyatlyukkk committed Oct 4, 2023
2 parents 81328bc + ea2d11a commit 3aa6990
Show file tree
Hide file tree
Showing 36 changed files with 519 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,61 @@

import com.project.carsharingapp.dto.rental.CreateRentalRequestDto;
import com.project.carsharingapp.dto.rental.RentalDto;
import com.project.carsharingapp.dto.rental.SetActualReturnDateRequestDto;
import com.project.carsharingapp.service.rental.RentalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Rental management",
description = "Endpoints for managing rentals")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/rentals")
public class RentalController {
private final RentalService rentalService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Save new rental",
description = "Save new rental and decrease car inventory by 1")
public RentalDto add(@RequestBody @Valid CreateRentalRequestDto requestDto) {
return null;
return rentalService.add(requestDto);
}

@GetMapping("/{userId}/{isActive}")
@GetMapping("/{user_id}/{is_active}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get the rentals by user id and active status",
description = "Retrieve rentals by user identification number"
+ " and whether the rental is still active or not")
public RentalDto getByUserIdAndActiveStatus(@PathVariable Long userId,
@PathVariable boolean isActive) {
return null;
public List<RentalDto> getByUserIdAndActiveStatus(@RequestParam Long userId,
@RequestParam boolean isActive) {
return rentalService.getByUserIdAndActiveStatus(userId, isActive);
}

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get the rental by id",
description = "Get specific rental by identification number")
public RentalDto getById(@PathVariable Long id) {
return null;
return rentalService.getById(id);
}

@PostMapping("/{returnDate}")
@PostMapping("/return")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Set actual return date",
description = "Set actual return date and increase car inventory by 1")
public RentalDto setActualReturnDay(@RequestBody
@Valid SetActualReturnDateRequestDto returnDate) {
return null;
public RentalDto setActualReturnDay(Long id) {
return rentalService.setActualReturnDay(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.project.carsharingapp.dto.user.UpdateUserProfileRequestDto;
import com.project.carsharingapp.dto.user.UpdateUserRoleRequestDto;
import com.project.carsharingapp.dto.user.UserDto;
import com.project.carsharingapp.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -18,17 +20,19 @@

@Tag(name = "User management",
description = "Endpoints for managing users")
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/users")
public class UserController {
private final UserService userService;

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Get user profile info by user id",
description = "Get user's detailed information about"
+ " user profile by user identification number")
public UserDto getById(@PathVariable Long id) {
return null;
return userService.findById(id);
}

@PatchMapping("/{id}/role")
Expand All @@ -38,7 +42,7 @@ public UserDto getById(@PathVariable Long id) {
public UserDto updateUserRole(@PathVariable Long id,
@RequestBody @Valid
UpdateUserRoleRequestDto requestDto) {
return null;
return userService.updateUserRole(id, requestDto);
}

@PutMapping("/{id}")
Expand All @@ -48,6 +52,6 @@ public UserDto updateUserRole(@PathVariable Long id,
+ "by user identification number")
public UserDto updateUserProfile(@PathVariable Long id,
@Valid @RequestBody UpdateUserProfileRequestDto requestDto) {
return null;
return userService.updateUserProfile(id, requestDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
@Getter
@Setter
public class CreateRentalRequestDto {
@NotNull
private LocalDateTime rentalDate;
@NotNull
private LocalDateTime returnDate;
@NotNull
private LocalDateTime actualReturnDate;
@NotNull
private Long carId;
@NotNull
private Long userId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.carsharingapp.dto.rental;

public record RentalSearchParametersDto(
String[] userId,
String[] isActive
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.project.carsharingapp.dto.user;

import com.project.carsharingapp.model.RoleName;
import com.project.carsharingapp.model.Role;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -9,5 +9,5 @@
@Setter
public class UpdateUserRoleRequestDto {
@NotNull
private RoleName role;
private Role.RoleName role;
}
17 changes: 17 additions & 0 deletions src/main/java/com/project/carsharingapp/mapper/RentalMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.project.carsharingapp.mapper;

import com.project.carsharingapp.config.MapperConfig;
import com.project.carsharingapp.dto.rental.CreateRentalRequestDto;
import com.project.carsharingapp.dto.rental.RentalDto;
import com.project.carsharingapp.model.Rental;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(config = MapperConfig.class)
public interface RentalMapper {
Rental toEntity(CreateRentalRequestDto requestDto);

@Mapping(source = "user.id", target = "userId")
@Mapping(source = "car.id", target = "carId")
RentalDto toDto(Rental rental);
}
11 changes: 11 additions & 0 deletions src/main/java/com/project/carsharingapp/mapper/UserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.project.carsharingapp.mapper;

import com.project.carsharingapp.config.MapperConfig;
import com.project.carsharingapp.dto.user.UserDto;
import com.project.carsharingapp.model.User;
import org.mapstruct.Mapper;

@Mapper(config = MapperConfig.class)
public interface UserMapper {
UserDto toDto(User user);
}
9 changes: 9 additions & 0 deletions src/main/java/com/project/carsharingapp/model/Rental.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,27 @@ public class Rental {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "rental_date", nullable = false)
private LocalDateTime rentalDate;

@Column(name = "return_date", nullable = false)
private LocalDateTime returnDate;

@Column(name = "actual_return_date")
private LocalDateTime actualReturnDate;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "car_id", nullable = false)
private Car car;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(name = "is_deleted",nullable = false)
private boolean isDeleted;

@Column(name = "is_active",nullable = false)
private boolean isActive = false;
}
5 changes: 5 additions & 0 deletions src/main/java/com/project/carsharingapp/model/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class Role {
@Column(name = "name", nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private RoleName roleName;

public enum RoleName {
ROLE_MANAGER,
ROLE_CUSTOMER
}
}
6 changes: 0 additions & 6 deletions src/main/java/com/project/carsharingapp/model/RoleName.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.model.Role;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RoleRepository extends JpaRepository<Role, Long> {
Optional<Role> findByRoleName(Role.RoleName name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.dto.rental.RentalSearchParametersDto;
import org.springframework.data.jpa.domain.Specification;

public interface SpecificationBuilder<T> {
Specification<T> build(RentalSearchParametersDto searchParametersDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.model.Rental;
import org.springframework.data.jpa.domain.Specification;

public interface SpecificationProvider<T> {
String getKey();

Specification<Rental> getSpecification(String[] params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.project.carsharingapp.repository;

public interface SpecificationProviderManager<T> {
SpecificationProvider<T> getSpecificationProvider(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import com.project.carsharingapp.model.User;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface UserRepository extends JpaRepository<User, Long> {

@Query("FROM User u LEFT JOIN FETCH u.roles WHERE u.id = :id")
Optional<User> findUserById(Long id);

Optional<User> findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.project.carsharingapp.repository;
package com.project.carsharingapp.repository.rentals;

import com.project.carsharingapp.model.Rental;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface RentalRepository extends JpaRepository<Rental, Long> {
@Query("FROM Rental r LEFT JOIN FETCH r.car "
+ "LEFT JOIN FETCH r.user WHERE r.id = :id")
Optional<Rental> findById(Long id);

@Query("FROM Rental r WHERE r.user.id = :userId AND r.isActive = :isActive")
List<Rental> findRentalsByUserIdAndActiveStatus(Long userId, boolean isActive);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.project.carsharingapp.repository.rentals;

import com.project.carsharingapp.dto.rental.RentalSearchParametersDto;
import com.project.carsharingapp.model.Rental;
import com.project.carsharingapp.repository.SpecificationBuilder;
import com.project.carsharingapp.repository.SpecificationProviderManager;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class RentalSpecificationBuilder implements SpecificationBuilder<Rental> {
public static final String USER_ID_KEY = "userId";
public static final String IS_ACTIVE_KEY = "isActive";

private final SpecificationProviderManager<Rental> rentalSpecificationProviderManager;

@Override
public Specification<Rental> build(RentalSearchParametersDto searchParametersDto) {
Specification<Rental> spec = Specification.where(null);
spec = getRentalSpecification(searchParametersDto.userId(), spec, USER_ID_KEY);
spec = getRentalSpecification(searchParametersDto.isActive(), spec, IS_ACTIVE_KEY);
return spec;
}

private Specification<Rental> getRentalSpecification(String[] searchParametersDto,
Specification<Rental> spec,
String key) {
if (searchParametersDto != null && searchParametersDto.length > 0) {
spec = spec.and(rentalSpecificationProviderManager.getSpecificationProvider(key)
.getSpecification(searchParametersDto));
}
return spec;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.project.carsharingapp.repository.rentals;

import com.project.carsharingapp.model.Rental;
import com.project.carsharingapp.repository.SpecificationProvider;
import com.project.carsharingapp.repository.SpecificationProviderManager;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class RentalSpecificationProviderManager implements SpecificationProviderManager<Rental> {
private final List<SpecificationProvider<Rental>> rentalSpecificationProviders;

@Override
public SpecificationProvider<Rental> getSpecificationProvider(String key) {
return rentalSpecificationProviders
.stream()
.filter(p -> p.getKey().equals(key))
.findFirst()
.orElseThrow(() -> new NoSuchElementException(
"Can't find correct specification provider for key: " + key));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.project.carsharingapp.repository.rentals.spec;

import static com.project.carsharingapp.repository.rentals.RentalSpecificationBuilder.IS_ACTIVE_KEY;

import com.project.carsharingapp.model.Rental;
import com.project.carsharingapp.repository.SpecificationProvider;
import java.util.Arrays;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

@Component
public class ActiveStatusSpecificationProvider implements SpecificationProvider<Rental> {
@Override
public String getKey() {
return IS_ACTIVE_KEY;
}

public Specification<Rental> getSpecification(String[] params) {
return (root, query, criteriaBuilder) -> root.get(IS_ACTIVE_KEY)
.in(Arrays.stream(params).toArray());
}
}
Loading

0 comments on commit 3aa6990

Please sign in to comment.