-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/mayvlima/japaoPadaria
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/main/java/com/techbank/japaoPadaria/controller/ClienteController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
src/main/java/com/techbank/japaoPadaria/model/Cliente.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' + | ||
'}'; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/techbank/japaoPadaria/repository/ClienteRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters