Compare commits
3 Commits
ba13f21102
...
7cd3b063bc
| Author | SHA1 | Date | |
|---|---|---|---|
| 7cd3b063bc | |||
| f0fcbb588d | |||
| 8dc84d2110 |
@ -0,0 +1,8 @@
|
||||
package com.techivw.webprice.application.exceptions;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package com.techivw.webprice.application.services;
|
||||
|
||||
import com.techivw.webprice.application.exceptions.NotFoundException;
|
||||
import com.techivw.webprice.application.ports.in.PriceServicePort;
|
||||
import com.techivw.webprice.application.ports.out.PriceRepositoryPort;
|
||||
import com.techivw.webprice.domain.Price;
|
||||
@ -16,6 +17,9 @@ public class PriceService implements PriceServicePort {
|
||||
|
||||
@Override
|
||||
public Price getPriceWithHighestPriorityByDateTimeAndProductIdAndBrandId(LocalDateTime dateTime, Long productId, Long brandId) {
|
||||
return priceRepositoryPort.findHighestPriorityPriceByDateTimeAndProductIdAndBrandId(dateTime, productId, brandId).orElse(null);
|
||||
return priceRepositoryPort.findHighestPriorityPriceByDateTimeAndProductIdAndBrandId(dateTime, productId, brandId)
|
||||
.orElseThrow(() ->
|
||||
new NotFoundException(String.format(
|
||||
"Price for product %d of brand %d not found for date %s", productId, brandId, dateTime)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,3 +10,8 @@ spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
# H2 console
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
# Flyway
|
||||
spring.flyway.enabled=true
|
||||
spring.flyway.locations=filesystem:infrastructure/out/sql-repository/sql/migration
|
||||
spring.flyway.baseline-on-migrate=true
|
||||
@ -3,6 +3,7 @@ package com.techivw.webprice.infrastructure.in.controllers.adapters;
|
||||
import com.techivw.webprice.application.ports.in.PriceServicePort;
|
||||
import com.techivw.webprice.domain.Price;
|
||||
import com.techivw.webprice.infrastructure.in.controllers.model.PriceResponse;
|
||||
import com.techivw.webprice.infrastructure.in.controllers.model.mappers.PriceResponseMapper;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
@ -35,8 +36,8 @@ public class PriceControllerAdapter {
|
||||
description = "Price information found",
|
||||
content = @Content(mediaType = "application/json", schema = @Schema(implementation = PriceResponse.class))
|
||||
)
|
||||
@ApiResponse(responseCode = "404", description = "Price information not found for the given parameters")
|
||||
@ApiResponse(responseCode = "400", description = "Invalid request parameters")
|
||||
@ApiResponse(responseCode = "404", description = "Price information not found for the given parameters")
|
||||
public ResponseEntity<PriceResponse> getPrice(
|
||||
@Parameter(description = "Date and time for price lookup (ISO format)", example = "2025-04-30T12:00:00")
|
||||
@RequestParam(name = "dateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime,
|
||||
@ -46,14 +47,6 @@ public class PriceControllerAdapter {
|
||||
@RequestParam(name = "brandId") Long brandId) {
|
||||
|
||||
Price price = priceServicePort.getPriceWithHighestPriorityByDateTimeAndProductIdAndBrandId(dateTime, productId, brandId);
|
||||
PriceResponse priceResponse = PriceResponse.builder()
|
||||
.productId(price.getProductId())
|
||||
.brandId(price.getBrandId())
|
||||
.priceList(price.getPriceList())
|
||||
.startDate(price.getStartDate())
|
||||
.endDate(price.getEndDate())
|
||||
.price(price.getPrice())
|
||||
.build();
|
||||
return ResponseEntity.ok(priceResponse);
|
||||
return ResponseEntity.ok(PriceResponseMapper.fromPrice(price));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
package com.techivw.webprice.infrastructure.in.controllers.handlers;
|
||||
|
||||
import com.techivw.webprice.application.exceptions.NotFoundException;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class AdapterExceptionHandler {
|
||||
|
||||
@ExceptionHandler(NotFoundException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleNotFoundException(NotFoundException ex) {
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("timestamp", new Date());
|
||||
body.put("status", NOT_FOUND.value());
|
||||
body.put("error", "Not Found");
|
||||
body.put("message", ex.getMessage());
|
||||
|
||||
return new ResponseEntity<>(body, NOT_FOUND);
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,12 @@
|
||||
package com.techivw.webprice.infrastructure.in.controllers.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class PriceResponse {
|
||||
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
package com.techivw.webprice.infrastructure.in.controllers.model.mappers;
|
||||
|
||||
import com.techivw.webprice.domain.Price;
|
||||
import com.techivw.webprice.infrastructure.in.controllers.model.PriceResponse;
|
||||
|
||||
public class PriceResponseMapper {
|
||||
|
||||
public static PriceResponse fromPrice(Price price) {
|
||||
return PriceResponse.builder()
|
||||
.productId(price.getProductId())
|
||||
.brandId(price.getBrandId())
|
||||
.priceList(price.getPriceList())
|
||||
.startDate(price.getStartDate())
|
||||
.endDate(price.getEndDate())
|
||||
.price(price.getPrice())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@ -26,6 +26,15 @@
|
||||
<artifactId>application</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
<version>11.7.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,11 @@
|
||||
CREATE TABLE prices (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
brand_id BIGINT NOT NULL,
|
||||
start_date TIMESTAMP NOT NULL,
|
||||
end_date TIMESTAMP NOT NULL,
|
||||
price_list BIGINT NOT NULL,
|
||||
product_id BIGINT NOT NULL,
|
||||
priority BIGINT NOT NULL,
|
||||
price DECIMAL(10, 2) NOT NULL,
|
||||
currency VARCHAR(3)
|
||||
);
|
||||
@ -0,0 +1,6 @@
|
||||
INSERT INTO prices (brand_id, start_date, end_date, price_list, product_id, priority, price, currency)
|
||||
VALUES
|
||||
(1, '2020-06-14 00:00:00', '2020-12-31 23:59:59', 1, 35455, 0, 35.50, 'EUR'),
|
||||
(1, '2020-06-14 15:00:00', '2020-06-14 18:30:00', 2, 35455, 1, 25.45, 'EUR'),
|
||||
(1, '2020-06-15 00:00:00', '2020-06-15 11:00:00', 3, 35455, 1, 30.50, 'EUR'),
|
||||
(1, '2020-06-15 16:00:00', '2020-12-31 23:59:59', 4, 35455, 1, 38.95, 'EUR');
|
||||
4
pom.xml
4
pom.xml
@ -42,10 +42,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user