Commit 1daecc8a authored by Tejas Sharma's avatar Tejas Sharma

Initial commit added pagination and error handling to the basic order-service...

Initial commit added pagination and error handling to the basic order-service to add and retrieve orders
parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nisum</groupId>
<artifactId>ordering-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>notification-service</artifactId>
<packaging>jar</packaging>
<name>notification-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package org.nisum;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
package org.nisum;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.nisum</groupId>
<artifactId>ordering-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>order-service</artifactId>
<packaging>jar</packaging>
<name>order-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb-reactive -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version> <!-- Choose the version that fits your needs -->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>3.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<compilerArgument>-parameters</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
package org.nisum;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class OrderServiceApplication
{
public static void main( String[] args )
{
SpringApplication.run(OrderServiceApplication.class);
}
}
package org.nisum.controller;
import org.nisum.model.Order;
import org.nisum.service.OrderService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/orders")
public class OrderController {
private final OrderService orderService;
public OrderController(OrderService orderService) {
this.orderService = orderService;
}
// @PostMapping("/createOrder")
// public Mono<Order> createOrder(@RequestBody Order order){
// return orderService.createOrder(order);
// }
// @GetMapping("/{id}")
// public Mono<Order> getOrderById(@PathVariable String id){
// return orderService.getOrderById(id);
// }
@GetMapping(value = "/getAllOrders", produces = "application/json")
public Flux<Order> getAllOrders(@RequestParam(name = "page",defaultValue = "0") int page, @RequestParam(name = "size", defaultValue = "10") int size){
return orderService.getAllOrders(page,size);
}
//
// @GetMapping("/getAllOrders")
// public Flux<Order> getOrders(){
// return orderService.getOrders();
// }
}
package org.nisum.exception;
import lombok.Data;
@Data
public class ErrorResponse {
private int status;
private String message;
private String details;
public ErrorResponse(int status, String message, String details) {
this.status = status;
this.message = message;
this.details = details;
}
}
package org.nisum.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import reactor.core.publisher.Mono;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(OrderNotFoundException.class)
public Mono<ResponseEntity<ErrorResponse>> handleOrderNotFound(OrderNotFoundException ex){
ErrorResponse errorResponse = new ErrorResponse(404,"Order not found", ex.getMessage());
return Mono.just(ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse));
}
@ExceptionHandler(Exception.class)
public Mono<ResponseEntity<ErrorResponse>> handleGeneralException(Exception ex){
ErrorResponse errorResponse = new ErrorResponse(500, "Internal Server Error", ex.getMessage());
return Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse));
}
}
package org.nisum.exception;
public class OrderNotFoundException extends RuntimeException{
public OrderNotFoundException(String message) {
super(message);
}
}
package org.nisum.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
@Data
@Document(collection = "orders")
public class Order {
@Id
private String id;
private String productId;
private String productName;
private int quantity;
private double price;
private String status;
private LocalDateTime timeStamp;
}
package org.nisum.repository;
import org.nisum.model.Order;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Repository
public interface OrderRepository extends ReactiveMongoRepository<Order,String> {
Flux<Order> findAllBy(Pageable pageable);
}
package org.nisum.service;
import lombok.var;
import org.nisum.exception.OrderNotFoundException;
import org.nisum.model.Order;
import org.nisum.repository.OrderRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.LocalDateTime;
@Service
public class OrderService {
public static final Logger log = LoggerFactory.getLogger(OrderService.class);
@Autowired
private OrderRepository orderRepository;
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
// private final KafkaTemplate<String,String> kafkaTemplate;
// public Mono<Order> createOrder(Order order){
//
// if(order.getProductId() == null){
// return Mono.error(new OrderNotFoundException("Order Id cannot be null"));
// }
// order.setStatus("Placed");
// order.setTimeStamp(LocalDateTime.now());
// return orderRepository.save(order).doOnSuccess(
// order1 -> kafkaTemplate
// .send("order-status-topic",
// "Order with product id: " + order1.getProductId()
// +" is placed successfully" ));
//
// }
public Flux<Order> getAllOrders(int page, int size) {
Pageable pageable = PageRequest.of(page,size);
return orderRepository.findAllBy(pageable);
}
// public Flux<Order> getOrders(){
// return orderRepository.findAll();
// }
//
// public Mono<Order> getOrderById(String id) {
// return orderRepository.findById(id);
// }
}
server:
port: 8081
spring:
kafka:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
data:
mongodb:
uri: mongodb://localhost:27017/orders_db
package org.nisum;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class OrderServiceApplicationTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public OrderServiceApplicationTest(String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( OrderServiceApplicationTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.nisum</groupId>
<artifactId>ordering-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ordering-app</name>
<url>http://maven.apache.org</url>
<modules>
<module>notification-service</module>
<module>order-service</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package org.nisum;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
package org.nisum;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment