(139) - User Overview/ user add/archive

mongodb
Li Shengzhao 2016-03-25 17:09:10 +08:00
parent 76882ed1da
commit c4e12d10b5
7 changed files with 102 additions and 40 deletions

View File

@ -17,7 +17,6 @@ import com.monkeyk.sos.domain.user.User;
import java.io.Serializable; import java.io.Serializable;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
/** /**
@ -102,4 +101,12 @@ public class UserDto implements Serializable {
public void setPrivileges(List<Privilege> privileges) { public void setPrivileges(List<Privilege> privileges) {
this.privileges = privileges; this.privileges = privileges;
} }
public static List<UserDto> toDtos(List<User> users) {
List<UserDto> dtos = new ArrayList<>(users.size());
for (User user : users) {
dtos.add(new UserDto(user));
}
return dtos;
}
} }

View File

@ -1,14 +1,3 @@
/*
* Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
* www.monkeyk.com
* All rights reserved.
*
* This software is the confidential and proprietary information of
* MONKEYK Information Technology Co. Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with MONKEYK Information Technology Co. Ltd.
*/
package com.monkeyk.sos.domain.dto; package com.monkeyk.sos.domain.dto;
import java.io.Serializable; import java.io.Serializable;
@ -33,6 +22,9 @@ public class UserOverviewDto implements Serializable {
public UserOverviewDto() { public UserOverviewDto() {
} }
public int getSize() {
return userDtos.size();
}
public String getUsername() { public String getUsername() {
return username; return username;

View File

@ -2,6 +2,8 @@ package com.monkeyk.sos.domain.user;
import com.monkeyk.sos.domain.shared.Repository; import com.monkeyk.sos.domain.shared.Repository;
import java.util.List;
/** /**
* @author Shengzhao Li * @author Shengzhao Li
*/ */
@ -16,4 +18,5 @@ public interface UserRepository extends Repository {
User findByUsername(String username); User findByUsername(String username);
List<User> findUsersByUsername(String username);
} }

View File

@ -14,17 +14,16 @@ package com.monkeyk.sos.infrastructure.jdbc;
import com.monkeyk.sos.domain.user.Privilege; import com.monkeyk.sos.domain.user.Privilege;
import com.monkeyk.sos.domain.user.User; import com.monkeyk.sos.domain.user.User;
import com.monkeyk.sos.domain.user.UserRepository; import com.monkeyk.sos.domain.user.UserRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
* 2015/11/16 * 2015/11/16
@ -59,9 +58,7 @@ public class UserRepositoryJdbc implements UserRepository {
final List<String> strings = this.jdbcTemplate.queryForList(sql, new Object[]{userId}, String.class); final List<String> strings = this.jdbcTemplate.queryForList(sql, new Object[]{userId}, String.class);
List<Privilege> privileges = new ArrayList<>(strings.size()); List<Privilege> privileges = new ArrayList<>(strings.size());
for (String string : strings) { privileges.addAll(strings.stream().map(Privilege::valueOf).collect(Collectors.toList()));
privileges.add(Privilege.valueOf(string));
}
return privileges; return privileges;
} }
@ -69,10 +66,7 @@ public class UserRepositoryJdbc implements UserRepository {
public void saveUser(final User user) { public void saveUser(final User user) {
final String sql = " insert into user_(guid,archived,create_time,email,password,username,phone) " + final String sql = " insert into user_(guid,archived,create_time,email,password,username,phone) " +
" values (?,?,?,?,?,?,?) "; " values (?,?,?,?,?,?,?) ";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() { this.jdbcTemplate.update(sql, ps -> {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, user.guid()); ps.setString(1, user.guid());
ps.setBoolean(2, user.archived()); ps.setBoolean(2, user.archived());
@ -83,16 +77,13 @@ public class UserRepositoryJdbc implements UserRepository {
ps.setString(6, user.username()); ps.setString(6, user.username());
ps.setString(7, user.phone()); ps.setString(7, user.phone());
}
}); });
} }
@Override @Override
public void updateUser(final User user) { public void updateUser(final User user) {
final String sql = " update user_ set username = ?, password = ?, phone = ?,email = ? where guid = ? "; final String sql = " update user_ set username = ?, password = ?, phone = ?,email = ? where guid = ? ";
this.jdbcTemplate.update(sql, new PreparedStatementSetter() { this.jdbcTemplate.update(sql, ps -> {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, user.username()); ps.setString(1, user.username());
ps.setString(2, user.password()); ps.setString(2, user.password());
@ -100,7 +91,6 @@ public class UserRepositoryJdbc implements UserRepository {
ps.setString(4, user.email()); ps.setString(4, user.email());
ps.setString(5, user.guid()); ps.setString(5, user.guid());
}
}); });
} }
@ -117,4 +107,21 @@ public class UserRepositoryJdbc implements UserRepository {
return user; return user;
} }
@Override
public List<User> findUsersByUsername(String username) {
String sql = " select * from user_ where archived = 0 ";
Object[] params = new Object[]{};
if (StringUtils.isNotEmpty(username)) {
sql += " and username like ?";
params = new Object[]{"%" + username + "%"};
}
sql += " order by create_time desc ";
final List<User> list = this.jdbcTemplate.query(sql, params, userRowMapper);
for (User user : list) {
user.privileges().addAll(findPrivileges(user.id()));
}
return list;
}
} }

