Skip to content

Commit 8d57d44

Browse files
author
Somnath More
committed
Pair demo
1 parent 67759ff commit 8d57d44

31 files changed

+204
-26
lines changed

backend/HELP.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ For further reference, please consider the following sections:
77
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/3.2.2/maven-plugin/reference/html/)
88
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/3.2.2/maven-plugin/reference/html/#build-image)
99
* [Spring Web](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#web)
10-
* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#using.devtools)
1110
* [Spring Data JPA](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#data.sql.jpa-and-spring-data)
11+
* [Validation](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#io.validation)
1212

1313
### Guides
1414
The following guides illustrate how to use some features concretely:
1515

1616
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
1717
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
1818
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)
19-
* [Accessing data with MySQL](https://spring.io/guides/gs/accessing-data-mysql/)
2019
* [Accessing Data with JPA](https://spring.io/guides/gs/accessing-data-jpa/)
20+
* [Accessing data with MySQL](https://spring.io/guides/gs/accessing-data-mysql/)
21+
* [Validation](https://spring.io/guides/gs/validating-form-input/)
2122

backend/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

backend/pom.xml

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<version>3.2.2</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
11-
<groupId>com.reactpairdemo</groupId>
11+
<groupId>com.pairdemo</groupId>
1212
<artifactId>demo</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
<name>demo</name>
@@ -22,34 +22,19 @@
2222
<artifactId>modelmapper</artifactId>
2323
<version>3.1.1</version>
2424
</dependency>
25-
<dependency>
26-
<groupId>org.slf4j</groupId>
27-
<artifactId>jul-to-slf4j</artifactId>
28-
</dependency>
29-
30-
<!-- <dependency>-->
31-
<!-- <groupId>org.springframework.boot</groupId>-->
32-
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
33-
<!-- </dependency>-->
3425
<dependency>
3526
<groupId>org.springframework.boot</groupId>
36-
<artifactId>spring-boot-starter-validation</artifactId>
27+
<artifactId>spring-boot-starter-data-jpa</artifactId>
3728
</dependency>
3829
<dependency>
3930
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
<artifactId>spring-boot-starter-validation</artifactId>
4132
</dependency>
4233
<dependency>
4334
<groupId>org.springframework.boot</groupId>
4435
<artifactId>spring-boot-starter-web</artifactId>
4536
</dependency>
4637

47-
<dependency>
48-
<groupId>org.springframework.boot</groupId>
49-
<artifactId>spring-boot-devtools</artifactId>
50-
<scope>runtime</scope>
51-
<optional>true</optional>
52-
</dependency>
5338
<dependency>
5439
<groupId>com.mysql</groupId>
5540
<artifactId>mysql-connector-j</artifactId>

backend/src/main/java/com/reactpairdemo/demo/DemoApplication.java renamed to backend/src/main/java/com/pairdemo/demo/DemoApplication.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.reactpairdemo.demo;
1+
package com.pairdemo.demo;
22

33
import org.modelmapper.ModelMapper;
44
import org.springframework.boot.SpringApplication;
@@ -11,9 +11,10 @@ public class DemoApplication {
1111
public static void main(String[] args) {
1212
SpringApplication.run(DemoApplication.class, args);
1313
}
14+
1415
@Bean
15-
public ModelMapper modelMapper() {
16-
return new ModelMapper();
17-
}
16+
public ModelMapper modelMapper(){
17+
return new ModelMapper();
18+
}
1819

1920
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.pairdemo.demo.controller;
2+
3+
import com.pairdemo.demo.dto.ProductDto;
4+
import com.pairdemo.demo.entity.Product;
5+
import com.pairdemo.demo.service.ProductService;
6+
import jakarta.validation.Valid;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.*;
11+
12+
import java.util.List;
13+
14+
import static com.pairdemo.demo.utils.Constants.BASE_URL;
15+
16+
@RestController
17+
@RequestMapping(BASE_URL)
18+
public class ProductController {
19+
20+
21+
@Autowired
22+
private ProductService productService;
23+
@GetMapping("/search")
24+
public ResponseEntity<List<ProductDto>> searchProductByName(@RequestParam("name") String name) {
25+
return new ResponseEntity<>(productService.searchProductByName(name), HttpStatus.OK) ;
26+
}
27+
28+
@PostMapping
29+
public ResponseEntity<ProductDto> createProduct(@Valid @RequestBody ProductDto productDto) {
30+
31+
return new ResponseEntity<>(productService.createProduct(productDto), HttpStatus.CREATED) ;
32+
}
33+
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.pairdemo.demo.dto;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.NotNull;
5+
import jakarta.validation.constraints.Size;
6+
import lombok.Data;
7+
8+
@Data
9+
public class ProductDto {
10+
private Long id;
11+
@NotBlank(message = "Please enter a name")
12+
@Size(min = 3,message = "")
13+
private String name;
14+
@NotNull(message = "Please enter a price")
15+
private Double Price;
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pairdemo.demo.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import lombok.Data;
8+
9+
@Entity
10+
@Data
11+
public class Product {
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.IDENTITY)
14+
15+
private Long id;
16+
private String name;
17+
private Double Price;
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.pairdemo.demo.exceptions;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
public class ErrorResponse {
9+
String message;
10+
String title;
11+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.pairdemo.demo.exceptions;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.validation.FieldError;
6+
import org.springframework.web.bind.MethodArgumentNotValidException;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@RestControllerAdvice
14+
public class GlobalExceptionHandler {
15+
16+
@ExceptionHandler(ResourceNotFoundException.class)
17+
public ResponseEntity<ErrorResponse> handleResourceNotFound(Exception e){
18+
ErrorResponse errorResponse=ErrorResponse.builder().message(e.getMessage()).title(HttpStatus.NOT_FOUND.getReasonPhrase()).build();
19+
return new ResponseEntity<>(errorResponse,HttpStatus.NOT_FOUND);
20+
}
21+
22+
@ExceptionHandler(MethodArgumentNotValidException.class)
23+
public ResponseEntity<Map<String,String>> handleResourceNotFound(MethodArgumentNotValidException e){
24+
Map<String,String> resp=new HashMap<>();
25+
e.getBindingResult().getAllErrors().forEach(objectError -> {
26+
resp.put(((FieldError)objectError).getField(),objectError.getDefaultMessage());
27+
});
28+
29+
30+
return new ResponseEntity<>(resp,HttpStatus.BAD_REQUEST);
31+
}
32+
33+
@ExceptionHandler(IllegalArgumentException.class)
34+
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e){
35+
ErrorResponse errorResponse=ErrorResponse.builder().message(e.getMessage()).title(HttpStatus.NOT_ACCEPTABLE.getReasonPhrase()).build();
36+
return new ResponseEntity<>(errorResponse,HttpStatus.NOT_ACCEPTABLE);
37+
}
38+
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.pairdemo.demo.exceptions;
2+
3+
public class ResourceNotFoundException extends RuntimeException{
4+
public ResourceNotFoundException(String message) {
5+
super(message);
6+
}
7+
}

0 commit comments

Comments
 (0)