Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Siglyane committed Jul 29, 2021
2 parents d7a98fd + 0157dde commit 8d9d93c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.techbank.japaoPadaria.controller;

import com.techbank.japaoPadaria.model.Cliente;
import com.techbank.japaoPadaria.repository.ClienteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/cliente")

public class ClienteController {

@Autowired
ClienteRepository clienteRepository;

@GetMapping("/listarTodos")
public ResponseEntity<List<Cliente>> getAllCliente() {
try {

List<Cliente> cliente = new ArrayList<Cliente>(clienteRepository.findAll());

if (cliente.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

return new ResponseEntity<>(cliente, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@GetMapping("buscar/{id}")
public ResponseEntity<Cliente> getClienteById(@PathVariable("id") long id) {
Optional<Cliente> cliente = clienteRepository.findById(id);

if (cliente.isPresent()) {
return new ResponseEntity<>(cliente.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}

@PostMapping("/criar")
public ResponseEntity<Cliente> createCliente(@RequestBody Cliente cliente) {
try {
Cliente novoCliente = clienteRepository
.save(cliente);
return new ResponseEntity<>(novoCliente, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@PutMapping("/atualizar/{id}")
public ResponseEntity<Cliente> updateCliente(@PathVariable("id") long id, @RequestBody Cliente cliente) {
Optional<Cliente> clienteDesejado = clienteRepository.findById(id);

if (clienteDesejado.isPresent()) {
Cliente atualizacao = clienteDesejado.get();
atualizacao.setNome(cliente.getNome());
atualizacao.setCpf(cliente.getCpf());

return new ResponseEntity<>(clienteRepository.save(atualizacao), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
59 changes: 59 additions & 0 deletions src/main/java/com/techbank/japaoPadaria/model/Cliente.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.techbank.japaoPadaria.model;

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

import java.math.BigDecimal;

@Entity
@Table(name = "Cliente")
public class Cliente {


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;

@Column
private String Nome;

@Column
private BigDecimal cpf;

public Long getId() {
return Id;
}

public void setId(Long id) {
Id = id;
}

public String getNome() {
return Nome;
}

public void setNome(String nome) {
Nome = nome;
}

public BigDecimal getCpf() {
return cpf;
}

public void setCpf(BigDecimal cpf) {
this.cpf = cpf;
}

@Override
public String toString() {
return "Cliente{" +
"id=" + Id +
", nome='" + Nome + '\'' +
", cnpj='" + cpf + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.techbank.japaoPadaria.repository;

import com.techbank.japaoPadaria.model.Cliente;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ClienteRepository extends JpaRepository<Cliente, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface CompraRepository extends JpaRepository<Compra, Long> {

}

0 comments on commit 8d9d93c

Please sign in to comment.