Skip to content

Rest controller #16

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

Open
wants to merge 2 commits into
base: rest-controller
Choose a base branch
from
Open
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
21 changes: 12 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.CR2</org.mapstruct.version>
<java.version>11</java.version>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.24</org.projectlombok.version>
</properties>

<dependencies>
Expand All @@ -47,13 +48,14 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -87,18 +89,19 @@
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package guru.springfamework.api.v1.mapper;

import guru.springfamework.api.v1.model.CustomerDTO;
import guru.springfamework.controllers.v1.CustomerController;
import guru.springfamework.domain.Customer;

import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

/**
Expand All @@ -13,7 +18,15 @@ public interface CustomerMapper {

CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);

@Mapping(source = "id", target = "customerUrl")
CustomerDTO customerToCustomerDTO(Customer customer);

@AfterMapping
default void addBaseURL(@MappingTarget CustomerDTO customerDTO)
{
customerDTO.setCustomerUrl(CustomerController.BASE_URL + "/" + customerDTO.getCustomerUrl());
}

@Mapping(target = "id", ignore = true)
Customer customerDtoToCustomer(CustomerDTO customerDTO);
}
30 changes: 30 additions & 0 deletions src/main/java/guru/springfamework/api/v1/mapper/VendorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package guru.springfamework.api.v1.mapper;

import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import guru.springfamework.api.v1.model.VendorDTO;
import guru.springfamework.controllers.v1.VendorController;
import guru.springfamework.domain.Vendor;

@Mapper
public interface VendorMapper
{
VendorMapper INSTANCE = Mappers.getMapper(VendorMapper.class);

@Mapping(source = "id", target = "vendorUrl")
VendorDTO vendorToVendorDTO(Vendor vendor);

@AfterMapping
default void addBaseURL(@MappingTarget VendorDTO vendorDTO)
{
vendorDTO.setVendorUrl(VendorController.BASE_URL + "/" + vendorDTO.getVendorUrl());
}

@Mapping(target = "id", ignore = true)
Vendor vendorDtoToVendor(VendorDTO vendorDTO);

}
18 changes: 18 additions & 0 deletions src/main/java/guru/springfamework/api/v1/model/VendorDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package guru.springfamework.api.v1.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class VendorDTO
{
private String name;

@JsonProperty("vendor_url")
private String vendorUrl;
}
15 changes: 15 additions & 0 deletions src/main/java/guru/springfamework/api/v1/model/VendorListDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guru.springfamework.api.v1.model;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class VendorListDTO
{
private List<VendorDTO> vendors;
}
28 changes: 27 additions & 1 deletion src/main/java/guru/springfamework/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import guru.springfamework.domain.Category;
import guru.springfamework.domain.Customer;
import guru.springfamework.domain.Vendor;
import guru.springfamework.repositories.CategoryRepository;
import guru.springfamework.repositories.CustomerRepository;
import guru.springfamework.repositories.VendorRepository;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

Expand All @@ -15,17 +18,20 @@ public class Bootstrap implements CommandLineRunner{

private final CategoryRepository categoryRespository;
private final CustomerRepository customerRepository;
private final VendorRepository vendorRepository;

public Bootstrap(CategoryRepository categoryRespository, CustomerRepository customerRepository) {
public Bootstrap(CategoryRepository categoryRespository, CustomerRepository customerRepository, VendorRepository vendorRepository) {
this.categoryRespository = categoryRespository;
this.customerRepository = customerRepository;
this.vendorRepository = vendorRepository;
}

@Override
public void run(String... args) throws Exception {

loadCategories();
loadCustomers();
loadVendors();
}

private void loadCategories() {
Expand Down Expand Up @@ -70,4 +76,24 @@ private void loadCustomers() {

System.out.println("Customers Loaded: " + customerRepository.count());
}

private void loadVendors()
{
Vendor vendor = new Vendor();
vendor.setId(1L);
vendor.setName("vendor 1");
vendorRepository.save(vendor);

vendor = new Vendor();
vendor.setId(2L);
vendor.setName("vendor 2");
vendorRepository.save(vendor);

vendor = new Vendor();
vendor.setId(3L);
vendor.setName("vendor 3");
vendorRepository.save(vendor);

System.out.println("Vendors Loaded: " + vendorRepository.count());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package guru.springfamework.controllers.v1;

import java.util.List;

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.PatchMapping;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import guru.springfamework.api.v1.model.VendorDTO;
import guru.springfamework.services.VendorService;

@RestController()
@RequestMapping(VendorController.BASE_URL)
public class VendorController
{
public static final String BASE_URL = "/api/v1/vendors";

public VendorController(VendorService vendorService)
{
this.vendorService = vendorService;
}

private final VendorService vendorService;

@GetMapping
@ResponseStatus(HttpStatus.OK)
List<VendorDTO> getVendors()
{
return vendorService.getAllVendors();
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
VendorDTO createVendor(@RequestBody VendorDTO vendorDTO)
{
VendorDTO createdVendor = vendorService.createNewVendor(vendorDTO);

return createdVendor;
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
void deleteVendor(@PathVariable Long id)
{
vendorService.deleteVendorById(id);
}

@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
VendorDTO getVendor(@PathVariable Long id)
{
return vendorService.getVendorById(id);
}


@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
VendorDTO updateVendor(@PathVariable Long id, @RequestBody VendorDTO vendorDTO)
{
return vendorService.saveVendorByDTO(id, vendorDTO);
}

@PatchMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
VendorDTO patchVendor(@PathVariable Long id, @RequestBody VendorDTO vendorDTO)
{
return vendorService.patchVendor(id, vendorDTO);
}

}
19 changes: 19 additions & 0 deletions src/main/java/guru/springfamework/domain/Vendor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package guru.springfamework.domain;

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

import lombok.Data;

@Entity
@Data
public class Vendor
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springfamework.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import guru.springfamework.domain.Vendor;

public interface VendorRepository extends JpaRepository<Vendor, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import guru.springfamework.api.v1.mapper.CustomerMapper;
import guru.springfamework.api.v1.model.CustomerDTO;
import guru.springfamework.controllers.v1.CustomerController;
import guru.springfamework.domain.Customer;
import guru.springfamework.repositories.CustomerRepository;
import org.springframework.stereotype.Service;
Expand All @@ -29,11 +28,7 @@ public List<CustomerDTO> getAllCustomers() {
return customerRepository
.findAll()
.stream()
.map(customer -> {
CustomerDTO customerDTO = customerMapper.customerToCustomerDTO(customer);
customerDTO.setCustomerUrl(getCustomerUrl(customer.getId()));
return customerDTO;
})
.map(customerMapper::customerToCustomerDTO)
.collect(Collectors.toList());
}

Expand All @@ -42,11 +37,6 @@ public CustomerDTO getCustomerById(Long id) {

return customerRepository.findById(id)
.map(customerMapper::customerToCustomerDTO)
.map(customerDTO -> {
//set API URL
customerDTO.setCustomerUrl(getCustomerUrl(id));
return customerDTO;
})
.orElseThrow(ResourceNotFoundException::new);
}

Expand All @@ -57,13 +47,8 @@ public CustomerDTO createNewCustomer(CustomerDTO customerDTO) {
}

private CustomerDTO saveAndReturnDTO(Customer customer) {
Customer savedCustomer = customerRepository.save(customer);

CustomerDTO returnDto = customerMapper.customerToCustomerDTO(savedCustomer);

returnDto.setCustomerUrl(getCustomerUrl(savedCustomer.getId()));

return returnDto;

return customerMapper.customerToCustomerDTO(customerRepository.save(customer));
}

@Override
Expand All @@ -86,19 +71,11 @@ public CustomerDTO patchCustomer(Long id, CustomerDTO customerDTO) {
customer.setLastname(customerDTO.getLastname());
}

CustomerDTO returnDto = customerMapper.customerToCustomerDTO(customerRepository.save(customer));

returnDto.setCustomerUrl(getCustomerUrl(id));

return returnDto;
return customerMapper.customerToCustomerDTO(customerRepository.save(customer));

}).orElseThrow(ResourceNotFoundException::new);
}

private String getCustomerUrl(Long id) {
return CustomerController.BASE_URL + "/" + id;
}

@Override
public void deleteCustomerById(Long id) {
customerRepository.deleteById(id);
Expand Down
Loading