Skip to content

Commit

Permalink
Merge pull request #4 from mairess/creates-customer-routes
Browse files Browse the repository at this point in the history
Creates customer routes
  • Loading branch information
mairess authored Jun 29, 2024
2 parents 19f7ec5 + f7183c5 commit 293d1e7
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 9 deletions.
68 changes: 67 additions & 1 deletion src/main/java/com/maires/wnet/controller/CustomerController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package com.maires.wnet.controller;

import com.maires.wnet.controller.dto.CustomerCreationDto;
import com.maires.wnet.controller.dto.CustomerDto;
import com.maires.wnet.entity.Customer;
import com.maires.wnet.service.CustomerService;
import com.maires.wnet.service.exception.CustomerNotFoundException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
* The type Customer controller.
*/
@RestController
@RequestMapping("customers")
@RequestMapping("/customers")
public class CustomerController {

private final CustomerService customerService;
Expand All @@ -24,4 +36,58 @@ public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

/**
* Find all customers list.
*
* @return the list
*/
@GetMapping
public List<CustomerDto> findAllCustomers() {
return customerService.findAllCustomers().stream().map(CustomerDto::fromEntity).toList();
}

/**
* Find customer by id customer dto.
*
* @param customerId the customer id
* @return the customer dto
* @throws CustomerNotFoundException the customer not found exception
*/
@GetMapping("/{customerId}")
public CustomerDto findCustomerById(@PathVariable Long customerId)
throws CustomerNotFoundException {
return CustomerDto.fromEntity(
customerService.findCustomerById(customerId)
);
}


/**
* Create customer customer dto.
*
* @param customerCreationDto the customer creation dto
* @return the customer dto
*/
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CustomerDto createCustomer(@RequestBody CustomerCreationDto customerCreationDto) {
Customer newCustomer = customerService.createCustomer(customerCreationDto.toEntity());
return CustomerDto.fromEntity(newCustomer);
}


/**
* Remove customer by id customer dto.
*
* @param customerId the customer id
* @return the customer dto
* @throws CustomerNotFoundException the customer not found exception
*/
@DeleteMapping("/{customerId}")
public CustomerDto removeCustomerById(@PathVariable Long customerId)
throws CustomerNotFoundException {
return CustomerDto.fromEntity(
customerService.removeCustomerById(customerId)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.maires.wnet.controller.advice;

import com.maires.wnet.service.exceptions.NotFoundException;
import com.maires.wnet.service.exception.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.maires.wnet.controller.dto;

import com.maires.wnet.entity.Customer;

/**
* The addressType Customer creation dto.
*/
public record CustomerCreationDto(String name, String cpf, String phone, String email) {

/**
* To entity customer.
*
* @return the customer
*/
public Customer toEntity() {
return new Customer(name, cpf, phone, email);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/maires/wnet/controller/dto/CustomerDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.maires.wnet.controller.dto;

import com.maires.wnet.entity.Customer;

/**
* The addressType Customer dto.
*/
public record CustomerDto(Long id, String name, String cpf, String phone, String email,
String registrationDate) {

/**
* From entity customer dto.
*
* @param customer the customer
* @return the customer dto
*/
public static CustomerDto fromEntity(Customer customer) {
return new CustomerDto(
customer.getId(),
customer.getName(),
customer.getCpf(),
customer.getPhone(),
customer.getEmail(),
customer.getRegistrationDate()
);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/maires/wnet/service/CustomerService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.maires.wnet.service;

import com.maires.wnet.entity.Customer;
import com.maires.wnet.repository.CustomerRepository;
import com.maires.wnet.service.exception.CustomerNotFoundException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -23,4 +26,48 @@ public CustomerService(CustomerRepository customerRepository) {
}


/**
* Find all customers list.
*
* @return the list
*/
public List<Customer> findAllCustomers() {
return customerRepository.findAll();
}

/**
* Find customer by id customer.
*
* @param customerId the customer id
* @return the customer
* @throws CustomerNotFoundException the customer not found exception
*/
public Customer findCustomerById(Long customerId) throws CustomerNotFoundException {
return customerRepository.findById(customerId)
.orElseThrow(CustomerNotFoundException::new);
}

/**
* Create customer customer.
*
* @param customer the customer
* @return the customer
*/
public Customer createCustomer(Customer customer) {
return customerRepository.save(customer);
}

/**
* Remove customer by id customer.
*
* @param customerId the customer id
* @return the customer
* @throws CustomerNotFoundException the customer not found exception
*/
public Customer removeCustomerById(Long customerId) throws CustomerNotFoundException {
Customer customer = findCustomerById(customerId);
customerRepository.delete(customer);
return customer;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;

/**
* The type Address not found exception.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;

/**
* The type Customer not found exception.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;

/**
* The type Not found exception.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.maires.wnet.service.exceptions;
package com.maires.wnet.service.exception;


/**
Expand Down

0 comments on commit 293d1e7

Please sign in to comment.