-
Notifications
You must be signed in to change notification settings - Fork 0
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
実装例にオリジナルの仕様を追加 #1
base: main
Are you sure you want to change the base?
Changes from 3 commits
71e85f9
b459032
f188074
cbd7a95
e45fb69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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点それぞれのクエリ文字列にバリデーションをかける。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,46 @@ | ||
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.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@Validated | ||
public class DemoController { | ||
|
||
@GetMapping("/names") | ||
public List<String> getName(){ | ||
return List.of("Ichiro","Jiro", "Saburo"); | ||
public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) { | ||
Map<Integer, String> namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return namesById.get(Integer.valueOf(id)); | ||
} | ||
|
||
@GetMapping("/fruit") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 複数の果物が返却されるのが予想できるので |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mapの変数名は
|
||
Integer priceMinToInt = Integer.valueOf(priceMin); | ||
List<String> fruitsFilterByPrice = new ArrayList<>(); | ||
|
||
fruits.forEach((fruitName, fruitPrice) -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Streamのfilter使ってみるとよいです。 |
||
Integer price = Integer.valueOf(fruitPrice); | ||
if (priceMinToInt < price){ | ||
fruitsFilterByPrice.add(fruitName); | ||
} | ||
}); | ||
if (fruitsFilterByPrice.isEmpty()){ | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空ならば |
||
} | ||
return fruitsFilterByPrice; | ||
} | ||
|
||
@PostMapping("/names") | ||
public ResponseEntity<String> create(@RequestBody CreateForm form){ | ||
System.out.println(form); | ||
URI url = UriComponentsBuilder.fromUriString("http:localhost:8080") | ||
.path("/names/id") | ||
.build() | ||
|
@@ -31,6 +53,10 @@ public ResponseEntity<Map<String, String>> updateForm(@PathVariable("id") int id | |
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" )); | ||
} | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たいしたことではないのですが、フォーマットはこのようにするのがbetterです。