Skip to content

Pair demo #6

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions backend/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ For further reference, please consider the following sections:
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/3.2.2/maven-plugin/reference/html/)
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/3.2.2/maven-plugin/reference/html/#build-image)
* [Spring Web](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#web)
* [Spring Boot DevTools](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#using.devtools)
* [Spring Data JPA](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#data.sql.jpa-and-spring-data)
* [Validation](https://docs.spring.io/spring-boot/docs/3.2.2/reference/htmlsingle/index.html#io.validation)

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

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

1 change: 0 additions & 1 deletion backend/README.md

This file was deleted.

21 changes: 3 additions & 18 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.reactpairdemo</groupId>
<groupId>com.pairdemo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
Expand All @@ -22,34 +22,19 @@
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.reactpairdemo.demo;
package com.pairdemo.demo;

import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
Expand All @@ -11,9 +11,10 @@ public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
public ModelMapper modelMapper(){
return new ModelMapper();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pairdemo.demo.controller;

import com.pairdemo.demo.dto.ProductDto;
import com.pairdemo.demo.entity.Product;
import com.pairdemo.demo.service.ProductService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.pairdemo.demo.utils.Constants.BASE_URL;

@RestController
@RequestMapping(BASE_URL)
public class ProductController {

private final ProductService productService;

@Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping("/search")
public ResponseEntity<List<ProductDto>> searchProductByName(@RequestParam("name") String name) {
return new ResponseEntity<>(productService.searchProductByName(name), HttpStatus.OK) ;
}

@PostMapping
public ResponseEntity<ProductDto> createProduct(@Valid @RequestBody ProductDto productDto) {

return new ResponseEntity<>(productService.createProduct(productDto), HttpStatus.CREATED);
}
@DeleteMapping("/{id}")
public ResponseEntity<String> createProduct(@PathVariable Long id) {

return new ResponseEntity<>(productService.deleteProduct(id), HttpStatus.CREATED) ;
}

}
16 changes: 16 additions & 0 deletions backend/src/main/java/com/pairdemo/demo/dto/ProductDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pairdemo.demo.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;

@Data
public class ProductDto {
private Long id;
@NotBlank(message = "Please enter a name")
@Size(min = 3,message = "")
private String name;
@NotNull(message = "Please enter a price")
private Double Price;
}
18 changes: 18 additions & 0 deletions backend/src/main/java/com/pairdemo/demo/entity/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pairdemo.demo.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Data;

@Entity
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long id;
private String name;
private Double Price;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pairdemo.demo.exceptions;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class ErrorResponse {
String message;
String title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.pairdemo.demo.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponse> handleResourceNotFound(Exception e){
ErrorResponse errorResponse=ErrorResponse.builder().message(e.getMessage()).title(HttpStatus.NOT_FOUND.getReasonPhrase()).build();
return new ResponseEntity<>(errorResponse,HttpStatus.NOT_FOUND);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String,String>> handleResourceNotFound(MethodArgumentNotValidException e){
Map<String,String> resp=new HashMap<>();
e.getBindingResult().getAllErrors().forEach(objectError -> {
resp.put(((FieldError)objectError).getField(),objectError.getDefaultMessage());
});


return new ResponseEntity<>(resp,HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e){
ErrorResponse errorResponse=ErrorResponse.builder().message(e.getMessage()).title(HttpStatus.NOT_ACCEPTABLE.getReasonPhrase()).build();
return new ResponseEntity<>(errorResponse,HttpStatus.NOT_ACCEPTABLE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pairdemo.demo.exceptions;

public class ResourceNotFoundException extends RuntimeException{
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pairdemo.demo.repository;

import com.pairdemo.demo.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProductRepository extends JpaRepository<Product,Long> {
public List<Product> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pairdemo.demo.service;

import com.pairdemo.demo.dto.ProductDto;

import java.util.List;

public interface ProductService {
List<ProductDto> searchProductByName(String name);

ProductDto createProduct(ProductDto productDto);
String deleteProduct(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.pairdemo.demo.service;

import com.pairdemo.demo.dto.ProductDto;
import com.pairdemo.demo.entity.Product;
import com.pairdemo.demo.repository.ProductRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.pairdemo.demo.utils.Constants.ID_NOT_FOUND;
import static com.pairdemo.demo.utils.Constants.SUCESS_DELETE;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService{

private final ProductRepository productRepository;

private final ModelMapper modelMapper;

@Autowired
public ProductServiceImpl(ProductRepository productRepository, ModelMapper modelMapper) {
this.productRepository = productRepository;
this.modelMapper = modelMapper;
}

@Override
public List<ProductDto> searchProductByName(String name) {
List<Product> productList = productRepository.findByName(name);
// System.out.println(productList);
// return productList.stream().map(product -> modelMapper.map(product,ProductDto.class)).collect(Collectors.toList());
return productList.stream().map(product -> modelMapper.map(product, ProductDto.class)).filter(productDto -> productDto.getName().equalsIgnoreCase(name)).collect(Collectors.toList());
}

@Override
public ProductDto createProduct(ProductDto productDto) {
Product product = modelMapper.map(productDto, Product.class);
Product product1 = productRepository.save(product);
return modelMapper.map(product, ProductDto.class);
}

@Override
public String deleteProduct(Long id) {
Optional<Product> productOptional = productRepository.findById(id);
if (productOptional.isEmpty()) {
throw new ResourceNotFoundException(ID_NOT_FOUND);
}
productRepository.deleteById(id);
return SUCESS_DELETE;
}
}
7 changes: 7 additions & 0 deletions backend/src/main/java/com/pairdemo/demo/utils/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pairdemo.demo.utils;

public class Constants {
public static final String BASE_URL = "/api/v1/product";
public static final String ID_NOT_FOUND= "Given Id Not Found!!";
public static final String SUCESS_DELETE= "Success to Delete";
}
1 change: 1 addition & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


spring.datasource.url=jdbc:mysql://localhost:3306/pairdemo?useSSL=false&ServerTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=root
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.reactpairdemo.demo;
package com.pairdemo.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
1 change: 1 addition & 0 deletions backend/target/classes/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@


spring.datasource.url=jdbc:mysql://localhost:3306/pairdemo?useSSL=false&ServerTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=root
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.