Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch05] Implemented UserService.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Sep 2, 2017
1 parent 3edfa61 commit c46c3cd
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 14 deletions.
5 changes: 4 additions & 1 deletion sql/tobystudy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ CREATE TABLE `users` (
`id` varchar(10) NOT NULL,
`name` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`level` tinyint(1) NOT NULL,
`login` int(11) NOT NULL,
`recommend` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand All @@ -39,4 +42,4 @@ CREATE TABLE `users` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-07-29 20:32:01
-- Dump completed on 2017-09-02 18:55:32
51 changes: 51 additions & 0 deletions src/main/java/ch01/springbook/user/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ch01.springbook.user;

import java.util.List;

import ch01.springbook.user.dao.UserDao;
import ch01.springbook.user.domain.Level;
import ch01.springbook.user.domain.User;

public class UserService {

UserDao userDao;

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public void upgradeLevels() {

List<User> userList = userDao.getAll();

for (User user : userList) {

Boolean changed = null;

if (user.getLevel() == Level.BASIC && user.getLogin() >= 50) {
user.setLevel(Level.SILVER);
changed = true;

} else if (user.getLevel() == Level.SILVER && user.getRecommend() >= 30) {
user.setLevel(Level.GOLD);
changed = true;
} else if (user.getLevel() == Level.GOLD) {
changed = false;
} else {
changed = false;
}

if (changed) {
userDao.update(user);
}
}
}

public void add(User user) {
if (user.getLevel() == null) {
user.setLevel(Level.BASIC);
}

userDao.add(user);
}
}
6 changes: 4 additions & 2 deletions src/main/java/ch01/springbook/user/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ch01.springbook.user.dao;

import java.util.List;

import ch01.springbook.user.domain.User;

import java.util.List;

public interface UserDao {

void add(User user);
User get(String id);
List<User> getAll();
void deleteAll();
int getCount();
void update(User user);
}
22 changes: 20 additions & 2 deletions src/main/java/ch01/springbook/user/dao/UserDaoJdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

Expand All @@ -13,6 +14,7 @@
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import ch01.springbook.user.domain.Level;
import ch01.springbook.user.domain.User;

public class UserDaoJdbc implements UserDao {
Expand All @@ -26,6 +28,9 @@ public User mapRow(ResultSet resultSet, int i) throws SQLException {
user.setId(resultSet.getString("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setLevel(Level.valueOf(resultSet.getInt("level")));
user.setLogin(resultSet.getInt("login"));
user.setRecommend(resultSet.getInt("recommend"));

return user;
}
Expand All @@ -40,8 +45,9 @@ public void setDataSource(DataSource dataSource) {
}

public void add(User user) {
this.jdbcTemplate.update("INSERT INTO users(id, name, password) VALUES(?, ?, ?)",
user.getId(), user.getName(), user.getPassword());
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());
}

public User get(String id) {
Expand All @@ -51,6 +57,10 @@ public User get(String id) {
);
}

public List<User> getAll() {
return this.jdbcTemplate.query("SELECT * FROM users", this.userMapper);
}

public void deleteAll() {
this.jdbcTemplate.update("delete from users");
}
Expand All @@ -75,4 +85,12 @@ public Integer extractData(ResultSet resultSet) throws SQLException, DataAccessE

// this.jdbcTemplate.queryForInt("select count(*) from users");
}

public void update(User user) {
this.jdbcTemplate.update(
"update users set name = ?, password = ?, level = ?, login = ?, " +
"recommend = ? where id = ? ",
user.getName(), user.getPassword(), user.getLevel().intValue(), user.getLogin(),
user.getRecommend(), user.getId());
}
}
27 changes: 27 additions & 0 deletions src/main/java/ch01/springbook/user/domain/Level.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ch01.springbook.user.domain;

public enum Level {

BASIC(1),
SILVER(2),
GOLD(3);

private final int value;

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

public int intValue() {
return value;
}

public static Level valueOf(int value) {
switch(value) {
case 1: return BASIC;
case 2: return SILVER;
case 3: return GOLD;
default: throw new AssertionError("Unknown value: " + value);
}
}
}
33 changes: 32 additions & 1 deletion src/main/java/ch01/springbook/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ public class User {
String name;
String password;

Level level;
int login;
int recommend;

public User() {

}

public User(String id, String name, String password) {
public User(String id, String name, String password, Level level, int login, int recommend) {
this.id = id;
this.name = name;
this.password = password;
this.level = level;
this.login = login;
this.recommend = recommend;
}

public String getId() {
Expand All @@ -38,4 +45,28 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

public Level getLevel() {
return level;
}

public void setLevel(Level level) {
this.level = level;
}

public int getLogin() {
return login;
}

public void setLogin(int login) {
this.login = login;
}

public int getRecommend() {
return recommend;
}

public void setRecommend(int recommend) {
this.recommend = recommend;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@
<property name="dataSource" ref="dataSource" />
</bean>

<bean id="userService" class="ch01.springbook.user.UserService">
<property name="userDao" ref="userDao" />
</bean>

</beans>
58 changes: 51 additions & 7 deletions src/test/java/ch01/springbook/user/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.util.List;

import javax.sql.DataSource;

import org.junit.Before;
Expand All @@ -16,6 +18,7 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ch01.springbook.user.dao.UserDao;
import ch01.springbook.user.domain.Level;
import ch01.springbook.user.domain.User;

@RunWith(SpringJUnit4ClassRunner.class)
Expand All @@ -37,9 +40,17 @@ public class UserDaoTest {

@Before
public void setUp() {
user1 = new User("test01", "test", "no1");
user2 = new User("test02", "test", "no2");
user3 = new User("test03", "test", "no3");
user1 = new User("test01", "test", "no1", Level.BASIC, 1, 0);
user2 = new User("test02", "test", "no2", Level.SILVER, 55, 10);
user3 = new User("test03", "test", "no3", Level.GOLD, 100, 40);
}

private void checkSameUser(User user1, User user2) {
assertThat(user2.getName(), is(user1.getName()));
assertThat(user2.getPassword(), is(user1.getPassword()));
assertThat(user2.getLevel(), is(user1.getLevel()));
assertThat(user2.getLogin(), is(user1.getLogin()));
assertThat(user2.getRecommend(), is(user1.getRecommend()));
}

@Test
Expand All @@ -53,12 +64,23 @@ public void addAndGet() {
assertThat(userDao.getCount(), is(2));

User userget1 = userDao.get(user1.getId());
assertThat(userget1.getName(), is(user1.getName()));
assertThat(userget1.getPassword(), is(user1.getPassword()));
checkSameUser(userget1, user1);

User userget2 = userDao.get(user2.getId());
assertThat(userget2.getName(), is(user2.getName()));
assertThat(userget2.getPassword(), is(user2.getPassword()));
checkSameUser(userget2, user2);
}

@Test
public void getAll() {

userDao.deleteAll();

userDao.add(user1);
userDao.add(user2);
userDao.add(user3);

List<User> userList = userDao.getAll();
assertThat(userList.size(), is(3));
}

@Test
Expand Down Expand Up @@ -91,4 +113,26 @@ public void duplicateKey() {
userDao.add(user1);
userDao.add(user1);
}

@Test
public void update() {
userDao.deleteAll();

userDao.add(user1);
userDao.add(user2);

user1.setName("test011");
user1.setPassword("testmod");
user1.setLevel(Level.GOLD);
user1.setLogin(1000);
user1.setRecommend(999);

userDao.update(user1);

User userget1 = userDao.get(user1.getId());
checkSameUser(user1, userget1);

User userget2 = userDao.get(user2.getId());
checkSameUser(user2, userget2);
}
}

0 comments on commit c46c3cd

Please sign in to comment.