Skip to content

Commit

Permalink
#4 서비스 추상화
Browse files Browse the repository at this point in the history
  • Loading branch information
mtak0235 committed Jul 24, 2023
1 parent 19bb9c5 commit 72aefa2
Show file tree
Hide file tree
Showing 130 changed files with 7,594 additions and 456 deletions.
1,554 changes: 1,554 additions & 0 deletions spring/tobby/1_스프링의_이해와_원리/1_오브젝트와 의존 관계.md

Large diffs are not rendered by default.

731 changes: 731 additions & 0 deletions spring/tobby/1_스프링의_이해와_원리/2_테스트.md

Large diffs are not rendered by default.

1,594 changes: 1,594 additions & 0 deletions spring/tobby/1_스프링의_이해와_원리/3_템플릿.md

Large diffs are not rendered by default.

536 changes: 536 additions & 0 deletions spring/tobby/1_스프링의_이해와_원리/4_예외.md

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions spring/tobby/1_스프링의_이해와_원리/5_서비스_추상화.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
지금까지 만든 DAO 에 transaction 을 적용하면서 스프링이 어떻게 성격이 비슷한 여러 종류의 기술을 추상화하고 이를 일관된 방법으로 사용할 수 있도록 지원하는지를 살펴보자.

# 1. 사용자 레벨 관리 기능 추가

현재 UserDao는 CRUD만 가능하다. 여기에 사용자 관리 기능을 추가해보자
만들 비즈니스 로직

* 사용자의 레벨은 BASIC, SILVER, GOLD 세 가지 중 하나다.
* 사용자가 처음 가입하면 BASIC 레벨이 되며, 이후 활동에 따라서 한 단계씩 업그레이드될 수 있다.
* 가입 후 50회 이상 로그인을 하면 BASIC에서 SILVER 레벨이 된다.
* SILVER 레벨이면서 30번 이상 추천을 받으면 GOLD 레벨이 된다.
* 사용자 레벨의 변경 작업은 일정한 주기를 가지고 일괄적으로 진행된다
변경 작업 전에는 조 건을 충족하더라도 레벨의 변경이 일어나지 않는다

😎조건에 따라 사용자의 레벨을 주기적으로 변경한다는 내용이로군

## 1.1 필드 추가

### Level enum

User class에 사용자의 레벨을 저장할 필드를 추가하자 .

🤔varchar 타입으로 할까? 별론데.. 각 레벨을 코드화 해서 숫자로 넣을까?

```java
class User {
private static final int BASIC = 1;
private static final int SILVER = 2;
private static final int GOLD = 3;
int level;
public void setLevel(int level) {
this.level = level;
}
```

```java
if (user1.getLevel() = = User.BASIC) {
user1.setLevel(User.SILVER);
}
```

🤔level의 타입이 int라서 다른 종류의 정보를 넣거나 없는 번호를 넣어도 컴파일러가 모를 것 같다.

``` java
user1.setLevel(other.getSum());
user1.setLevel(1000);
```

😎java5 ~ 의 enum을 사용하자

```java
package com.example.tobby.user.dao;

public enum Level {
BASIC(1), SILVER(2), GOLD(3);
private final int value;

public int getValue() {
return value;
}

Level(int value) {
this.value = value;
}

public static Level valueOf(int value) {
return switch (value) {
case 1 -> BASIC;
case 2 -> SILVER;
case 3 -> GOLD;
default -> throw new AssertionError("Unknown value: " + value);
};
}
}

```

### User 필드 추가

Level typed 변수, 로그인 횟수, 추천수까지 User class에 추가하자.

### UserDaoTest 수정

```java
@BeforeEach
private void setUp() {
SingleConnectionDataSource dataSource = new SingleConnectionDataSource("jdbc:mysql://localhost/tobby", "tobaby", "0000", true);
// dao.setDataSource(dataSource);
// dao = context.getBean("userDao", UserDao.class);
user3 = new User("mtak", "탁민경", "0000", Level.BASIC, 1, 0);
user1 = new User("yeji", "탁예지", "0000", Level.SILVER, 55, 10);
user2 = new User("doyun", "탁도윤", "000", Level.GOLD, 100, 40);

}

```


```java
private void checkSameUser(User user, User user1) {
assertThat(user.getId()).isEqualTo(user1.getId());
assertThat(user.getPassword()).isEqualTo(user1.getPassword());
assertThat(user.getName()).isEqualTo(user1.getName());
assertThat(user.getLevel()).isEqualTo(user1.getLevel());
assertThat(user.getLogin()).isEqualTo(user1.getLogin());
assertThat(user.getRecommend()).isEqualTo(user1.getRecommend());
}
```

### UserDaoJdbc 수정

```java
public void add(User user) {
this.jdbcTemplate.update("insert into users (id, name, password, level, login, recommend) values(?,?,?, ?, ?, ?)", user.getId(), user.getName(), user.getPassword(), user.getLevel().intValue(), user.getLogin(), user.getRecommend());

}
```

```java
public class UserDaoJdbc implements UserDao {

private final RowMapper<User> userMapper = new RowMapper<>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setLevel(Level.valueOf(rs.getString("level")));
user.setLogin(rs.getInt("login"));
user.setRecommend(rs.getInt("recommend"));
return user;
}
};
...
}
```

## 1.2 사용자 수정 기능 추가

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Wed Jul 12 13:14:30 KST 2023
gradle.version=8.1.1
Binary file not shown.
Binary file added spring/tobby/tobby/.gradle/file-system.probe
Binary file not shown.
Empty file.
8 changes: 8 additions & 0 deletions spring/tobby/tobby/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions spring/tobby/tobby/.idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spring/tobby/tobby/.idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions spring/tobby/tobby/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions spring/tobby/tobby/.idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions spring/tobby/tobby/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions spring/tobby/tobby/.idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions spring/tobby/tobby/.idea/jpa-buddy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions spring/tobby/tobby/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spring/tobby/tobby/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions spring/tobby/tobby/.idea/modules/tobby.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions spring/tobby/tobby/.idea/modules/tobby.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72aefa2

Please sign in to comment.