Skip to content
Open

Xml #15

Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
finish Vendor put patch delete get post and Swagger annotation
  • Loading branch information
lxian-coder committed Nov 25, 2020
commit ab8aa1e969e8ebb2a75848507484b8a16f0633bf
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<springfox-swagger-version>2.7.0</springfox-swagger-version>
</properties>

<dependencies>
Expand Down Expand Up @@ -58,7 +59,17 @@
<artifactId>mapstruct</artifactId>
<version>1.4.1.Final</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger-version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@SpringBootApplication
public class Spring5MvcRestApplication {

public class Spring5MvcRestApplication {

public static void main(String[] args) {
SpringApplication.run(Spring5MvcRestApplication.class, args);
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package guru.springfamework.api.v1.model;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
Expand All @@ -9,7 +10,9 @@
public class CustomerDTO {

private Long id;
@ApiModelProperty(value = "This is the first name",required = true)
private String firstname;
@ApiModelProperty(value = "This is the first name",required = false)
private String lastname;
private String customerUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class VendorDTO {

private Long id;
private String name;
private String nickName;
private String vendorUrl;

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

package guru.springfamework.api.v1.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.List;

@Data
@AllArgsConstructor
public class VendorListDTO
{
List<VendorDTO> vendorDTOS;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/guru/springfamework/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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 lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
Expand All @@ -16,12 +18,14 @@
public class Bootstrap implements CommandLineRunner {
private CategoryRepository categoryRepository;
private CustomerRepository customerRepository;
private VendorRepository vendorRepository;


@Override
public void run(String... args) throws Exception {
loadCategories();
loadCustomers();
loadVendors();

}

Expand Down Expand Up @@ -65,4 +69,20 @@ private void loadCustomers(){
customerRepository.save(customer);
System.out.println("Customers Loaded = " + customerRepository.count());
}

private void loadVendors(){
Vendor vendor = new Vendor();
vendor.setName("Nike");
vendor.setNickName("ccccc");

Vendor vendor1 = new Vendor();
vendor1.setName("Addidass");
vendor1.setNickName("kkkkkk");

vendorRepository.save(vendor);
vendorRepository.save(vendor1);
System.out.println("Vendors Loaded = " + vendorRepository.count());


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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
* Darcy Xian 25/11/20 9:58 am spring5-mvc-rest
*/
@EnableSwagger2
@Configuration

public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(RequestHandlerSelectors.basePackage("guru.springfamework.controllers.v1"))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.apiInfo(metaData());
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
private ApiInfo metaData(){
Contact contact = new Contact("Darcy Xian","http","[email protected]");

return new ApiInfo(
"Spring Framework Darcy",
"Spring Framework 5",
"1.0",
"terms of service: blah",
contact,
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<>());


}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package guru.springfamework.api.controllers.v1;
package guru.springfamework.controllers.v1;

import guru.springfamework.api.v1.model.CategoryDTO;
import guru.springfamework.api.v1.model.CategoryListDTO;
import guru.springfamework.services.CategoryService;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

/**
* Darcy Xian 21/11/20 7:37 pm spring5-mvc-rest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package guru.springfamework.api.controllers.v1;
package guru.springfamework.controllers.v1;

import guru.springfamework.api.v1.model.CustomerDTO;
import guru.springfamework.api.v1.model.CustomerListDTO;

import guru.springfamework.services.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
* Darcy Xian 22/11/20 5:05 pm spring5-mvc-rest
*/

@Api(description = "This is my Customer controller")
@RestController
@RequestMapping(CustomerController.BASE_URL)
@AllArgsConstructor
Expand All @@ -25,7 +24,7 @@ public class CustomerController {

CustomerService customerService;


@ApiOperation(value = "This will get a list of customers.", notes = "These are some notes about the API")
@GetMapping
@ResponseStatus(HttpStatus.OK)
public CustomerListDTO getAllCustomers (){
Expand Down Expand Up @@ -75,12 +74,12 @@ public CustomerDTO patchCustomer(
@ResponseStatus(HttpStatus.OK)
// @RequestBody tell Sping mVC to look at the body of the request and parse it and try to create a
// CustomerDTO out of that
public Void deleteCustomer(
public void deleteCustomer(
@PathVariable Long id){

customerService.deleteCustomerById(id);

return null;
return ;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package guru.springfamework.api.controllers.v1;
package guru.springfamework.controllers.v1;

import guru.springfamework.services.ResourceNotFoundException;
import org.springframework.http.HttpHeaders;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package guru.springfamework.controllers.v1;

import guru.springfamework.api.v1.model.VendorDTO;
import guru.springfamework.api.v1.model.VendorListDTO;
import guru.springfamework.services.VendorService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
* Darcy Xian 24/11/20 4:23 pm spring5-mvc-rest
*/
@Api(description = "This is my Vendor controller")
@RestController
@RequestMapping(VendorController.BASE_URL)
@AllArgsConstructor
public class VendorController {

VendorService vendorService;
public static final String BASE_URL = "/api/v1/vendors";
@GetMapping
@ResponseStatus(HttpStatus.OK)
public VendorListDTO getAllVendors (){
return new VendorListDTO(vendorService.findAllVendors());
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public VendorDTO createNewVendor(@RequestBody VendorDTO vendorDTO){
return vendorService.createNewVendor(vendorDTO);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public void deleteById(@PathVariable Long id){
vendorService.deleteById(id);
return ;
}
@GetMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public VendorDTO findById(@PathVariable Long id){
VendorDTO vendorDTO = vendorService.findBYId(id);
vendorDTO.setVendorUrl(VendorController.BASE_URL + id);
return vendorDTO;
}
@PutMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public VendorDTO updateById(
@PathVariable Long id,
@RequestBody VendorDTO vendorDTO){
return vendorService.saveById(id,vendorDTO);
}
@PatchMapping("/{id}")
@ResponseStatus(HttpStatus.OK)
public VendorDTO patchById(@PathVariable Long id,
@RequestBody VendorDTO vendorDTO){
return vendorService.patchById(id,vendorDTO);
}

}
3 changes: 2 additions & 1 deletion src/main/java/guru/springfamework/domain/Vendor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Vendor
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String vendorUrl;
private String nickName;


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package guru.springfamework.services;

import guru.springfamework.api.controllers.v1.CustomerController;
import guru.springfamework.controllers.v1.CustomerController;
import guru.springfamework.api.v1.mapper.CustomerMapper;
import guru.springfamework.api.v1.model.CustomerDTO;
import guru.springfamework.domain.Customer;
Expand Down Expand Up @@ -69,6 +69,7 @@ public CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO) {
CustomerDTO customerDTO1 = customerMapper.CustomerToCustomerDTO(
customerRepository.save(
customerMapper.CustomerDTOToCustomer(customerDTO)));

customerDTO1.setCustomerUrl(getCustomerUrl( id));
return customerDTO1;
}
Expand All @@ -88,7 +89,7 @@ public CustomerDTO patchCustomer(Long id, CustomerDTO customerDTO) {
CustomerDTO customerDTO1 = customerMapper.CustomerToCustomerDTO(customerRepository.save(customer));
customerDTO1.setCustomerUrl(getCustomerUrl(id));
return customerDTO1;
}).orElseThrow(RuntimeException::new);
}).orElseThrow(ResourceNotFoundException::new);

}
@Override
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/guru/springfamework/services/VendorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package guru.springfamework.services;

import guru.springfamework.api.v1.model.VendorDTO;
import guru.springfamework.domain.Vendor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Darcy Xian 24/11/20 3:59 pm spring5-mvc-rest
*/
@Service
public interface VendorService {

List<VendorDTO> findAllVendors();

VendorDTO createNewVendor(VendorDTO vendorDTO);

void deleteById(Long id);

VendorDTO findBYId(Long id);

VendorDTO saveById(Long id, VendorDTO vendorDTO);

VendorDTO patchById(Long id, VendorDTO vendorDTO);
}
Loading