Skip to content
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

指摘事項修正(バリデーションのステータスコード対応除く) #2

Merged
merged 1 commit into from
Jul 3, 2022
Merged
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
39 changes: 16 additions & 23 deletions src/main/java/com/fujimoto/rest/DemoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import javax.validation.constraints.Pattern;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -16,31 +15,26 @@
public class DemoController {

@GetMapping("/names")
public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) {
Map<Integer, String> namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo");
public String getName(@RequestParam("id") @Pattern(regexp = "^[1-3]$") String id) {
Map<Integer, String> namesById = Map.of(1, "Ichiro", 2, "Jiro", 3, "Saburo");
return namesById.get(Integer.valueOf(id));
}

@GetMapping("/fruit")
public List<String> getFruitList(@RequestParam("priceMin") @Pattern (regexp = "^[0-9]{1,4}$" ) String priceMin) {
Map<String, Integer> fruits = Map.of("apple",100,"orange", 150,"grape",1000);
@GetMapping("/fruits")
public List<String> getFruitList(@RequestParam("priceMin") @Pattern(regexp = "^[0-9]{1,4}$") String priceMin) {
Map<String, Integer> priceByFruit = Map.of("apple", 100, "orange", 150, "grape", 1000);
Integer priceMinToInt = Integer.valueOf(priceMin);
List<String> fruitsFilterByPrice = new ArrayList<>();

fruits.forEach((fruitName, fruitPrice) -> {
Integer price = Integer.valueOf(fruitPrice);
if (priceMinToInt < price){
fruitsFilterByPrice.add(fruitName);
}
});
if (fruitsFilterByPrice.isEmpty()){
return null;
}

List<String> fruitsFilterByPrice = priceByFruit.entrySet().stream()
.filter(map -> map.getValue() > priceMinToInt)
.map(map -> map.getKey())
.toList();

return fruitsFilterByPrice;
}

@PostMapping("/names")
public ResponseEntity<String> create(@RequestBody CreateForm form){
public ResponseEntity<String> create(@RequestBody CreateForm form) {
URI url = UriComponentsBuilder.fromUriString("http:localhost:8080")
.path("/names/id")
.build()
Expand All @@ -49,15 +43,14 @@ public ResponseEntity<String> create(@RequestBody CreateForm form){
}

@PatchMapping("/names/{id}")
public ResponseEntity<Map<String, String>> updateForm(@PathVariable("id") int id , @RequestBody UpdateForm form){
return ResponseEntity.ok(Map.of("message", "name successfully updated" ));
public ResponseEntity<Map<String, String>> updateForm(@PathVariable("id") int id, @RequestBody UpdateForm form) {
return ResponseEntity.ok(Map.of("message", "name successfully updated"));
}

@DeleteMapping("/names/{id}")
public ResponseEntity<Map<String, String>> deleteForm(@PathVariable("id") int id , @RequestBody UpdateForm form){
return ResponseEntity.ok(Map.of("message", "name successfully deleted" ));
public ResponseEntity<Map<String, String>> deleteForm(@PathVariable("id") int id, @RequestBody UpdateForm form) {
return ResponseEntity.ok(Map.of("message", "name successfully deleted"));
}



}