Skip to content

Commit c9ddcce

Browse files
committed
Add rest delete and update routes
1 parent a56939e commit c9ddcce

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,24 @@ public List<User> selectMany() {
4747
// 1件更新用メソッド
4848
@Override
4949
public boolean update(User user) {
50-
return false;
50+
int result = dao.updateOne(user);
51+
52+
if(result == 0) {
53+
return false;
54+
} else {
55+
return true;
56+
}
5157
}
5258

5359
// 1件削除用メソッド
5460
@Override
5561
public boolean delete(String userId) {
56-
return false;
62+
int result = dao.deleteOne(userId);
63+
64+
if(result == 0) {
65+
return false;
66+
} else {
67+
return true;
68+
}
5769
}
5870
}

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

+37
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.util.List;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
67
import org.springframework.web.bind.annotation.GetMapping;
78
import org.springframework.web.bind.annotation.PathVariable;
89
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.PutMapping;
911
import org.springframework.web.bind.annotation.RequestBody;
1012
import org.springframework.web.bind.annotation.RestController;
1113

@@ -49,4 +51,39 @@ public String postUserOne(@RequestBody User user) {
4951
// 結果用の文字列を返す
5052
return str;
5153
}
54+
55+
// ユーザー1件更新
56+
@PutMapping("/rest/update")
57+
public String putUserOne(@RequestBody User user) {
58+
// ユーザー1件更新
59+
boolean result = service.update(user);
60+
61+
String str = "";
62+
63+
if(result) {
64+
str = "{\"result\":\"ok\"}";
65+
} else {
66+
str = "{\"result\":\"error\"}";
67+
}
68+
69+
return str;
70+
}
71+
72+
// ユーザー1件削除
73+
@DeleteMapping("/rest/delete/{id:.+}")
74+
public String deleteUserOne(@PathVariable("id") String userId) {
75+
// ユーザー1件削除
76+
boolean result = service.delete(userId);
77+
78+
String str = "";
79+
80+
if(result) {
81+
str = "{\"result\":\"ok\"}";
82+
} else {
83+
str = "{\"result\":\"error\"}";
84+
}
85+
86+
// 結果用の文字列をリターン
87+
return str;
88+
}
5289
}

0 commit comments

Comments
 (0)