Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b72ffa

Browse files
committed
Add validation check on request handlers:
- check if request a has valid dto.
1 parent db6947c commit 2b72ffa

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.nihat.springwebfluxdemo.controllers.mvc;
2+
3+
import com.nihat.springwebfluxdemo.model.ProductDTO;
4+
import com.nihat.springwebfluxdemo.services.ProductService;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.*;
10+
import reactor.core.publisher.Flux;
11+
import reactor.core.publisher.Mono;
12+
13+
@RestController
14+
@RequiredArgsConstructor
15+
public class ProductController {
16+
17+
public static final String PRODUCT_BASE_URL = "/api/v1/products";
18+
public static final String PRODUCT_ID_URL = PRODUCT_BASE_URL + "/{id}";
19+
20+
private final ProductService productService;
21+
22+
// handle get all products:
23+
@GetMapping(PRODUCT_BASE_URL)
24+
public Flux<ProductDTO> getAllProducts() {
25+
return productService.getAllProducts();
26+
}
27+
28+
// handle get product by id:
29+
@GetMapping(PRODUCT_ID_URL)
30+
public Mono<ResponseEntity<ProductDTO>> getProductById(@PathVariable String id) {
31+
return productService.getProductById(id)
32+
.map(ResponseEntity::ok)
33+
.defaultIfEmpty(ResponseEntity.notFound().build());
34+
}
35+
36+
// handle create product that returns the created mono and location header:
37+
@PostMapping(PRODUCT_BASE_URL)
38+
@ResponseStatus(HttpStatus.CREATED)
39+
public Mono<ProductDTO> createProduct(@RequestBody @Validated Mono<ProductDTO> productDTOMono) {
40+
return productService.saveProduct(productDTOMono);
41+
}
42+
43+
// handle update product:
44+
@PutMapping(PRODUCT_ID_URL)
45+
public Mono<ResponseEntity<ProductDTO>> updateProduct(@PathVariable String id, @RequestBody @Validated ProductDTO productDTO) {
46+
return productService.updateProduct(id, productDTO)
47+
.map(ResponseEntity::ok)
48+
.defaultIfEmpty(ResponseEntity.notFound().build());
49+
}
50+
51+
// handle delete product:
52+
@DeleteMapping(PRODUCT_ID_URL)
53+
@ResponseStatus(HttpStatus.NO_CONTENT)
54+
public Mono<Void> deleteProduct(@PathVariable String id) {
55+
return productService.deleteProductById(id);
56+
}
57+
58+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /