Commit 114b63c3 authored by snamdeo's avatar snamdeo

Commiting code after adding mysql database

parent faa147ee
package com.altimetrik.poc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShoppingCartAppApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingCartAppApplication.class, args);
}
}
package com.altimetrik.poc.controller;
import com.altimetrik.poc.dto.ProductDetails;
import com.altimetrik.poc.repository.FetchProductsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@RestController
@RequestMapping("/fetchData")
public class FetchProductDataController {
@Autowired
FetchProductsRepository fetchProductsRepository;
@GetMapping("/getAllProducts")
@CrossOrigin(origins="http://localhost:3000/")
public ResponseEntity<List<ProductDetails>> getAllProducts() throws SQLException, IOException {
List<ProductDetails> products = fetchProductsRepository.findAll();
System.out.println(products);
return ResponseEntity.ok(products);
}
}
package com.altimetrik.poc.controller;
import com.altimetrik.poc.dto.OrderDetails;
import com.altimetrik.poc.services.ShoppingCartServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("api/v1")
public class ShoppingCartController {
@Autowired
ShoppingCartServices shoppingCartServices;
@GetMapping("/getProducts")
public ResponseEntity<List<OrderDetails>> getProducts(){
List<OrderDetails> products = shoppingCartServices.getProducts();
return ResponseEntity.ok(products);
}
@DeleteMapping("/removeProducts")
public void removeProducts(@RequestBody OrderDetails p){
shoppingCartServices.removeProducts(p);
}
@PostMapping ("/addProducts")
@CrossOrigin(origins="http://localhost:3000/")
public void addProducts(@RequestBody OrderDetails p){
shoppingCartServices.addProducts(p);
}
@PutMapping("/updateProducts")
public void updateProducts(@RequestBody OrderDetails p){
shoppingCartServices.updateProduct(p);
}
}
package com.altimetrik.poc.controller;
import com.altimetrik.poc.dto.Customer;
import com.altimetrik.poc.repository.UserLoggedInRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api/v1")
public class UserDataController {
@Autowired
UserLoggedInRepository userLoggedInRepository;
@PostMapping("/userLoggedIn")
@CrossOrigin(origins="http://localhost:3000/")
public void addUser(@RequestBody Customer p){
System.out.println(p);
userLoggedInRepository.save(p);
}
}
package com.altimetrik.poc.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Customer {
@Id
@GeneratedValue
private Integer id;
private Integer userId;
private String firstName;
private String lastName;
private String emailId;
@OneToMany(
mappedBy = "customer",
cascade = CascadeType.ALL
)
private List<OrderDetails> orderDetails = new ArrayList<OrderDetails>();
@OneToOne
@JoinColumn(name = "customer_valet_details_id")
private CustomerValetDetails customerValetDetails;
public Customer(Integer userId, String firstName, String lastName, String emailId) {
this.userId = userId;
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
}
package com.altimetrik.poc.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "UserdataDetails")
@Entity
public class CustomerValetDetails {
@Id
@GeneratedValue
private Integer id;
private Integer valetAmount;
@OneToOne(mappedBy = "customerValetDetails")
private Customer customer;
}
\ No newline at end of file
package com.altimetrik.poc.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class OrderDetails {
@Id
@GeneratedValue
private Integer id;
private Integer orderNumber;
private Integer taxPrice;
private Integer shippingPrice;
private Integer totalPrice;
private Integer itemsPrice;
private Integer[] productIds;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
}
package com.altimetrik.poc.dto;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class ProductDetails {
public ProductDetails() {
}
@Id
@GeneratedValue
private Integer id;
public ProductDetails(Integer id, Integer price, String name, String image) {
this.id = id;
this.price = price;
this.name = name;
this.image = image;
}
private Integer price;
private String name;
private String image;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
package com.altimetrik.poc.repository;
import com.altimetrik.poc.dto.ProductDetails;;
import org.springframework.data.jpa.repository.JpaRepository;
public interface FetchProductsRepository extends JpaRepository<ProductDetails,Integer> {
}
package com.altimetrik.poc.repository;
import com.altimetrik.poc.dto.OrderDetails;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ShoppingCartRepository extends JpaRepository<OrderDetails,Integer> {
}
package com.altimetrik.poc.repository;
import com.altimetrik.poc.dto.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserLoggedInRepository extends JpaRepository<Customer,Integer> {
}
package com.altimetrik.poc.services;
import com.altimetrik.poc.dto.OrderDetails;
import java.util.List;
public interface ShoppingCartServices {
public List<OrderDetails> getProducts();
public void addProducts(OrderDetails p);
public void removeProducts(OrderDetails p);
public void updateProduct(OrderDetails p);
}
package com.altimetrik.poc.services;
import com.altimetrik.poc.dto.OrderDetails;
import com.altimetrik.poc.repository.ShoppingCartRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ShoppingCartServicesImpl implements ShoppingCartServices{
@Autowired
ShoppingCartRepository shoppingCartRepository;
List<OrderDetails> product = new ArrayList<>();
public List<OrderDetails> getProducts(){
return shoppingCartRepository.findAll();
}
public void addProducts(OrderDetails p){
shoppingCartRepository.save(p);
}
public void removeProducts(OrderDetails p){
product.remove(p);
}
public void updateProduct(OrderDetails p){
product.remove(p);
}
}
package com.altimetrik.poc.controller;
import com.altimetrik.poc.dto.ProductDetails;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@SpringBootTest
public class FetchProductControllerTest {
@MockBean private JdbcTemplate jdbcTemplate;
@Autowired private FetchProductDataController fetchProductDataController;
@Test
public void getAllTheProducts() throws SQLException, IOException {
String sql = "Select * from ProductDetails";
ProductDetails pd = new ProductDetails();
pd.setId(1);pd.setName("Pizza"); pd.setPrice(300);pd.setImage("./Images/Pizza2.png");
List<ProductDetails> lpd = new ArrayList<ProductDetails>();
when(jdbcTemplate.query(sql,BeanPropertyRowMapper.newInstance(ProductDetails.class))).thenReturn(lpd);
assertEquals("200 OK",fetchProductDataController.getAllProducts().getStatusCode().toString());
}
}
package com.altimetrik.poc.controller;
import com.altimetrik.poc.dto.Customer;
import com.altimetrik.poc.repository.UserLoggedInRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@SpringBootTest
public class UserDataControllerTest {
@MockBean
UserLoggedInRepository userLoggedInRepository;
@Autowired
UserDataController userDataController;
@Test
public void removeProducts(){
Customer c = new Customer(233, "Shishir", "Namdeo","shishirnamdeo220@gmaik.com");
userDataController.addUser(c);
verify(userLoggedInRepository,times(1)).save(c);
}
}
package com.altimetrik.poc.services;
import com.altimetrik.poc.dto.Customer;
import com.altimetrik.poc.dto.OrderDetails;
import com.altimetrik.poc.dto.ProductDetails;
import com.altimetrik.poc.repository.ShoppingCartRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
@SpringBootTest
public class ShoppingCartServicesTest {
@MockBean
ShoppingCartRepository shoppingCartRepository;
@Autowired
ShoppingCartServices shoppingCartServices;
@MockBean
List<ProductDetails> lpd;
@Test
public void getProducts(){
when(shoppingCartRepository.findAll()).thenReturn(Stream.of(
new OrderDetails(300,3761,0,0,42,342,new Integer[2],new Customer())).collect(Collectors.toList()));
assertEquals(1,shoppingCartServices.getProducts().size());
}
@Test
public void addProducts(){
OrderDetails pd = new OrderDetails(300,3721,4356,6456,6565,454, new Integer[43], new Customer());
shoppingCartServices.addProducts(pd);
verify(shoppingCartRepository,times(1)).save(pd);
}
}
package com.altimetrik.poc.shoppingCartApp;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ShoppingCartAppApplicationTests {
@Test
void contextLoads() {
}
}
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