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

Product CRUD #3

Merged
merged 3 commits into from
Jul 11, 2022
Merged
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
6 changes: 6 additions & 0 deletions watchbeansAPI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> -->
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.watchbeans.watchbeans.controllers;

import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.watchbeans.watchbeans.repositories.ManagerRepository;
Expand All @@ -15,7 +18,7 @@


@RestController
@RequestMapping("/manager")
@RequestMapping("/gerente")
@CrossOrigin(origins = "*")
public class ManagerController {

Expand All @@ -35,11 +38,18 @@ public String login(@RequestBody Manager manager) {
}
}

@RequestMapping(value = "/findAll", method = RequestMethod.GET)
@RequestMapping(value = "/listar", method = RequestMethod.GET)
public List<Manager> findAll() {
return this.managerRepository.findAll();
}

@RequestMapping(value = "/usuario_logado", method = RequestMethod.GET)
@ResponseBody
public String getCurrentUserId(Principal principal) {
return principal.getName();
}


// @RequestMapping(value = "/login", method = RequestMethod.POST)
// public @ResponseBody Professor login(@RequestBody Professor professor){
// Professor professorLogin = professorRepository.login(professor.getMatricula(), professor.getSenha());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
package com.watchbeans.watchbeans.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import com.watchbeans.watchbeans.models.Product;
import com.watchbeans.watchbeans.repositories.ProductRepository;


import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/produtos")
@RequestMapping("/produto")
public class ProductController {

public ProductController() {
}

@Autowired
private ProductRepository productRepository;

@RequestMapping(value = "/helloWorld", method = RequestMethod.GET)
public String helloWorld(){
return "Estamos no ar mesmo?";
}

@RequestMapping(value = "/criar", method = RequestMethod.POST)
public Product createProduct(@RequestBody Product product) {
return productRepository.save(product);
}

@RequestMapping(value = "/listar", method = RequestMethod.GET)
public List<Product> listProducts() {
return productRepository.findAll();
}

@RequestMapping(value = "/listar/{id}", method = RequestMethod.GET)
public Product findProduct(@PathVariable("id") Long id) {
return productRepository.findProduct(id);
}

@RequestMapping(value = "/atualizar_preco", method = RequestMethod.PUT)
public String updateProduct(@RequestBody Product product) {
productRepository.updatePrice(product.getPrice(), product.getId());
return "Atualizado com sucesso";
}

@RequestMapping(value = "/deletar", method = RequestMethod.DELETE)
public String deleteProduct(@RequestBody Product product) {
productRepository.deleteProduct(product.getId());
return "Deletado com sucesso";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.watchbeans.watchbeans.models;

import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "produto")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cod_produto")
private Long id;

@Column(name = "nome_produto", nullable = false)
private String name;

@Column(name = "preco_produto", nullable = false)
private Float price;

@Column(name = "modelo_produto", nullable = false)
private String model;

@Column(name = "cod_venda", nullable = true)
private Integer sale_id;


public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return this.name;
}

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

public Float getPrice() {
return this.price;
}

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

public String getModel() {
return this.model;
}

public void setModel(String model) {
this.model = model;
}

public Integer getSale_id() {
return this.sale_id;
}

public void setSale_id(Integer sale_id) {
this.sale_id = sale_id;
}

@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Product)) {
return false;
}
Product product = (Product) o;
return Objects.equals(id, product.id) && Objects.equals(name, product.name) && Objects.equals(price, product.price) && Objects.equals(model, product.model) && Objects.equals(sale_id, product.sale_id);
}

@Override
public int hashCode() {
return Objects.hash(id, name, price, model);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.watchbeans.watchbeans.models;

import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "venda")
public class Sale {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cod_venda")
private Long id;

@Column(name = "cod_produto", nullable = false)
private Long product_id;


public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public Long getProduct_id() {
return this.product_id;
}

public void setProduct_id(Long product_id) {
this.product_id = product_id;
}


@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Sale)) {
return false;
}
Sale sale = (Sale) o;
return Objects.equals(id, sale.id) && Objects.equals(product_id, sale.product_id);
}

@Override
public int hashCode() {
return Objects.hash(id, product_id);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ManagerRepository extends JpaRepository<Manager, Long>{
//public Professor login(String matricula, String senha);

//Aluno save(Aluno aluno);
//uma classe que é temporaria para mandar os dados para o banco
//uma classe que é temporaria para mandar os dados para o banco S é a classe temp e a T é a classe que esta no banco
//<S extends T> S save(S name);
// <newAluno extends Aluno> newAluno save(newAluno aluno);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.watchbeans.watchbeans.repositories;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.watchbeans.watchbeans.models.Product;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long>{

<newProduct extends Product> newProduct save(newProduct product);

@Modifying
@Transactional
@Query(value = "UPDATE produto SET preco_produto = :newPrice WHERE cod_produto = :id", nativeQuery = true)
public void updatePrice(@Param("newPrice")Float price, @Param("id")Long id);

@Modifying
@Transactional
@Query(value = "DELETE FROM produto WHERE cod_produto = :id", nativeQuery = true)
public void deleteProduct(@Param("id")Long id);

@Query(value = "SELECT * FROM produto WHERE cod_produto = :id", nativeQuery = true)
public Product findProduct(@Param("id")Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.watchbeans.watchbeans.repositories;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.watchbeans.watchbeans.models.Sale;

public interface SaleRepository extends JpaRepository<Sale, Long>{

//TODO - create the insert and update methods

<newSale extends Sale> newSale save(newSale sale);


//essa lógica talvez nao seja aplicada como "adicionar produto" pois só altera o valor da venda sem ter conjunto com o valor do produto, e nem aplica o código da venda ao produto
//talvez dê de fazer isso pelo front onde ele faz uma requisição da venda para atualizar o preco e no produto para atualizar o cod_venda
//mas o melhor seria fazer aqui.
//escrever a lógica da venda
@Modifying
@Transactional
@Query(value = "UPDATE venda SET valor_venda = :newPrice WHERE cod_venda = :id", nativeQuery = true)
public void updatePrice(@Param("newPrice")Float price, @Param("id")Long id);



}
3 changes: 3 additions & 0 deletions watchbeansAPI/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update

security.basic.enable: false
security.ignored=