Skip to content

Commit

Permalink
refactor: correcting bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Edith Soares committed May 25, 2021
1 parent 30e45df commit d5e417e
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 236 deletions.
59 changes: 53 additions & 6 deletions src/main/java/spg/finalchallenge/controller/ClientController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,37 @@ public class ClientController {
@ApiResponse(code = 500, message = "An exception was thrown", response = Response.class),
})
@PostMapping("/client")
public ResponseEntity<?> saveClient(@RequestBody Client client){
try{
return new ResponseEntity<>(clientService.saveClient(client), HttpStatus.CREATED);
public ResponseEntity<?> createClient(@RequestBody Client client){
try {
Client clientResultCpf = clientService.findByCpf(client.getCpf());
if (clientResultCpf == null){
return new ResponseEntity<>(clientService.saveClient(client), HttpStatus.CREATED);
}
return new ResponseEntity<>(new Response(false, "Cpf já existe!"), HttpStatus.CONFLICT);
}catch (Exception e){
return new ResponseEntity<>( new Response(false, "Request Errors"),
return new ResponseEntity<>(new Response(false,"Request Errors"),
HttpStatus.BAD_REQUEST);
}
}

@ApiOperation(value = "Search client by Id")
@ApiResponses(value ={
@ApiResponse(code = 200, message = "Return Client", response = Response.class),
@ApiResponse(code = 401, message = "You do not have permission to access this feature.", response = Response.class),
@ApiResponse(code = 404, message = "List products not found", response = Response.class),
@ApiResponse(code = 500, message = "An exception was thrown", response = Response.class),
})
@GetMapping("/client/{id}")
public ResponseEntity<?> getClientById(@PathVariable Long id){
try {
Optional<Client> clientResultId = clientService.findById(id);
if (clientResultId.isPresent()){
return new ResponseEntity<>(clientResultId, HttpStatus.OK);
}
return new ResponseEntity<>(new Response(false, "Client not foud"),
HttpStatus.NOT_FOUND);
}catch (Exception e){
return new ResponseEntity<>(new Response(false, "Request Errors"),
HttpStatus.BAD_REQUEST);
}
}
Expand Down Expand Up @@ -72,7 +98,7 @@ public ResponseEntity<?> listAllClients(){
@PutMapping("/client/update/{id}")
public ResponseEntity<?> updateClient(@PathVariable long id, @RequestBody Client client){
try {
Optional<Client> clientResult = clientService.findByIdClient(id);
Optional<Client> clientResult = clientService.findById(id);
if (clientResult.isPresent()){
client.setId(clientResult.get().getId());
return new ResponseEntity<>(clientService.update(client), HttpStatus.OK);
Expand All @@ -84,7 +110,28 @@ public ResponseEntity<?> updateClient(@PathVariable long id, @RequestBody Client
return new ResponseEntity<>( new Response(false, "Request Errors"),
HttpStatus.BAD_REQUEST);
}
}

@ApiOperation(value = "Delete client by Id")
@ApiResponses(value ={
@ApiResponse(code = 200, message = "Returns the cpf deleted", response = Response.class),
@ApiResponse(code = 401, message = "You do not have permission to access this feature.", response = Response.class),
@ApiResponse(code = 404, message = "List products not found", response = Response.class),
@ApiResponse(code = 500, message = "An exception was thrown", response = Response.class),
})
@DeleteMapping("client/id/{id}")
public ResponseEntity<?> deleteById(@PathVariable long id){
try {
Client clientResultId = clientService.getId(id);
if (clientResultId != null){
clientService.deleteClient(clientResultId);
return new ResponseEntity<>(new Response(true, "Client deleted id: " + id ),
HttpStatus.OK);
}
return new ResponseEntity<>(new Response(false, "Id not found " + clientResultId.getId()), HttpStatus.NOT_FOUND);
}catch (Exception e){
return new ResponseEntity<>(new Response(false, "Request erros"), HttpStatus.BAD_REQUEST);
}
}

@ApiOperation(value = "Delete client by Cpf")
Expand All @@ -97,7 +144,7 @@ public ResponseEntity<?> updateClient(@PathVariable long id, @RequestBody Client
@DeleteMapping("client/delete/{cpf}")
public ResponseEntity<?> deleteClientByCpf(@PathVariable String cpf){
try {
Client client = clientService.findByCpfClient(cpf);
Client client = clientService.findByCpf(cpf);
if (client != null){
clientService.deleteClient(client);
return new ResponseEntity<>(new Response(true, "Cliente deleted cpd: " + cpf ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import org.springframework.stereotype.Repository;
import spg.finalchallenge.entity.Client;

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

@Repository
public interface ClientRepository extends JpaRepository<Client, Long> {

Expand Down
20 changes: 12 additions & 8 deletions src/main/java/spg/finalchallenge/service/ClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,41 @@ public class ClientService {
@Autowired
private ClientRepository clientRepository;

// Cadastrar clients
public Client saveClient(Client client){
return clientRepository.save(client);
}

// Buscar todos os Clients
public List<Client> listAllClient(){
return clientRepository.findAll();
}

public Client findByCpfClient(String cpf){
public Client findByCpf(String cpf) {
return clientRepository.findByCpf(cpf);
}

public void deleteClient(Client client) {
clientRepository.delete(client);
}

public Client getId(long id) {
return clientRepository.findById(id);
}

public Optional<Client> findByIdClient(Long id) {
public Optional<Client> findById(Long id) {
return clientRepository.findById(id);
}

public Client update(Client client) {
return clientRepository.save(client);
}

public void deleteClient(Client client) {
clientRepository.delete(client);
}

public long count() {
return clientRepository.count();
}



// public void delete(long id){
// clientRepository.deleteById(id);
// }
}
28 changes: 14 additions & 14 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
### --------- Configuração banco MySql ---------
#
##
#spring.datasource.url=mysql://b66ed712bc811c:[email protected]/heroku_26b0e4901d71f24?reconnect=true
#spring.datasource.username=b66ed712bc811c
#spring.datasource.password=070eab02
#spring.jpa.hibernate.ddl-auto=update
#
### Hibernate Properties
## The SQL dialect makes Hibernate generate better SQL for the chosen database

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

#spring.h2.console.enabled=true
#spring.h2.console.path=/h2
#spring.datasource.url=jdbc:h2:mem:teste
#spring.datasource.driver-class-name=org.h2.Driver
#spring.datasource.username=sa
#spring.datasource.password=password
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#server.port=8080
#spring.jpa.hibernate.ddl-auto=create
#spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:teste
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
server.port=8080
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
134 changes: 67 additions & 67 deletions test/java/spg/finalchallenge/service/ClientServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
package spg.finalchallenge.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import spg.finalchallenge.entity.Client;



import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ClientServiceTest {

@Autowired
ClientService clientServiceTest;

private Client instaceClient(String name, String cpf){
Client client = new Client();
client.setName(name);
client.setCpf(cpf);
return clientServiceTest.saveClient(client);
}


@Test
public void saveClient(){
Client client = instaceClient("Edith", "123");

assertThat(client).isNotNull();
}


@Test
public void checkIdClientSave(){
Client client = instaceClient("Edith", "123");

//Client clientSave = clientServiceTest.saveClient(client);

assertThat(client.getId()).isEqualTo(1);
}

@Test
public void getClientById(){
Client client = instaceClient("Edith", "123");

//Client clientSave = clientServiceTest.saveClient(client);

Client found = clientServiceTest.getId(client.getId());

assertThat(found.getName()).isEqualTo(client.getName());
}

@Test
public void countClient(){
Client client = instaceClient("Edith", "123");//

// Client clientSvae = clientServiceTest.saveClient(client);

long qtd = clientServiceTest.count();

assertThat(qtd).isEqualTo(1);
}
}
//package spg.finalchallenge.service;
//
//import org.junit.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//import spg.finalchallenge.entity.Client;
//
//
//
//import static org.assertj.core.api.Assertions.assertThat;
//
//@RunWith(SpringRunner.class)
//@SpringBootTest
//public class ClientServiceTest {
//
// @Autowired
// ClientService clientServiceTest;
//
// private Client instaceClient(String name, String cpf){
// Client client = new Client();
// client.setName(name);
// client.setCpf(cpf);
// return clientServiceTest.saveClient(client);
// }
//
//
// @Test
// public void saveClient(){
// Client client = instaceClient("Edith", "123");
//
// assertThat(client).isNotNull();
// }
//
//
// @Test
// public void checkIdClientSave(){
// Client client = instaceClient("Edith", "123");
//
// //Client clientSave = clientServiceTest.saveClient(client);
//
// assertThat(client.getId()).isEqualTo(1);
// }
//
// @Test
// public void getClientById(){
// Client client = instaceClient("Edith", "123");
//
// //Client clientSave = clientServiceTest.saveClient(client);
//
// Client found = clientServiceTest.getId(client.getId());
//
// assertThat(found.getName()).isEqualTo(client.getName());
// }
//
// @Test
// public void countClient(){
// Client client = instaceClient("Edith", "123");//
//
//// Client clientSvae = clientServiceTest.saveClient(client);
//
// long qtd = clientServiceTest.count();
//
// assertThat(qtd).isEqualTo(1);
// }
//}
Loading

0 comments on commit d5e417e

Please sign in to comment.