From 71e85f992268d2bb9306d37fdabf8ef2c4c68615 Mon Sep 17 00:00:00 2001 From: fujimoto Date: Sat, 2 Jul 2022 15:26:31 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E3=82=AF=E3=82=A8=E3=83=AA=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=88=97=E3=81=AB=E3=83=90=E3=83=AA=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF?= =?UTF-8?q?=E3=82=92=E3=81=8B=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../java/com/fujimoto/rest/DemoController.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 6dfb779..77cf2f4 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springframework.boot:spring-boot-starter-validation' } tasks.named('test') { diff --git a/src/main/java/com/fujimoto/rest/DemoController.java b/src/main/java/com/fujimoto/rest/DemoController.java index 51a4490..99335a1 100644 --- a/src/main/java/com/fujimoto/rest/DemoController.java +++ b/src/main/java/com/fujimoto/rest/DemoController.java @@ -1,24 +1,26 @@ package com.fujimoto.rest; import org.springframework.http.ResponseEntity; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.util.UriComponentsBuilder; +import javax.validation.constraints.Pattern; import java.net.URI; -import java.util.List; import java.util.Map; @RestController +@Validated public class DemoController { @GetMapping("/names") - public List getName(){ - return List.of("Ichiro","Jiro", "Saburo"); + public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) { + Map namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo"); + return namesById.get(Integer.valueOf(id)); } @PostMapping("/names") public ResponseEntity create(@RequestBody CreateForm form){ - System.out.println(form); URI url = UriComponentsBuilder.fromUriString("http:localhost:8080") .path("/names/id") .build() @@ -31,6 +33,10 @@ public ResponseEntity> updateForm(@PathVariable("id") int id return ResponseEntity.ok(Map.of("message", "name successfully updated" )); } + @DeleteMapping("/names/{id}") + public ResponseEntity> deleteForm(@PathVariable("id") int id , @RequestBody UpdateForm form){ + return ResponseEntity.ok(Map.of("message", "name successfully deleted" )); + } From b459032a41648f5e2960a323040639b015abee79 Mon Sep 17 00:00:00 2001 From: fujimoto Date: Sat, 2 Jul 2022 16:56:36 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=83=9E=E3=83=83=E3=83=97fruits=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E3=81=97=E3=81=A6=E3=82=AF=E3=82=A8=E3=83=AA=E6=96=87?= =?UTF-8?q?=E5=AD=97=E5=88=97priceMin=E3=82=88=E3=82=8A=E9=AB=98=E4=BE=A1?= =?UTF-8?q?=E3=81=AA=E6=9E=9C=E7=89=A9=E3=82=92=E6=8A=BD=E5=87=BA=E3=81=97?= =?UTF-8?q?=E3=81=A6=E8=BF=94=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fujimoto/rest/DemoController.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/fujimoto/rest/DemoController.java b/src/main/java/com/fujimoto/rest/DemoController.java index 99335a1..f2f145c 100644 --- a/src/main/java/com/fujimoto/rest/DemoController.java +++ b/src/main/java/com/fujimoto/rest/DemoController.java @@ -7,6 +7,8 @@ import javax.validation.constraints.Pattern; import java.net.URI; +import java.util.ArrayList; +import java.util.List; import java.util.Map; @RestController @@ -19,6 +21,24 @@ public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String return namesById.get(Integer.valueOf(id)); } + @GetMapping("/fruit") + public List getFruitList(@RequestParam("priceMin") @Pattern (regexp = "^[0-9]{1,4}$" ) String priceMin) { + Map fruits = Map.of("apple",100,"orange", 150,"grape",1000); + Integer priceMinToInt = Integer.valueOf(priceMin); + List fruitsFilterByPrice = new ArrayList<>(); + + fruits.forEach((fruitName, fruitPrice) -> { + Integer price = Integer.valueOf(fruitPrice); + if (priceMinToInt < price){ + fruitsFilterByPrice.add(fruitName); + } + }); + if (fruitsFilterByPrice.isEmpty()){ + return null; + } + return fruitsFilterByPrice; + } + @PostMapping("/names") public ResponseEntity create(@RequestBody CreateForm form){ URI url = UriComponentsBuilder.fromUriString("http:localhost:8080") From f1880747bd9ab2ca112e12e881295f67059c3979 Mon Sep 17 00:00:00 2001 From: fujimoto Date: Sat, 2 Jul 2022 17:04:55 +0900 Subject: [PATCH 3/4] =?UTF-8?q?README=E3=81=AB=E8=AA=AC=E6=98=8E=E6=96=87?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..0c66e81 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# 第7回課題 + +## 課題内容 + +- 授業で扱った実装例を参考にHTTPメソッドのGET/POST/PATCH/DELETEのリクエストを扱えるControllerを実装しましょう。 +- オリジナルの仕様を加えてみましょう。 + +例) +- http://localhost:8080/names?name=koyamaのようにクエリ文字列を受け取れるようにする +- 名前以外にも生年月日を受け取れるよう実装する +- バリデーションについて調べてnameが空文字、null、20文字以上の場合はエラーとする + +## アレンジした内容 + +- idとnameのマップを作成し、idをクエリ文字列として使用し、該当のnameを返す。 +- マップfruitsを作成し、priceMin(価格下限)をクエリ文字として使用し、priceMinよりも高価な果物を返す。 +- 上記2点それぞれのクエリ文字列にバリデーションをかける。 From cbd7a959de791449b10024761b476d84d1863af7 Mon Sep 17 00:00:00 2001 From: fujimoto Date: Sun, 3 Jul 2022 10:08:52 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8C=87=E6=91=98=E4=BA=8B=E9=A0=85?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3(=E3=83=90=E3=83=AA=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=81=AE=E3=82=B9=E3=83=86=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=82=B9=E3=82=B3=E3=83=BC=E3=83=89=E5=AF=BE=E5=BF=9C?= =?UTF-8?q?=E9=99=A4=E3=81=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fujimoto/rest/DemoController.java | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/fujimoto/rest/DemoController.java b/src/main/java/com/fujimoto/rest/DemoController.java index f2f145c..2efd5da 100644 --- a/src/main/java/com/fujimoto/rest/DemoController.java +++ b/src/main/java/com/fujimoto/rest/DemoController.java @@ -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; @@ -16,31 +15,26 @@ public class DemoController { @GetMapping("/names") - public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) { - Map namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo"); + public String getName(@RequestParam("id") @Pattern(regexp = "^[1-3]$") String id) { + Map namesById = Map.of(1, "Ichiro", 2, "Jiro", 3, "Saburo"); return namesById.get(Integer.valueOf(id)); } - @GetMapping("/fruit") - public List getFruitList(@RequestParam("priceMin") @Pattern (regexp = "^[0-9]{1,4}$" ) String priceMin) { - Map fruits = Map.of("apple",100,"orange", 150,"grape",1000); + @GetMapping("/fruits") + public List getFruitList(@RequestParam("priceMin") @Pattern(regexp = "^[0-9]{1,4}$") String priceMin) { + Map priceByFruit = Map.of("apple", 100, "orange", 150, "grape", 1000); Integer priceMinToInt = Integer.valueOf(priceMin); - List fruitsFilterByPrice = new ArrayList<>(); - - fruits.forEach((fruitName, fruitPrice) -> { - Integer price = Integer.valueOf(fruitPrice); - if (priceMinToInt < price){ - fruitsFilterByPrice.add(fruitName); - } - }); - if (fruitsFilterByPrice.isEmpty()){ - return null; - } + + List fruitsFilterByPrice = priceByFruit.entrySet().stream() + .filter(map -> map.getValue() > priceMinToInt) + .map(map -> map.getKey()) + .toList(); + return fruitsFilterByPrice; } @PostMapping("/names") - public ResponseEntity create(@RequestBody CreateForm form){ + public ResponseEntity create(@RequestBody CreateForm form) { URI url = UriComponentsBuilder.fromUriString("http:localhost:8080") .path("/names/id") .build() @@ -49,15 +43,14 @@ public ResponseEntity create(@RequestBody CreateForm form){ } @PatchMapping("/names/{id}") - public ResponseEntity> updateForm(@PathVariable("id") int id , @RequestBody UpdateForm form){ - return ResponseEntity.ok(Map.of("message", "name successfully updated" )); + public ResponseEntity> updateForm(@PathVariable("id") int id, @RequestBody UpdateForm form) { + return ResponseEntity.ok(Map.of("message", "name successfully updated")); } @DeleteMapping("/names/{id}") - public ResponseEntity> deleteForm(@PathVariable("id") int id , @RequestBody UpdateForm form){ - return ResponseEntity.ok(Map.of("message", "name successfully deleted" )); + public ResponseEntity> deleteForm(@PathVariable("id") int id, @RequestBody UpdateForm form) { + return ResponseEntity.ok(Map.of("message", "name successfully deleted")); } - }