Repository: AW05724/java-springboot
Pull request #5: 課題4
Author: login: AW05724
Created: 2025-03-18T09:20:59Z | Merged: 2025-03-27T04:40:16Z

3/18 進捗分です。


Diff:
diff --git a/pom.xml b/pom.xml
index eb1ba91..8d3fa56 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,6 +37,11 @@
             <groupId>org.mybatis.spring.boot</groupId>
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>3.0.3</version>
+        </dependency>
+                <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <scope>provided</scope>
         </dependency>
     </dependencies>
 
@@ -56,4 +61,4 @@
         </plugins>
     </build>
 
-</project>
\ No newline at end of file
+</project>
diff --git a/src/main/java/com/example/demo/controller/UserController.java b/src/main/java/com/example/demo/controller/UserController.java
index 0d5e9f2..722c54b 100644
--- a/src/main/java/com/example/demo/controller/UserController.java
+++ b/src/main/java/com/example/demo/controller/UserController.java
@@ -1,13 +1,18 @@
 package com.example.demo.controller;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import com.example.demo.repository.UserRepository;
 import com.example.demo.repository.mapper.User;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 @RestController
@@ -25,4 +30,42 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
+
+    @PostMapping("/users/update")
+    public ResponseEntity<String> updateUser(
+            @RequestParam("id") int id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .lastLogined(LocalDateTime.now())
+                .build();
+        userRepository.update(id, user);
+        return ResponseEntity.ok("User updated successfully");
+    }
+
+    @PostMapping("/users/create")
+    public ResponseEntity<String> createUser(
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .created(LocalDateTime.now())
+                .lastLogined(LocalDateTime.now())
+                .build();
+        userRepository.insert(user);
+        return ResponseEntity.ok("User created successfully");
+    }
 }
diff --git a/src/main/java/com/example/demo/repository/UserRepository.java b/src/main/java/com/example/demo/repository/UserRepository.java
index a2fa66b..55ce274 100644
--- a/src/main/java/com/example/demo/repository/UserRepository.java
+++ b/src/main/java/com/example/demo/repository/UserRepository.java
@@ -8,4 +8,8 @@ public interface UserRepository {
     List<User> findAll();
 
     User findById(Integer id);
+
+    void update(Integer id,User user);
+
+    void insert(User user);
 }
diff --git a/src/main/java/com/example/demo/repository/UserRepositoryImpl.java b/src/main/java/com/example/demo/repository/UserRepositoryImpl.java
index c5d792c..176da94 100644
--- a/src/main/java/com/example/demo/repository/UserRepositoryImpl.java
+++ b/src/main/java/com/example/demo/repository/UserRepositoryImpl.java
@@ -23,4 +23,12 @@ public List<User> findAll() {
     public User findById(Integer id) {
         return userMapper.findById(id);
     }
+
+    public void update(Integer id, User user) {
+        userMapper.update(id, user);
+    }
+
+    public void insert(User user) {
+        userMapper.insert(user);
+    }
 }
diff --git a/src/main/java/com/example/demo/repository/mapper/User.java b/src/main/java/com/example/demo/repository/mapper/User.java
index e2d9c34..239b582 100644
--- a/src/main/java/com/example/demo/repository/mapper/User.java
+++ b/src/main/java/com/example/demo/repository/mapper/User.java
@@ -2,6 +2,11 @@
 
 import java.time.LocalDateTime;
 
