Skip to content

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

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
33 changes: 26 additions & 7 deletions src/main/java/com/fujimoto/rest/DemoController.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
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<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");
return namesById.get(Integer.valueOf(id));
}

@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 = priceByFruit.entrySet().stream()
.filter(map -> map.getValue() > priceMinToInt)
.map(map -> map.getKey())
.toList();

return fruitsFilterByPrice;
}

@PostMapping("/names")
public ResponseEntity<String> create(@RequestBody CreateForm form){
System.out.println(form);
public ResponseEntity<String> create(@RequestBody CreateForm form) {
URI url = UriComponentsBuilder.fromUriString("http:localhost:8080")
.path("/names/id")
.build()
Expand All @@ -27,11 +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"));
}


}