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 category and Customer put patch delete get post
  • Loading branch information
lxian-coder committed Nov 24, 2020
commit 0b5e65a77aeeab711e93332215bebdb133ebb5ef
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

/**
* Darcy Xian 21/11/20 7:37 pm spring5-mvc-rest
*/
@Controller
@RequestMapping("/api/v1/categories/")
@RestController
@RequestMapping(CategoryController.BASE_URL)
@AllArgsConstructor
public class CategoryController {

private final CategoryService categoryService;
public static final String BASE_URL = "/api/v1/categories/";

public CategoryController(CategoryService categoryService){
private final CategoryService categoryService;

this.categoryService = categoryService;
}

@GetMapping
public ResponseEntity<CategoryListDTO> getAllCategories(){
return new ResponseEntity<CategoryListDTO>(
new CategoryListDTO(categoryService.getAllCategories()), HttpStatus.OK);
@ResponseStatus(HttpStatus.OK)
public CategoryListDTO getAllCategories(){
return new CategoryListDTO(categoryService.getAllCategories());
}
@GetMapping("{name}")
public ResponseEntity<CategoryDTO> getCategoryByName (@PathVariable String name){
return new ResponseEntity<CategoryDTO>(
categoryService.getCategoryByName(name),HttpStatus.OK
);
@ResponseStatus(HttpStatus.OK)
public CategoryDTO getCategoryByName (@PathVariable String name){
return categoryService.getCategoryByName(name);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,72 @@
* Darcy Xian 22/11/20 5:05 pm spring5-mvc-rest
*/

@Controller
@EnableWebMvc
@RequestMapping("/api/v1/customers")
@RestController
@RequestMapping(CustomerController.BASE_URL)
@AllArgsConstructor
public class CustomerController {

public static final String BASE_URL = "/api/v1/customers";


CustomerService customerService;


@GetMapping
public ResponseEntity<CustomerListDTO> getAllCustomers (){
return new ResponseEntity<CustomerListDTO>(
new CustomerListDTO(customerService.findAllCustomers()), HttpStatus.OK);
@ResponseStatus(HttpStatus.OK)
public CustomerListDTO getAllCustomers (){
return new CustomerListDTO(customerService.findAllCustomers());
}

@GetMapping("/{id}")
public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable Long id){
@ResponseStatus(HttpStatus.OK)
public CustomerDTO getCustomerById(@PathVariable Long id){
CustomerDTO customerDTO = customerService.findCustomerById(id);
customerDTO.setCustomerUrl("/api/v1/cusotmers/"+id);
customerDTO.setCustomerUrl(BASE_URL + id);


return new ResponseEntity<CustomerDTO>(
customerDTO ,HttpStatus.OK);
return customerDTO;
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
// @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 ResponseEntity<CustomerDTO> createNewCustomer(@RequestBody CustomerDTO customerDTO){
return new ResponseEntity<CustomerDTO>(customerService.createNewCustomer(customerDTO),HttpStatus.CREATED);
public CustomerDTO createNewCustomer(@RequestBody CustomerDTO customerDTO){
return customerService.createNewCustomer(customerDTO);
}

@PutMapping({"/{id}"})
@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 CustomerDTO updateCustomer(
@PathVariable Long id,
@RequestBody CustomerDTO customerDTO){
return customerService.saveCustomerByDTO(id,customerDTO);
}

@PatchMapping({"/{id}"})
@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 ResponseEntity<CustomerDTO> updateCustomer(
public CustomerDTO patchCustomer(
@PathVariable Long id,
@RequestBody CustomerDTO customerDTO){
return new ResponseEntity<CustomerDTO>(
customerService.saveCustomerByDTO(id,customerDTO),HttpStatus.CREATED);
return customerService.patchCustomer(id,customerDTO);
}


@DeleteMapping({"/{id}"})
@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(
@PathVariable Long id){

customerService.deleteCustomerById(id);

return null;
}
}

Expand All @@ -69,3 +94,44 @@ public ResponseEntity<CustomerDTO> updateCustomer(












































Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springfamework.api.controllers.v1;

import guru.springfamework.services.ResourceNotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
* Darcy Xian 24/11/20 12:13 pm spring5-mvc-rest
*/
@ControllerAdvice
public class RestReponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler({ResourceNotFoundException.class})
public ResponseEntity<Object> handleNotFoundException(Exception exception, WebRequest request){

return new ResponseEntity<Object>("Resource not found",new HttpHeaders(), HttpStatus.NOT_FOUND);
}
}
25 changes: 25 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,25 @@
package guru.springfamework.api.v1.mapper;

import guru.springfamework.api.v1.model.VendorDTO;
import guru.springfamework.domain.Vendor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;


/**
* Darcy Xian 24/11/20 3:36 pm spring5-mvc-rest
*/
@Mapper
public interface VendorMapper {

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

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

@Mapping(source = "id", target = "id")
Vendor vendorDTOToVendor(VendorDTO vendorDTO);


}
20 changes: 20 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,20 @@
package guru.springfamework.api.v1.model;

import lombok.Data;

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

/**
* Darcy Xian 24/11/20 3:30 pm spring5-mvc-rest
*/
@Data
public class VendorDTO {


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

}
11 changes: 11 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,11 @@

package guru.springfamework.api.v1.model;

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

@Data
public class VendorListDTO
{
List<VendorDTO> vendorDTOS;
}
24 changes: 24 additions & 0 deletions src/main/java/guru/springfamework/domain/Vendor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

package guru.springfamework.domain;

import lombok.Data;
import org.hibernate.annotations.Generated;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Data
@Entity
public class Vendor
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String vendorUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package guru.springfamework.repositories;

import guru.springfamework.domain.Vendor;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
* Darcy Xian 24/11/20 3:57 pm spring5-mvc-rest
*/
@Repository
public interface VendorRepository extends JpaRepository<Vendor,Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ public interface CustomerService {

CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO);

CustomerDTO patchCustomer(Long id, CustomerDTO customerDTO);

void deleteCustomerById(Long id);

}
Loading