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

Bootcamp final project santiagos #64

Open
wants to merge 1 commit into
base: bootcamp_santiagos
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
Binary file added FinalProject/finalproject.mwb
Binary file not shown.
68 changes: 68 additions & 0 deletions FinalProject/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.javabootcamp.finalproject.data.jpa.main.FinalProjectBootcamp</start-class>
<java.version>1.8</java.version>
</properties>

<artifactId>FinalProject</artifactId>
<groupId>com.javabootcamp</groupId>
<name>Java bootcamp final project</name>
<description>Spring Boot Data JPA Sample</description>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

package com.javabootcamp.finalproject.data.jpa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Class with main method, starts the application
* @author Santiago
*/
@SpringBootApplication
public class FinalProjectBootcamp {

public static void main(String[] args) throws Exception {
SpringApplication.run(FinalProjectBootcamp.class, args);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

package com.javabootcamp.finalproject.data.jpa.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import org.hibernate.annotations.NaturalId;

@Entity
public class Itemtobuy implements Serializable {

@Id
@GeneratedValue
private Long iditem;

@NaturalId
@PrimaryKeyJoinColumn(referencedColumnName = "idshoppingcart")
private Long idcart;

@NaturalId
@PrimaryKeyJoinColumn(referencedColumnName = "idproduct")
private Long idprod;

@Column(nullable = false)
private int quantity;

protected Itemtobuy(){}

public Itemtobuy (Long idcart, Long idprod, int quantity){
this.idcart = idcart;
this.idprod = idprod;
this.quantity = quantity;
}

public Long getIdcart() {
return idcart;
}

public void setIdcart(Long ids) {
this.idcart = ids;
}

public Long getIdprod() {
return idprod;
}

public void setIdprod(Long idprod) {
this.idprod = idprod;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

package com.javabootcamp.finalproject.data.jpa.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Product implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name="idproduct")
private Long idproduct;

@Column(nullable=false)
private String name;

@Column(nullable=false)
private String category;

@Column(nullable=false)
private double price;

protected Product(){}

public Product(String name, String category){
this.name = name;
this.category = category;
}

public Long getId() {
return idproduct;
}

public String getName() {
return name;
}

public String getCategory() {
return category;
}

@Override
public String toString() {
return "Product{" + "id=" + idproduct + ", name=" + name + ", category=" + category + '}';
}

public void setName(String name) {
this.name = name;
}

public void setCategory(String category) {
this.category = category;
}

public void setPrice(double p){
this.price = p;
}

public double getPrice(){
return price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

package com.javabootcamp.finalproject.data.jpa.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;


@Entity
public class Shoppingcart implements Serializable {

@Id
@PrimaryKeyJoinColumn(referencedColumnName = "iduser")
private Long idshoppingcart;

@Column(nullable = false)
private double total;

protected Shoppingcart(){}

public Shoppingcart(Long id, double total){
this.idshoppingcart = id;
this.total = total;
}

public Long getUserid() {
return idshoppingcart;
}

public void setUserid(Long userid) {
this.idshoppingcart = userid;
}


public double getTotal() {
return total;
}

public void setTotal(double num) {
this.total = num;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

package com.javabootcamp.finalproject.data.jpa.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column( name = "iduser")
private Long iduser;

@Column(nullable = false)
private String username;

@Column(nullable = false)
private String password;

protected User(){}

public User(String username, String password){
this.username = username;
this.password = password;
}

@Override
public String toString() {
return "User{" + ", username=" + username + ", password=" + password + ", administrator=" +'}';
}

public Long getId() {
return iduser;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package com.javabootcamp.finalproject.data.jpa.service;

import com.javabootcamp.finalproject.data.jpa.domain.Itemtobuy;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface ItemToBuyRepository extends CrudRepository<Itemtobuy, Long> {

@Query("Select p.name, i.quantity from Product p, Itemtobuy i where i.idprod = ?1 and i.idprod = p.idproduct" )
public String findAllProductsById(Long id);

@Query("from Itemtobuy i where idcart = ?1 and idprod = ?2 ")
public Itemtobuy findByShopId(Long id, Long idprod);

@Query("Select i.idprod from Itemtobuy i where i.idcart = ?1")
public List<Long> getIdItemsInCart(Long id);

@Modifying
@Query("Delete from Itemtobuy i where i.idcart = ?1")
public void deleteAllById(Long id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

package com.javabootcamp.finalproject.data.jpa.service;

import com.javabootcamp.finalproject.data.jpa.domain.Product;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, Long>{

Product findByIdproduct(Long productid);

List<Product> findAll();

@Query("Select idproduct from Product p where p.name = ?1")
Long getIdproductByName(String name);

@Query("Select price from Product p where p.idproduct = ?1")
public double getPriceForId(Long idprod);

List<Product> findAllByCategory(String category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

package com.javabootcamp.finalproject.data.jpa.service;

import com.javabootcamp.finalproject.data.jpa.domain.Product;
import java.util.List;

public interface ProductService {
/**
* Returns product with the given id
* @param productid
* @return
*/
Product findByIdproduct(Long productid);

/**
* Returns list with all products
* @return
*/
List<Product> findAll();

/**
* Returns list with the products of the given category
* @param category
* @return
*/
List<Product> findAllByCategory(String category);
}
Loading