Skip to content
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

Final project #65

Open
wants to merge 5 commits into
base: bootcamp_roberta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions finalProject/shopcart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
13 changes: 13 additions & 0 deletions finalProject/shopcart/derby.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
----------------------------------------------------------------
Sat Feb 07 20:22:39 ART 2015:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.11.1.1 - (1616546): instance a816c00e-014b-665a-2808-0000135e2de0
on database directory memory:/home/rodrigo/NetBeansProjects/shopcart/testdb with class loader java.net.URLClassLoader@57ac9313
Loaded from file:/home/rodrigo/.m2/repository/org/apache/derby/derby/10.11.1.1/derby-10.11.1.1.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_31-b13
user.dir=/home/rodrigo/NetBeansProjects/shopcart
os.name=Linux
os.arch=amd64
os.version=3.18.3-201.fc21.x86_64
derby.system.home=null
Database Class Loader started - derby.database.classpath=''
18 changes: 18 additions & 0 deletions finalProject/shopcart/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.hint.jdkPlatform>JDK 1.7</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
87 changes: 87 additions & 0 deletions finalProject/shopcart/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>javabootcamp</groupId>
<artifactId>shopcart</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>shopcart</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
<properties>
<start-class>javabootcamp.shopcart.Application</start-class>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>

</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package javabootcamp.shopcart;

import java.math.BigDecimal;
import java.util.List;
import javabootcamp.shopcart.model.Category;
import javabootcamp.shopcart.model.PaymentType;
import javabootcamp.shopcart.model.Product;
import javabootcamp.shopcart.model.ShopCart;
import javabootcamp.shopcart.model.ShopCartItem;
import javabootcamp.shopcart.repository.CategoryRepository;
import javabootcamp.shopcart.repository.ItemRepository;
import javabootcamp.shopcart.repository.ProductRepository;
import javabootcamp.shopcart.repository.ShopCartRepository;
import javabootcamp.shopcart.repository.UserRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
*
* @author roberta
*/
@SpringBootApplication
@RestController(value = "shop")
public class Application {

private ShopCart cart;

private static ConfigurableApplicationContext context;
private static ProductRepository productRepository;
private static ShopCartRepository cartRepository;
private static ItemRepository itemRepository;

private static CategoryRepository categoryRepository;

@RequestMapping("/ping")
public String getGreeting() {
return "Hello World!";
}

@RequestMapping("/createCart")
public String createCart() {
if (cart == null) {
cart = new ShopCart();
return "Cart created succesfully!";
} else {
return "Cart already created!";
}
}

@RequestMapping("/addProduct")
public String addProduct(@RequestParam(value = "idProd", required = true) Long productId,
@RequestParam(value = "quantity", required = false, defaultValue = "1") Integer quantity) {

createCart();
Product product = productRepository.findOne(productId);
if (product == null) {
return "The product does not exists!";
}
ShopCartItem item = new ShopCartItem(quantity, product, product.getPrice().multiply(new BigDecimal(quantity)), cart);
if (cart.getCartItems() == null) {
cart.setCartItems(new java.util.ArrayList<ShopCartItem>());
}
cart.getCartItems().add(item);
return "Product: " + product.getName() + " added succesfully!";
}

@RequestMapping("/removeProduct")
public String removeProduct(@RequestParam(value = "idProd", required = true) Long productId) {

int index = -1;

for (int i = 0; i < cart.getCartItems().size(); i++) {
if (cart.getCartItems().get(i).getProduct().getId().equals(productId)) {
index = i;
}

}
if (index > -1) {
cart.getCartItems().remove(index);
}
return "Item deleted succesfully!";

}

@RequestMapping("/listItems")
public String listCartItems() {
if (cart != null) {
StringBuilder sb = new StringBuilder();
sb.append("Product\t\t\tQuantity\t\t\tSubTotal\n");
for (ShopCartItem item : cart.getCartItems()) {
sb.append(item.getProduct().getName())
.append("\t\t\t").append(item.getQuantity())
.append("\t\t\t")
.append(item.getSubTotal().toPlainString())
.append("\n");
}
return sb.toString();
}
return null;
}

@RequestMapping("/confirm")
public String confirm(@RequestParam(value = "paymentType", required = false, defaultValue = "CASH") String paymentType) {
PaymentType payment = PaymentType.valueOf(paymentType);
if (cart != null) {
if (cart.getId() != null) {
return "You have yust checked out!";
}
cart.setPaymentType(payment);
cartRepository.save(cart);
itemRepository.save(cart.getCartItems());

return "Your invoce number: " + cart.getId() + " was generated for a total of: $" + cart.getTotal().toPlainString();

}
return "You haven't a shop cart yet...";
}

@RequestMapping(value = "/findProductsByCategory", produces = "application/json")
public List<Product> findByCategory(@RequestParam(value = "categoryId", required = true) Long category) {
Category findOne = categoryRepository.findOne(category);
return productRepository.findByCategory(findOne);
}

@RequestMapping(value = "/findProductsByName", produces = "application/json")
public List<Product> findByName(@RequestParam(value = "name") String name) {
return productRepository.findByName(name);

}

public static void main(String[] args) {
//SpringApplication.run(Application.class, args);

context = SpringApplication.run(Application.class);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Al ser una rest api no deberiamos tener un metodo main aqui. Todo debe ser invocado desde afuera de la aplicacion y solo a modo dde pruebas no es necesario commitear eso

productRepository = context.getBean(ProductRepository.class);
cartRepository = context.getBean(ShopCartRepository.class);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fijate como lo hizo santi #64

itemRepository = context.getBean(ItemRepository.class);
categoryRepository = context.getBean(CategoryRepository.class);
UserRepository userRepo = context.getBean(UserRepository.class);

Category electro = new Category();
electro.setName("Electronic");
Category shoes = new Category();
shoes.setName("Shoes");
categoryRepository.save(electro);
categoryRepository.save(shoes);

// userRepo.save(new ShopUser("admin", "admin"));
// userRepo.save(new ShopUser("user", "user"));
productRepository.save(new Product("Fridge", "A beautifull fridge", BigDecimal.TEN,electro));
productRepository.save(new Product("Nike Air", "A beautifull shoes", BigDecimal.TEN,shoes));

Iterable<Product> products = productRepository.findAll();
System.out.println("Products found with findAll():");
System.out.println("-------------------------------");
for (Product product : products) {
System.out.println(product);
}
System.out.println();

Product product = productRepository.findOne(1L);
System.out.println("Product found with findOne(1L):");
System.out.println("--------------------------------");
System.out.println(product);
System.out.println();

List<Product> finds = productRepository.findByName("Notebook");
System.out.println("Product found with findByName('Notebook'):");
System.out.println("--------------------------------------------");
for (Product prod : finds) {
System.out.println(prod);
}

//context.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javabootcamp.shopcart;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javabootcamp.shopcart;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.Properties;

/**
*
* @author roberta
*/
@Configuration
public class PersistenceContext {

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/shopCart");
dataSource.setUsername("root");
dataSource.setPassword("famas");
return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);

LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("javabootcamp.shopcart.model");
factory.setDataSource(dataSource());
Properties prop = new Properties();
prop.setProperty("spring.jpa.hibernate.ddl-auto", "create-drop");
factory.setJpaProperties(prop);
factory.afterPropertiesSet();

return factory;
}

@Bean
public PlatformTransactionManager transactionManager() {

JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory().getNativeEntityManagerFactory());
return txManager;
}
}
Loading