+import lombok.Builder;
+import lombok.Getter;
+
+@Getter
+@Builder
 public class User {
     private int id;
     private String mail;
@@ -10,62 +15,4 @@ public class User {
     private LocalDateTime created;
     private LocalDateTime lastLogined;
     private boolean enabled;
-
-    // ゲッター、セッター、コンストラクタなどは省略
-    // Lombokを使用する場合は、@Dataアノテーションなどを利用する
-    public Integer getId() {
-        return id;
-    }
-
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    public String getMail() {
-        return mail;
-    }
-
-    public void setMail(String mail) {
-        this.mail = mail;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-    public String getRoles() {
-        return roles;
-    }
-
-    public void setRoles(String roles) {
-        this.roles = roles;
-    }
-
-    public LocalDateTime getCreated() {
-        return created;
-    }
-
-    public void setCreated(LocalDateTime created) {
-        this.created = created;
-    }
-
-    public LocalDateTime getLastLogined() {
-        return lastLogined;
-    }
-
-    public void setLastLogined(LocalDateTime lastLogined) {
-        this.lastLogined = lastLogined;
-    }
-
-    public boolean getEnabled() {
-        return enabled;
-    }
-
-    public void setEnabled(boolean enabled) {
-        this.enabled = enabled;
-    }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/demo/repository/mapper/UserMapper.java b/src/main/java/com/example/demo/repository/mapper/UserMapper.java
index fe59e51..41e3200 100644
--- a/src/main/java/com/example/demo/repository/mapper/UserMapper.java
+++ b/src/main/java/com/example/demo/repository/mapper/UserMapper.java
@@ -1,10 +1,14 @@
 package com.example.demo.repository.mapper;
 
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
 import java.util.List;
 
 @Mapper
 public interface UserMapper {
     List<User> findAll();
     User findById(int id);
-}
+    void update(@Param("id") int id, @Param("user") User user);
+    void insert(@Param("user")User user);
+} 
diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml
index e21d668..bd9ab25 100644
--- a/src/main/resources/mapper/UserMapper.xml
+++ b/src/main/resources/mapper/UserMapper.xml
@@ -20,4 +20,19 @@
         SELECT id,mail,password,roles,created,last_logined,enabled FROM `user` WHERE id = #{id}
     </select>
 
+    <update id = "update" parameterType="com.example.demo.repository.mapper.User">
+        UPDATE `user`
+        SET mail = #{user.mail},
+            password = #{user.password},
+            roles = #{user.roles},
+            last_logined = #{user.lastLogined},
+            enabled = #{user.enabled}
+        WHERE id = #{id}
+    </update>
+
+    <insert id = "insert" parameterType = "com.example.demo.repository.mapper.User">
+        INSERT INTO `user`(mail,password,roles,created,last_logined,enabled)
+        VALUES (#{user.mail},#{user.password},#{user.roles},#{user.created},#{user.lastLogined},#{user.enabled})
+    </insert>
+
 </mapper>


Reviews:
user: login: ohorigenal
body: `curl -XPOST -H 'mail:aaa@com' -H 'password:abc' -H 'roles:AAA' -H 'enabled:true' 'http://localhost:8080/users/update?id=3'`

動作確認は上記のような形で大丈夫でした
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-22T03:55:43Z
user: login: ohorigenal
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-22T03:56:29Z
user: login: ohorigenal
state: COMMENTED
author_association: NONE
submitted_at: 2025-03-26T12:42:16Z
user: login: ohorigenal
state: APPROVED
author_association: NONE
submitted_at: 2025-03-27T04:38:17Z

Review comments:
diff_hunk: @@ -25,4 +29,39 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
+
+    @PostMapping("/users/update")
+    public String updateUser(
+            @RequestParam("id") Integer id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") String enabled
+    ) {
+        User user = new User();
+        user.setMail(mail);
+        user.setPassword(password);
+        user.setRoles(roles);
+        user.setEnabled(Boolean.parseBoolean(enabled));
+        user.setLastLogined(LocalDateTime.now());
+        userRepository.update(id, user);
+        return "User updated successfully";
+    }
+
+    @PostMapping("/users/create")
+    public String createUser(
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") String enabled
+    ) {
+        User user = new User();
+        user.setMail(mail);
+        user.setPassword(password);
+        user.setRoles(roles);
+        user.setEnabled(Boolean.parseBoolean(enabled));
+        user.setCreated(LocalDateTime.now());
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: ```
user.setLastLogined(LocalDateTime.now());
```
が不足しており、DBのカラムがNotNullでDBエラーとなるため追加をお願いします。
created_at: 2025-03-22T03:34:18Z
author_association: NONE
diff_hunk: @@ -25,4 +29,39 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
+
+    @PostMapping("/users/update")
+    public String updateUser(
+            @RequestParam("id") Integer id,
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: ここはintでいいと思います。なるべくプリミティブ型の方針のため
created_at: 2025-03-22T03:36:51Z
author_association: NONE
diff_hunk: @@ -25,4 +29,39 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
+
+    @PostMapping("/users/update")
+    public String updateUser(
+            @RequestParam("id") Integer id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") String enabled
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: boolean型でいいと思います。true, false以外を受け付けたくなく、その場合400エラーとしたいため。
現在の実装だとtrue, false以外の文字列でもfalseに設定されると思われます
created_at: 2025-03-22T03:40:34Z
author_association: NONE
diff_hunk: @@ -25,4 +29,39 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
+
+    @PostMapping("/users/update")
+    public String updateUser(
+            @RequestParam("id") Integer id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") String enabled
+    ) {
+        User user = new User();
+        user.setMail(mail);
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: 基本的にはsetterの利用はNGなのでインスタンス生成時に値を設定するのが正解になります。
lombokを使うならUserクラスに`@Builder`をつけ、User.Builder().mail(..).password(...)...という形も現場ではよく使います。
created_at: 2025-03-22T03:46:07Z
author_association: NONE
diff_hunk: @@ -2,6 +2,9 @@
 
 import java.time.LocalDateTime;
 
+import lombok.Data;
+
+@Data
path: src/main/java/com/example/demo/repository/mapper/User.java
user: login: ohorigenal
body: `@Data`はセッターも含まれ基本的にはNGなので、ここは`@Getter`と`@Builder`で今の所はいいかなと思います。
17行目以降は消してもらえれば
created_at: 2025-03-22T03:47:46Z
author_association: NONE
diff_hunk: @@ -0,0 +1,5 @@
+package com.example.demo.repository.mapper;
+
+public @interface NotNull {
path: src/main/java/com/example/demo/repository/mapper/NotNull.java
user: login: ohorigenal
body: このインターフェイスは削除でいいと思います
created_at: 2025-03-22T03:56:29Z
author_association: NONE
diff_hunk: @@ -38,6 +38,11 @@
             <artifactId>mybatis-spring-boot-starter</artifactId>
             <version>3.0.3</version>
         </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <version>1.18.36</version>
+        </dependency>
path: pom.xml
user: login: ohorigenal
body: version項目はspringbootで管理されているので、設定は不要になります。
下記で大丈夫だと思います。実行時にはlombokは不要なため、scope providedです
```
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
```
created_at: 2025-03-26T12:37:44Z
author_association: NONE
diff_hunk: @@ -25,4 +30,41 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
-}
+
+    @PostMapping("/users/update")
+    public ResponseEntity<String> updateUser(
+            @RequestParam("id") int id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .lastLogined(LocalDateTime.now())
+                .build();
+        userRepository.update(id, user);
+        return ResponseEntity.ok("User updated successfully");
+    }
+
+    @PostMapping("/users/create")
+    public ResponseEntity<String> createUser(
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .created(LocalDateTime.now())
+                .build();
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: たぶんlastLoginedも追加しないとnullでDBエラーになる気がします。
確認をお願いします
created_at: 2025-03-26T12:40:52Z
author_association: NONE
diff_hunk: @@ -25,4 +30,41 @@ public List<User> getUsers() {
     public User getUserById(@PathVariable int id) {
         return userRepository.findById(id);
     }
-}
+
+    @PostMapping("/users/update")
+    public ResponseEntity<String> updateUser(
+            @RequestParam("id") int id,
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .lastLogined(LocalDateTime.now())
+                .build();
+        userRepository.update(id, user);
+        return ResponseEntity.ok("User updated successfully");
+    }
+
+    @PostMapping("/users/create")
+    public ResponseEntity<String> createUser(
+            @RequestHeader("mail") String mail,
+            @RequestHeader("password") String password,
+            @RequestHeader("roles") String roles,
+            @RequestHeader("enabled") boolean enabled
+    ) {
+        User user = User.builder()
+                .mail(mail)
+                .password(password)
+                .roles(roles)
+                .enabled(enabled)
+                .created(LocalDateTime.now())
+                .build();
+        userRepository.insert(user);
+        return ResponseEntity.ok("User created successfully");
+    }
+}
path: src/main/java/com/example/demo/controller/UserController.java
user: login: ohorigenal
body: ファイルの末尾に改行コードも追加をお願いします。(エディタで保存時に末尾改行追加設定をするのが楽な気がします)
created_at: 2025-03-26T12:41:54Z
author_association: NONE

PR comments:
user: login: AW05724
created_at: 2025-03-26T09:15:47Z
body: ##概要
springboot課題４動作確認完了

<修正概要>
*DBにLocalDateTimeを追加
*@RequestParamのidをintに変更
*@RequestParamのenabledをbooleanに変更
*@Dataの削除、@Getter@Builderの追加
*不要なインターフェースの削除

##動作確認方法
<動作確認方法>
1. Spring Bootの起動
2. 下記コマンドを実行
3.  http://localhost:8080/usersにアクセス
4. id=3のデータが変更されていることを確認

<コマンド>
curl -XPOST -H 'mail:aaa@com' -H 'password:abc' -H 'roles:AAA' -H 'enabled:true' 'http://localhost:8080/users/update?id=3'

author_association: OWNER
user: login: AW05724
created_at: 2025-03-27T03:06:13Z
body: ##概要
springboot課題４フィードバック対応

<修正概要>
*pom.xmlにてlombokの記載変更
*UserController.javaにてlastLoginedの追加
*VS Codeでファイル末尾に自動的に改行を設定

##動作確認方法
<動作確認方法>

1. Spring Bootの起動
2. 下記コマンドを実行
3. http://localhost:8080/usersにアクセス
4. id=4のデータが追加されていることを確認

<コマンド>
curl -XPOST -H 'mail:aaa@com' -H 'password:abc' -H 'roles:AAA' -H 'enabled:true' 'http://localhost:8080/users/create?id=4'


author_association: OWNER