View File

@ -1,5 +1,6 @@
package com.monkeyk.sos.service.impl; package com.monkeyk.sos.service.impl;
import com.monkeyk.sos.domain.dto.UserDto;
import com.monkeyk.sos.domain.dto.UserJsonDto; import com.monkeyk.sos.domain.dto.UserJsonDto;
import com.monkeyk.sos.domain.dto.UserOverviewDto; import com.monkeyk.sos.domain.dto.UserOverviewDto;
import com.monkeyk.sos.domain.shared.security.WdcyUserDetails; import com.monkeyk.sos.domain.shared.security.WdcyUserDetails;
@ -16,6 +17,7 @@ import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collection; import java.util.Collection;
import java.util.List;
/** /**
* @author Shengzhao Li * @author Shengzhao Li
@ -52,7 +54,8 @@ public class UserServiceImpl implements UserService {
@Override @Override
public UserOverviewDto loadUserOverviewDto(UserOverviewDto overviewDto) { public UserOverviewDto loadUserOverviewDto(UserOverviewDto overviewDto) {
List<User> users = userRepository.findUsersByUsername(overviewDto.getUsername());
overviewDto.setUserDtos(UserDto.toDtos(users));
return overviewDto; return overviewDto;
} }

View File

@ -1,3 +1,4 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- <%--
* *
* @author Shengzhao Li * @author Shengzhao Li
@ -13,5 +14,37 @@
<a href="../">Home</a> <a href="../">Home</a>
<h2>User Overview</h2> <h2>User Overview</h2>
<form action="" class="form-inline">
<div class="form-group">
<input type="text" class="form-control" placeholder="Type username" name="username"
value="${overviewDto.username}"/>
</div>
<button type="submit" class="btn btn-default">Search</button>
&nbsp;<span class="text-info">Total: ${overviewDto.size}</span>
</form>
<br/>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Username</th>
<th>Privilege</th>
<th>Phone</th>
<th>Email</th>
<th>CreateTime</th>
</tr>
</thead>
<tbody>
<c:forEach items="${overviewDto.userDtos}" var="user">
<tr>
<td>${user.username}</td>
<td>${user.privileges}</td>
<td>${user.phone}</td>
<td>${user.email}</td>
<td>${user.createTime}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body> </body>
</html> </html>

View File

@ -17,6 +17,8 @@ import com.monkeyk.sos.infrastructure.AbstractRepositoryTest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.List;
import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull; import static org.testng.AssertJUnit.assertNotNull;
import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertNull;
@ -46,6 +48,21 @@ public class UserRepositoryJdbcTest extends AbstractRepositoryTest {
} }
@Test
public void findUsersByUsername() {
User user = userRepository.findByGuid("oood");
assertNull(user);
user = new User("user", "123", "123", "ewo@honyee.cc");
userRepository.saveUser(user);
final List<User> list = userRepository.findUsersByUsername(user.username());
assertNotNull(list);
assertEquals(list.size(), 1);
}
@Test @Test
public void updateUser() { public void updateUser() {