Skip to content

Commit

Permalink
class3 second half
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljain1203 committed Oct 17, 2023
1 parent ee474e1 commit 30e4cfc
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 20 deletions.
30 changes: 26 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.4</version>
<!-- <dependency>-->
<!-- <groupId>org.apache.httpcomponents</groupId>-->
<!-- <artifactId>httpclient</artifactId>-->
<!-- <version>4.3.4</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency> <groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -42,6 +47,23 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
//@ControllerAdvice
public class ExceptionAdives {
@ExceptionHandler({Exception.class})
//@ExceptionHandler({Exception.class})
public ResponseEntity<String> handleException(Exception e) {
return new ResponseEntity<>("Kuch toh phat hai", HttpStatus.INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.productservice_proxy.controllers;

import com.example.productservice_proxy.clients.IClientProductDto;
import com.example.productservice_proxy.clients.fakestore.dto.FakeStoreProductDto;
import com.example.productservice_proxy.dtos.ProductDto;
import com.example.productservice_proxy.models.Categories;
import com.example.productservice_proxy.models.Product;
Expand Down Expand Up @@ -52,9 +53,10 @@ public ResponseEntity<Product> getSingleProduct(@PathVariable("id") Long product
}

@PostMapping()
public ResponseEntity<Product> addNewProduct(@RequestBody IClientProductDto productDto) {
Product product = this.productService.addNewProduct(productDto);
ResponseEntity<Product> responseEntity = new ResponseEntity<>(product, HttpStatus.OK);
public ResponseEntity<Product> addNewProduct(@RequestBody ProductDto productDto) {
Product product = getProduct(productDto);
Product savedproduct = this.productService.addNewProduct(product);
ResponseEntity<Product> responseEntity = new ResponseEntity<>(savedproduct, HttpStatus.OK);
return responseEntity;
}

Expand All @@ -81,8 +83,21 @@ public String deleteProduct(@PathVariable("productId") Long productId) {
return "Deleting a product with id: " + productId;
}

@ExceptionHandler({NullPointerException.class, IllegalArgumentException.class})
//@ExceptionHandler({NullPointerException.class, IllegalArgumentException.class})
public ResponseEntity<String> handleException(Exception e) {
return new ResponseEntity<>("Kuch toh phat hai", HttpStatus.INTERNAL_SERVER_ERROR);
}

private Product getProduct(ProductDto productDto) {
Product product = new Product();
product.setId(productDto.getId());
product.setTitle(productDto.getTitle());
product.setPrice(productDto.getPrice());
Categories category = new Categories();
category.setName(productDto.getCategory());
product.setCategory(category);
product.setImageUrl(productDto.getImage());
product.setDescription(productDto.getDescription());
return product;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.example.productservice_proxy.models;

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@MappedSuperclass
public abstract class BaseModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Date createdAt;
private Date lastUpdatedAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.productservice_proxy.models;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -8,8 +11,10 @@

@Setter
@Getter
@Entity
public class Categories extends BaseModel{
private String name;
private String description;
@OneToMany(mappedBy = "category", cascade= CascadeType.ALL)
private List<Product> productList;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.example.productservice_proxy.models;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class Product extends BaseModel{
private String title;
private double price;
private String description;
@ManyToOne(cascade= CascadeType.ALL)
private Categories category;
private String imageUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.productservice_proxy.repostries;

import com.example.productservice_proxy.models.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductRepo extends JpaRepository<Product, Long> {
Product save(Product product); // (save) is a method of JpaRepository (interface)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.ArrayList;
import java.util.List;

@Service
//@Service
public class FakeStoreProductService implements IProductService {

private RestTemplateBuilder restTemplateBuilder;
Expand Down Expand Up @@ -78,13 +78,18 @@ public Product getSingleProduct(Long productId) {
return product;
}

// @Override
// public Product addNewProduct(IClientProductDto productDto) {
// RestTemplate restTemplate= restTemplateBuilder.build();
// //restTemplate.postForEntity("https://fakestoreapi.com/products", productDto, ProductDto.class);
// // saving the data for db
// Product product = getProduct((FakeStoreProductDto) productDto);
// return product;
// }

@Override
public Product addNewProduct(IClientProductDto productDto) {
RestTemplate restTemplate= restTemplateBuilder.build();
restTemplate.postForEntity("https://fakestoreapi.com/products", productDto, ProductDto.class);
// saving the data for db
Product product = getProduct((FakeStoreProductDto) productDto);
return product;
public Product addNewProduct(Product product) {
return null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IProductService {

Product getSingleProduct(Long productId);

Product addNewProduct(IClientProductDto productDto);
Product addNewProduct(Product product);

Product updateProduct(Long productId, Product product);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
package com.example.productservice_proxy.services;

public class SelfProductService {
import com.example.productservice_proxy.models.Product;
import com.example.productservice_proxy.repostries.ProductRepo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelfProductService implements IProductService {

ProductRepo productRepo;

public SelfProductService(ProductRepo productRepo) {
this.productRepo = productRepo;
}
@Override
public List<Product> getAllProducts() {
return null;
}

@Override
public Product getSingleProduct(Long productId) {
return null;
}

@Override
public Product addNewProduct(Product product) {
this.productRepo.save(product);
return product;
}

@Override
public Product updateProduct(Long productId, Product product) {
return null;
}

@Override
public String deleteProduct(Long productId) {
return null;
}
}
7 changes: 6 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

0 comments on commit 30e4cfc

Please sign in to comment.