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

実装例にオリジナルの仕様を追加 #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions README.md
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点それぞれのクエリ文字列にバリデーションをかける。
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/com/fujimoto/rest/DemoController.java
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

badge
たいしたことではないのですが、フォーマットはこのようにするのがbetterです。

Suggested change
public String getName( @RequestParam("id") @Pattern (regexp = "^[1-3]$" ) String id) {
public String getName(@RequestParam("id") @Pattern(regexp = "^[1-3]$") String id) {

Map<Integer, String> namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super badge

Suggested change
Map<Integer, String> namesById = Map.of(1,"Ichiro",2, "Jiro",3,"Saburo");
Map<Integer, String> namesById = Map.of(1, "Ichiro", 2, "Jiro", 3, "Saburo");

return namesById.get(Integer.valueOf(id));
}

@GetMapping("/fruit")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

複数の果物が返却されるのが予想できるのでfruitsがよいですね。

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapの変数名は
keyToValue
valueByKey
のような形式がよいですね。

fruitsToPriceとかでしょうか。

Integer priceMinToInt = Integer.valueOf(priceMin);
List<String> fruitsFilterByPrice = new ArrayList<>();

fruits.forEach((fruitName, fruitPrice) -> {

Choose a reason for hiding this comment

The 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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空ならば[]を返すようにしてもいいかと思います。
Collections.emptyList()的なものがあったはず。
もしくはfilterした結果をそのまま返せばよいですね。

}
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()
Expand All @@ -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" ));
}



Expand Down