|
| 1 | +package com.example.demo.login.controller; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 7 | +import org.springframework.web.bind.annotation.GetMapping; |
| 8 | +import org.springframework.web.bind.annotation.PathVariable; |
| 9 | +import org.springframework.web.bind.annotation.PostMapping; |
| 10 | +import org.springframework.web.bind.annotation.PutMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestBody; |
| 12 | +import org.springframework.web.bind.annotation.RestController; |
| 13 | + |
| 14 | +import com.example.demo.domain.model.User; |
| 15 | +import com.example.demo.domain.service.RestService; |
| 16 | + |
| 17 | + |
| 18 | +@RestController |
| 19 | +public class UserRestController { |
| 20 | + @Autowired |
| 21 | + RestService service; |
| 22 | + |
| 23 | + // ユーザー全件取得 |
| 24 | + @GetMapping("/rest/get") |
| 25 | + public List<User> getUserMany() { |
| 26 | + // ユーザー全件取得 |
| 27 | + return service.selectMany(); |
| 28 | + } |
| 29 | + |
| 30 | + // ユーザー1件取得 |
| 31 | + @GetMapping("/rest/get/{id:.+}") |
| 32 | + public User getUserOne(@PathVariable("id") String userId) { |
| 33 | + // ユーザー1件取得 |
| 34 | + return service.selectOne(userId); |
| 35 | + } |
| 36 | + |
| 37 | + // ユーザー1件登録 |
| 38 | + @PostMapping("/rest/insert") |
| 39 | + public String postUserOne(@RequestBody User user) { |
| 40 | + // ユーザーを1件登録 |
| 41 | + boolean result = service.insert(user); |
| 42 | + |
| 43 | + String str = ""; |
| 44 | + |
| 45 | + if(result) { |
| 46 | + str = "{\"result\":\"ok\"}"; |
| 47 | + } else { |
| 48 | + str = "{\"result\":\"error\"}"; |
| 49 | + } |
| 50 | + |
| 51 | + // 結果用の文字列を返す |
| 52 | + return str; |
| 53 | + } |
| 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 | + } |
| 89 | +} |
0 commit comments