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 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
1 change: 1 addition & 0 deletions finalProject/shopcart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
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,17 @@
package javabootcamp.shopcart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
*
* Class with main method, starts the application
*
* @author roberta
*/

@SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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 org.springframework.beans.factory.annotation.Autowired;
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
*/


@RestController(value = "shop")
public class CartController {
private ShopCart cart;
private ConfigurableApplicationContext context;
@Autowired
private ProductRepository productRepository;
@Autowired
private ShopCartRepository cartRepository;
@Autowired
private ItemRepository itemRepository;
@Autowired
private 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 boolean 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 false;
}
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 true;
}

@RequestMapping("/removeProduct")
public boolean 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);
Copy link
Owner

Choose a reason for hiding this comment

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

que pasa si sucede una exception aqui?

return true;
}
return false;

}

@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);

}
}
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