-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add Basic CRUD for Ratings #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
438eb7d
Disable sql in the application-test.properties
45d1e62
Add test data class
76df759
Add dto layer
6daf8d5
Rename
fc73ee2
Add mapper
073b280
Add rating service
52f8475
Add unit tests
4b661ef
Add controller and integration tests
8845bb2
Add roles to the endpoints
e1eef12
Add authorization denied to the global handler
08076cd
Refactor controller layer
4c4b3fb
Add Not Found to global Exception
21d4061
Add method mismatch to exception handler
63499b8
Add enum mismatch to handler
6121ced
Add driver layer
8f44f63
Add payment layer
b69273a
Add order layer
b1ca5cf
Update order repository
6d92ae9
Refactor existing code
38d4808
Refactor existing code #2
2bd9afe
Refactor existing code #3
1de2d2a
Add request/response for vehicle
bcc3f50
Add unit and its
ec84989
Fix rabbit suggestion
d244e31
Add transactional support to order, payment, and rating
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/com/angel/autonow/driver/DriverController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| 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.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/drivers") | ||
| public class DriverController { | ||
|
|
||
| private final DriverService driverService; | ||
|
|
||
| @PostMapping | ||
| @PreAuthorize("hasRole('ADMIN')") | ||
| public ResponseEntity<DriverResponseDTO> createDriver(@Valid @RequestBody DriverRequestDTO request) { | ||
| return driverService.createDriver(request) | ||
| .map(driver -> ResponseEntity.status(HttpStatus.CREATED).body(driver)) | ||
| .orElse(ResponseEntity.badRequest().build()); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'CUSTOMER', 'DRIVER')") | ||
| public DriverResponseDTO getDriverById(@PathVariable Long id) { | ||
| return driverService.getDriverById(id).orElse(null); | ||
| } | ||
|
|
||
| @GetMapping("/license/{licenseNumber}") | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'DRIVER')") | ||
| public DriverResponseDTO getDriverByLicenseNumber(@PathVariable String licenseNumber) { | ||
| return driverService.getDriverByLicenseNumber(licenseNumber).orElse(null); | ||
| } | ||
|
|
||
| @GetMapping | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'CUSTOMER', 'DRIVER')") | ||
| public List<DriverResponseDTO> getAllDrivers() { | ||
| return driverService.getAllDrivers(); | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/com/angel/autonow/driver/DriverMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import com.angel.autonow.vehicle.VehicleEntity; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import org.mapstruct.Named; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| public interface DriverMapper { | ||
|
|
||
| @Mapping(source = "vehicles", target = "vehicleIds", qualifiedByName = "vehiclesToIds") | ||
| DriverResponseDTO toDTO(DriverEntity driver); | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| @Mapping(target = "vehicles", ignore = true) | ||
| DriverEntity toEntity(DriverRequestDTO request); | ||
|
|
||
| @Named("vehiclesToIds") | ||
| default Set<Long> vehiclesToIds(Set<VehicleEntity> vehicles) { | ||
| if (vehicles == null) { | ||
| return Collections.emptySet(); | ||
| } | ||
|
|
||
| return vehicles.stream() | ||
| .map(VehicleEntity::getId) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/angel/autonow/driver/DriverRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface DriverRepository extends JpaRepository<DriverEntity, Long> { | ||
|
|
||
| Optional<DriverEntity> findByLicenseNumber(String licenseNumber); | ||
| } |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/com/angel/autonow/driver/DriverRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import com.angel.autonow.expertise.ExpertiseType; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Pattern; | ||
|
|
||
| public record DriverRequestDTO( | ||
|
|
||
| @NotBlank(message = "First name is required") | ||
| String firstName, | ||
|
|
||
| @NotBlank(message = "Last name is required") | ||
| String lastName, | ||
|
|
||
| @NotBlank(message = "Phone number is required") | ||
| @Pattern(regexp = "^\\+?[0-9]{10,15}$", message = "Phone number must be valid") | ||
| String phoneNumber, | ||
|
|
||
| @NotBlank(message = "License number is required") | ||
| String licenseNumber, | ||
|
|
||
| @NotNull(message = "Expertise type is required") | ||
| ExpertiseType expertiseType, | ||
|
|
||
| boolean available, | ||
|
|
||
| String imageUrl | ||
| ) { | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/com/angel/autonow/driver/DriverResponseDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import com.angel.autonow.expertise.ExpertiseType; | ||
| import lombok.Builder; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| @Builder | ||
| public record DriverResponseDTO( | ||
| Long id, | ||
| String firstName, | ||
| String lastName, | ||
| String phoneNumber, | ||
| String licenseNumber, | ||
| ExpertiseType expertiseType, | ||
| boolean available, | ||
| String imageUrl, | ||
| Set<Long> vehicleIds | ||
| ) { | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/angel/autonow/driver/DriverService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.angel.autonow.driver; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class DriverService { | ||
|
|
||
| private final DriverRepository driverRepository; | ||
| private final DriverMapper driverMapper; | ||
|
|
||
| public Optional<DriverResponseDTO> createDriver(DriverRequestDTO request) { | ||
| DriverEntity driver = driverMapper.toEntity(request); | ||
| DriverEntity saved = driverRepository.save(driver); | ||
| return Optional.of(driverMapper.toDTO(saved)); | ||
| } | ||
|
|
||
| public Optional<DriverResponseDTO> getDriverById(Long id) { | ||
| return driverRepository.findById(id).map(driverMapper::toDTO); | ||
| } | ||
|
|
||
| public Optional<DriverResponseDTO> getDriverByLicenseNumber(String licenseNumber) { | ||
| return driverRepository.findByLicenseNumber(licenseNumber).map(driverMapper::toDTO); | ||
| } | ||
|
|
||
| public List<DriverResponseDTO> getAllDrivers() { | ||
| return driverRepository.findAll().stream() | ||
| .map(driverMapper::toDTO) | ||
| .toList(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/com/angel/autonow/order/OrderController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.angel.autonow.order; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| 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.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/api/orders") | ||
| public class OrderController { | ||
|
|
||
| private final OrderService orderService; | ||
|
|
||
| @PostMapping | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'CUSTOMER')") | ||
| public ResponseEntity<OrderResponseDTO> createOrder(@Valid @RequestBody OrderRequestDTO request) { | ||
| return orderService.createOrder(request) | ||
| .map(order -> ResponseEntity.status(HttpStatus.CREATED).body(order)) | ||
| .orElse(ResponseEntity.badRequest().build()); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'CUSTOMER', 'DRIVER')") | ||
| public OrderResponseDTO getOrderById(@PathVariable Long id) { | ||
| return orderService.getOrderById(id).orElse(null); | ||
| } | ||
|
StoynovAngel marked this conversation as resolved.
|
||
|
|
||
| @GetMapping("/user/{userId}") | ||
| @PreAuthorize("hasAnyRole('ADMIN', 'CUSTOMER')") | ||
| public List<OrderResponseDTO> getOrdersByUserId(@PathVariable Long userId) { | ||
| return orderService.getOrdersByUserId(userId); | ||
| } | ||
|
|
||
| @GetMapping | ||
| @PreAuthorize("hasRole('ADMIN')") | ||
| public List<OrderResponseDTO> getAllOrders() { | ||
| return orderService.getAllOrders(); | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/angel/autonow/order/OrderMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.angel.autonow.order; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(componentModel = "spring") | ||
| public interface OrderMapper { | ||
|
|
||
| @Mapping(source = "user.id", target = "userId") | ||
| @Mapping(source = "driver.id", target = "driverId") | ||
| @Mapping(source = "vehicle.id", target = "vehicleId") | ||
| OrderResponseDTO toDTO(OrderEntity order); | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| @Mapping(target = "user", ignore = true) | ||
| @Mapping(target = "driver", ignore = true) | ||
| @Mapping(target = "vehicle", ignore = true) | ||
| @Mapping(target = "status", ignore = true) | ||
| @Mapping(target = "finalPrice", ignore = true) | ||
| @Mapping(target = "cancellationReason", ignore = true) | ||
| @Mapping(target = "createdAt", ignore = true) | ||
| @Mapping(target = "updatedAt", ignore = true) | ||
| OrderEntity toEntity(OrderRequestDTO request); | ||
| } |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/angel/autonow/order/OrderRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.angel.autonow.order; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface OrderRepository extends JpaRepository<OrderEntity, Long> { | ||
|
|
||
| List<OrderEntity> findByUserId(Long userId); | ||
| } |
50 changes: 50 additions & 0 deletions
50
backend/src/main/java/com/angel/autonow/order/OrderRequestDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.angel.autonow.order; | ||
|
|
||
| import com.angel.autonow.vehicle.VehicleType; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Positive; | ||
|
|
||
| public record OrderRequestDTO( | ||
|
|
||
| @NotNull(message = "User ID is required") | ||
| Long userId, | ||
|
|
||
| Long driverId, | ||
|
|
||
| Long vehicleId, | ||
|
|
||
| @NotNull(message = "Vehicle type is required") | ||
| VehicleType vehicleType, | ||
|
|
||
| @NotBlank(message = "Pickup address is required") | ||
| String pickupAddress, | ||
|
|
||
| @NotNull(message = "Pickup latitude is required") | ||
| Double pickupLatitude, | ||
|
|
||
| @NotNull(message = "Pickup longitude is required") | ||
| Double pickupLongitude, | ||
|
|
||
| @NotBlank(message = "Dropoff address is required") | ||
| String dropoffAddress, | ||
|
|
||
| @NotNull(message = "Dropoff latitude is required") | ||
| Double dropoffLatitude, | ||
|
|
||
| @NotNull(message = "Dropoff longitude is required") | ||
| Double dropoffLongitude, | ||
|
|
||
| @Positive(message = "Estimated price must be positive") | ||
| Double estimatedPrice, | ||
|
|
||
| @Positive(message = "Distance must be positive") | ||
| Double distanceKm, | ||
|
|
||
| @Positive(message = "Estimated duration must be positive") | ||
| Integer estimatedDurationMinutes, | ||
|
|
||
| String specialRequirements | ||
| ) { | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.