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

extends Notification service between driver and customer #35

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -12,4 +12,5 @@ public class ResourcePath {
public static final String REGISTER = "/register";
public static final String LOGIN = "/login";
public static final String AUTH= "/auth";
public static final String REQUEST = "/request";
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void sendRequestDriver(CustomerRequestDriver requestDriver){
() -> new BusinessException(ExceptionPayloadFactory.DRIVER_LOCATION_NOT_FOUND.get())
);
final Customer customer = findById(requestDriver.getCustomerId());
log.info("Begin sending message");
log.info("[+] Begin sending message");
rabbitTemplate.convertAndSend("customer.exchange", "customer.routingkey", requestDriver);
log.info("message send good");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nas.driver.command;


import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class AcceptRequestCustomer {
private String customerId;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.nas.driver.controller;


import com.nas.driver.command.AcceptRequestCustomer;
import com.nas.driver.dto.DriverDto;
import com.nas.driver.dto.NotificationDriverDto;
import com.nas.driver.dto.mapper.DriverMapper;
import com.nas.driver.dto.mapper.NotificationDriverMapper;
import com.nas.driver.model.Driver;
import com.nas.driver.model.NotificationDriver;
import com.nas.driver.service.notification.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.nas.core.constants.ResourcePath.*;

Expand All @@ -23,11 +27,19 @@ public class NotificationDriverController {

private final NotificationService notificationService;
private final NotificationDriverMapper notificationDriverMapper;
private final DriverMapper driverMapper;


@GetMapping(DRIVERS + "/{driverId}")
public ResponseEntity<Page<NotificationDriverDto>> getNotificationsByDriverId(@PathVariable("driverId") final String driverId, Pageable pageable){
final Page<NotificationDriver> notificationDrivers = notificationService.getNotificationsByDriverId(pageable, driverId);
public ResponseEntity<Page<NotificationDriverDto>> getNotificationsByDriverId(@PathVariable("driverId") final String driverId){
final List<NotificationDriver> notificationDriverList = notificationService.getNotificationsByDriverId(driverId);
final Page<NotificationDriver> notificationDrivers = new PageImpl<>(notificationDriverList);
return ResponseEntity.ok(notificationDrivers.map(notificationDriverMapper::toDto));
}
@PostMapping(REQUEST + "/{driverId}")
public ResponseEntity<DriverDto> acceptRequest(@PathVariable("driverId") final String driverId,
@RequestBody final AcceptRequestCustomer acceptRequestCustomer){
final Driver driver = notificationService.acceptRequest(driverId, acceptRequestCustomer);
return ResponseEntity.ok(driverMapper.toDto(driver));
}
}
3 changes: 3 additions & 0 deletions driver-service/src/main/java/com/nas/driver/model/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static Driver create(final DriverCommand driverCommand){
driver.driverStatus = DriverStatus.AVAILABLE;
return driver;
}
public void addToDriver(NotificationDriver notificationDriver){
this.notificationDrivers.add(notificationDriver);
}
public void updateInfo(final DriverCommand driverCommand){
this.firstName = driverCommand.getFirstName();
this.lastName = driverCommand.getLastName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface NotificationDriverRepository extends JpaRepository<NotificationDriver, String> {

Page<NotificationDriver> findAllByDriverId(Pageable pageable, String driverId);
List<NotificationDriver> findAllByDriverId(String driverId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.nas.driver.service.notification;


import com.nas.driver.command.AcceptRequestCustomer;
import com.nas.driver.model.Driver;
import com.nas.driver.model.NotificationDriver;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;


import java.util.List;

public interface NotificationService {

Page<NotificationDriver> getNotificationsByDriverId(Pageable pageable, String driverId);
List<NotificationDriver> getNotificationsByDriverId(String driverId);
Driver acceptRequest(final String driverId, final AcceptRequestCustomer acceptRequestCustomer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

import com.nas.core.exception.BusinessException;
import com.nas.core.exception.ExceptionPayloadFactory;
import com.nas.driver.command.AcceptRequestCustomer;
import com.nas.driver.model.Driver;
import com.nas.driver.model.NotificationDriver;
import com.nas.driver.repository.DriverRepository;
import com.nas.driver.repository.NotificationDriverRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
Expand All @@ -21,14 +26,29 @@ public class NotificationServiceImpl implements NotificationService{

private final NotificationDriverRepository notificationDriverRepository;
private final DriverRepository driverRepository;
private final RabbitTemplate rabbitTemplate;


@Override
public Page<NotificationDriver> getNotificationsByDriverId(Pageable pageable, String driverId) {
public List<NotificationDriver> getNotificationsByDriverId(String driverId) {
log.info("Begin fetching driver with id {}", driverId);
final Driver driver = driverRepository.findById(driverId).orElseThrow(
() -> new BusinessException(ExceptionPayloadFactory.DRIVER_NOT_FOUND.get())
);
return notificationDriverRepository.findAllByDriverId(pageable, driver.getId());
return notificationDriverRepository.findAllByDriverId(driver.getId());
}

@Override
public Driver acceptRequest(String driverId, final AcceptRequestCustomer acceptRequestCustomer) {
log.info("Begin fetching driver with id {}", driverId);
final Driver driver = driverRepository.findById(driverId).orElseThrow(
() -> new BusinessException(ExceptionPayloadFactory.DRIVER_NOT_FOUND.get()));
log.info("Driver with id {} fetched successfully", driverId);

final List<NotificationDriver> notificationDrivers = getNotificationsByDriverId(driverId);
notificationDrivers.stream().filter(nt -> nt.getId().equals(acceptRequestCustomer.getCustomerId())).forEach(driver::addToDriver);
log.info("[+] Begin sending acceptation to customers...");
rabbitTemplate.convertAndSend(driverId);
return driverRepository.save(driver);
}
}