Skip to content

Commit a56939e

Browse files
committed
Add rest post routes
1 parent 1af64a9 commit a56939e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/com/example/demo/domain/service/jdbc/RestServiceJdbcImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ public class RestServiceJdbcImpl implements RestService {
2323
// 1件登録用メソッド
2424
@Override
2525
public boolean insert(User user) {
26-
return false;
26+
int result = dao.insertOne(user);
27+
28+
if(result == 0) {
29+
return false;
30+
} else {
31+
return true;
32+
}
2733
}
2834

2935
// 1件検索用メソッド

src/main/java/com/example/demo/login/controller/UserRestController.java

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.web.bind.annotation.GetMapping;
77
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
810
import org.springframework.web.bind.annotation.RestController;
911

1012
import com.example.demo.domain.model.User;
@@ -29,4 +31,22 @@ public User getUserOne(@PathVariable("id") String userId) {
2931
// ユーザー1件取得
3032
return service.selectOne(userId);
3133
}
34+
35+
// ユーザー1件登録
36+
@PostMapping("/rest/insert")
37+
public String postUserOne(@RequestBody User user) {
38+
// ユーザーを1件登録
39+
boolean result = service.insert(user);
40+
41+
String str = "";
42+
43+
if(result) {
44+
str = "{\"result\":\"ok\"}";
45+
} else {
46+
str = "{\"result\":\"error\"}";
47+
}
48+
49+
// 結果用の文字列を返す
50+
return str;
51+
}
3252
}

0 commit comments

Comments
 (0)