-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JDBC 라이브러리 구현하기 - 4단계] 베베(최원용) 미션 제출합니다. (#590)
* refactor: Service 계층 분리 * refactor: Connection을 TransactionSynchronizationManager로 관리한다. * refactor: 와일드카드 제거 * refactor: unbindResource 메서드는 반환값이 존재하지 않는다. * feat: TSM 테스트코드 작성
- Loading branch information
1 parent
48d2d9c
commit 2cc0bf5
Showing
13 changed files
with
156 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/techcourse/service/AppUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.JdbcUserHistoryDao; | ||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import com.techcourse.repository.UserRepository; | ||
|
||
public class AppUserService implements UserService { | ||
|
||
private final UserRepository userRepository; | ||
private final JdbcUserHistoryDao jdbcUserHistoryDao; | ||
|
||
public AppUserService(final UserDao userDao, final JdbcUserHistoryDao jdbcUserHistoryDao) { | ||
this.userRepository = new UserRepository(userDao); | ||
this.jdbcUserHistoryDao = jdbcUserHistoryDao; | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return userRepository.findById(id); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
userRepository.save(user); | ||
} | ||
|
||
@Override | ||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userRepository.update(user); | ||
jdbcUserHistoryDao.log(new UserHistory(user, createBy)); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/techcourse/service/TxUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.domain.User; | ||
import org.springframework.jdbc.transaction.TransactionManager; | ||
|
||
import static com.techcourse.config.DataSourceConfig.getInstance; | ||
|
||
public class TxUserService implements UserService { | ||
|
||
private final UserService userService; | ||
private final TransactionManager transactionManager; | ||
|
||
public TxUserService(UserService userService) { | ||
this.userService = userService; | ||
this.transactionManager = new TransactionManager(getInstance()); | ||
} | ||
|
||
@Override | ||
public User findById(final long id) { | ||
return transactionManager.execute(connection -> userService.findById(id)); | ||
} | ||
|
||
@Override | ||
public void insert(final User user) { | ||
transactionManager.execute(connection -> { | ||
userService.insert(user); | ||
return null; | ||
} | ||
); | ||
} | ||
|
||
@Override | ||
public void changePassword(long id, String newPassword, String createBy) { | ||
transactionManager.execute(connection -> { | ||
userService.changePassword(id, newPassword, createBy); | ||
return null; | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,11 @@ | ||
package com.techcourse.service; | ||
|
||
import com.techcourse.dao.UserDao; | ||
import com.techcourse.dao.JdbcUserHistoryDao; | ||
import com.techcourse.domain.User; | ||
import com.techcourse.domain.UserHistory; | ||
import com.techcourse.repository.UserRepository; | ||
import org.springframework.jdbc.transaction.TransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
public interface UserService { | ||
|
||
public class UserService { | ||
User findById(final long id); | ||
void insert(final User user); | ||
void changePassword(final long id, final String newPassword, final String createBy); | ||
|
||
private final TransactionManager transactionManager; | ||
private final UserRepository userRepository; | ||
private final JdbcUserHistoryDao jdbcUserHistoryDao; | ||
|
||
public UserService(final DataSource dataSource, final UserDao userDao, final JdbcUserHistoryDao jdbcUserHistoryDao) { | ||
this.transactionManager = new TransactionManager(dataSource); | ||
this.userRepository = new UserRepository(userDao); | ||
this.jdbcUserHistoryDao = jdbcUserHistoryDao; | ||
} | ||
|
||
public User findById(final long id) { | ||
return userRepository.findById(id); | ||
} | ||
|
||
public void insert(final User user) { | ||
userRepository.save(user); | ||
} | ||
|
||
public void changePassword(final long id, final String newPassword, final String createBy) { | ||
transactionManager.execute(connection -> { | ||
final var user = findById(id); | ||
user.changePassword(newPassword); | ||
userRepository.update(user); | ||
jdbcUserHistoryDao.log(new UserHistory(user, createBy)); | ||
return null; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.