Skip to content
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
@@ -1,7 +1,6 @@
package com.podzilla.courier.controllers;

import com.podzilla.courier.dtos.couriers.CourierResponseDto;
import com.podzilla.courier.dtos.couriers.CreateCourierRequestDto;
import com.podzilla.courier.dtos.couriers.UpdateCourierRequestDto;
import com.podzilla.courier.services.courier.CourierService;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -12,7 +11,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -58,19 +56,6 @@ public ResponseEntity<CourierResponseDto> getCourierById(
.orElse(ResponseEntity.notFound().build());
}

@PostMapping
@Operation(summary = "Create a new courier",
description = "Adds a new courier to the system.")
@ApiResponse(responseCode = "200",
description = "Courier successfully created")
public ResponseEntity<CourierResponseDto> createCourier(
@RequestBody(description = "Details of the courier to create")
@org.springframework.web.bind.annotation.RequestBody
final CreateCourierRequestDto courier) {
LOGGER.info("Received request to add courier");
return ResponseEntity.ok(courierService.createCourier(courier));
}

@PutMapping("/{id}")
@Operation(summary = "Update a courier",
description = "Updates details of an existing courier.")
Expand All @@ -82,8 +67,7 @@ public ResponseEntity<CourierResponseDto> updateCourier(
@Parameter(description = "ID of the courier to update")
@PathVariable final String id,
@RequestBody(description = "Updated courier details")
@org.springframework.web.bind.annotation.RequestBody
final UpdateCourierRequestDto courier) {
@org.springframework.web.bind.annotation.RequestBody final UpdateCourierRequestDto courier) {
LOGGER.info("Received request to update courier with id {}", id);
return courierService.updateCourier(id, courier)
.map(ResponseEntity::ok)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
@Getter
@AllArgsConstructor
public class CreateCourierRequestDto {
@NotNull(message = "Courier ID is required")
private String courierId;
@NotNull(message = "Name is required")
private String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.podzilla.courier.events;

import com.podzilla.courier.dtos.couriers.CreateCourierRequestDto;
import com.podzilla.courier.services.courier.CourierService;
import com.podzilla.mq.EventsConstants;
import com.podzilla.mq.events.BaseEvent;
import com.podzilla.mq.events.CourierRegisteredEvent;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class CourierRegisteredEventConsumer {
private final CourierService courierService;

public CourierRegisteredEventConsumer(final CourierService courierService) {
this.courierService = courierService;
}

@RabbitListener(queues = EventsConstants.COURIER_USER_EVENT_QUEUE)
public void consumeCourierRegisteredEvent(final BaseEvent event) {
if (event instanceof CourierRegisteredEvent) {
CourierRegisteredEvent courierRegisteredEvent = (CourierRegisteredEvent) event;
CreateCourierRequestDto courier = new CreateCourierRequestDto(
courierRegisteredEvent.getCourierId(),
courierRegisteredEvent.getName(),
courierRegisteredEvent.getMobileNo()
);
courierService.createCourier(courier);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/podzilla/courier/mappers/CourierMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import com.podzilla.courier.dtos.couriers.CourierResponseDto;
import com.podzilla.courier.dtos.couriers.CreateCourierRequestDto;
import com.podzilla.courier.dtos.couriers.UpdateCourierRequestDto;
import com.podzilla.courier.models.Courier;

public class CourierMapper {

public static Courier toEntity(CreateCourierRequestDto dto) {
public static Courier toEntity(final CreateCourierRequestDto dto) {
Courier courier = new Courier();
courier.setName(dto.getName());
courier.setMobileNo(dto.getMobileNo());
courier.setId(dto.getCourierId());
return courier;
}

public static CourierResponseDto toCreateResponseDto(Courier courier) {
public static CourierResponseDto toCreateResponseDto(final Courier courier) {
return new CourierResponseDto(courier.getId(), courier.getName(), courier.getMobileNo(), courier.getStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public DeliveryTaskResponseDto updateDeliveryTaskLocation(final String orderId,
Optional<DeliveryTask> updatedDeliveryTask = deliveryTaskRepository.findByOrderId(orderId).stream().findFirst();
if (updatedDeliveryTask.isPresent()) {
DeliveryTask deliveryTask = updatedDeliveryTask.get();
deliveryTask.setCourierLongitude(latitude);
deliveryTask.setCourierLatitude(latitude);
deliveryTask.setCourierLongitude(longitude);
deliveryTaskRepository.save(deliveryTask);
LOGGER.debug("Location updated for delivery task with order id: {}", orderId);
Expand Down
17 changes: 0 additions & 17 deletions src/test/java/com/podzilla/courier/CourierApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,6 @@ void getCourierByIdNotFound() throws Exception {
.andExpect(status().isNotFound());
}

@Test
@DisplayName("POST /couriers → 200 OK with created courier")
void createCourier() throws Exception {
CreateCourierRequestDto req = new CreateCourierRequestDto("Daisy", "0222333444");
CourierResponseDto resp = new CourierResponseDto("100", "Daisy", "0222333444", CourierStatus.PICKED_UP);

Mockito.when(courierService.createCourier(Mockito.any(CreateCourierRequestDto.class))).thenReturn(resp);

mockMvc.perform(post("/couriers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(req))
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is("100")))
.andExpect(jsonPath("$.status", is("PICKED_UP")));
}

@Test
@DisplayName("PUT /couriers/{id} → 200 OK when update succeeds")
void updateCourierFound() throws Exception {
Expand Down