update user info

This commit is contained in:
aisensiy 2017-08-14 14:27:36 +08:00
parent f81a048f14
commit b1e8632c3b
12 changed files with 197 additions and 25 deletions

View File

@ -1,28 +1,68 @@
package io.spring.api; package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.application.user.UserQueryService; import io.spring.application.user.UserQueryService;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController @RestController
@RequestMapping(path = "/user")
public class CurrentUserApi { public class CurrentUserApi {
private UserQueryService userQueryService; private UserQueryService userQueryService;
private UserRepository userRepository;
@Autowired @Autowired
public CurrentUserApi(UserQueryService userQueryService) { public CurrentUserApi(UserQueryService userQueryService, UserRepository userRepository) {
this.userQueryService = userQueryService; this.userQueryService = userQueryService;
this.userRepository = userRepository;
} }
@RequestMapping(path = "/user", method = RequestMethod.GET) @GetMapping
public ResponseEntity currentUser(@AuthenticationPrincipal User currentUser, public ResponseEntity currentUser(@AuthenticationPrincipal User currentUser,
@RequestHeader(value = "Authorization") String authorization) { @RequestHeader(value = "Authorization") String authorization) {
return ResponseEntity.ok(userQueryService.fetchCurrentUser(currentUser.getUsername(), authorization.split(" ")[1])); return ResponseEntity.ok(userQueryService.fetchCurrentUser(currentUser.getUsername(), authorization.split(" ")[1]));
} }
@PutMapping
public ResponseEntity updateProfile(@AuthenticationPrincipal User currentUser,
@RequestHeader(value = "Authorization") String authorization,
@Valid @RequestBody UpdateUserParam updateUserParam,
BindingResult bindingResult) {
currentUser.update(
updateUserParam.getEmail(),
updateUserParam.getUsername(),
updateUserParam.getPassword(),
updateUserParam.getBio(),
updateUserParam.getImage());
userRepository.save(currentUser);
return ResponseEntity.ok(userQueryService.fetchCurrentUser(currentUser.getUsername(), authorization.split(" ")[1]));
}
}
@Getter
@JsonRootName("user")
@NoArgsConstructor
class UpdateUserParam {
@Email(message = "should be an email")
private String email = "";
private String password = "";
private String username = "";
private String bio = "";
private String image = "";
} }

View File

@ -14,8 +14,9 @@ import javax.persistence.Id;
@Entity @Entity
@JsonRootName("user") @JsonRootName("user")
public class UserData { public class UserData {
private String email;
@Id @Id
private String id;
private String email;
private String username; private String username;
private String bio; private String bio;
private String image; private String image;

View File

@ -5,10 +5,13 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.UUID;
@Getter @Getter
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(of = {"username"}) @EqualsAndHashCode(of = {"username"})
public class User { public class User {
private String id;
private String email; private String email;
private String username; private String username;
private String password; private String password;
@ -16,10 +19,33 @@ public class User {
private String image; private String image;
public User(String email, String username, String password, String bio, String image) { public User(String email, String username, String password, String bio, String image) {
this.id = UUID.randomUUID().toString();
this.email = email; this.email = email;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.bio = bio; this.bio = bio;
this.image = image; this.image = image;
} }
public void update(String email, String username, String password, String bio, String image) {
if (!email.equals("")) {
this.email = email;
}
if (!username.equals("")) {
this.username = username;
}
if (!password.equals("")) {
this.password = password;
}
if (!bio.equals("")) {
this.bio = bio;
}
if (!image.equals("")) {
this.image = image;
}
}
} }

View File

@ -8,6 +8,8 @@ import java.util.Optional;
public interface UserRepository { public interface UserRepository {
void save(User user); void save(User user);
Optional<User> findById(String id);
Optional<User> findByUsername(String username); Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email); Optional<User> findByEmail(String email);

View File

@ -18,7 +18,16 @@ public class MyBatisUserRepository implements UserRepository {
@Override @Override
public void save(User user) { public void save(User user) {
userMapper.save(user); if (userMapper.findById(user.getId()) == null) {
userMapper.insert(user);
} else {
userMapper.update(user);
}
}
@Override
public Optional<User> findById(String id) {
return Optional.ofNullable(userMapper.findById(id));
} }
@Override @Override

View File

@ -8,8 +8,12 @@ import org.springframework.stereotype.Component;
@Component @Component
@Mapper @Mapper
public interface UserMapper { public interface UserMapper {
void save(@Param("user") User user); void insert(@Param("user") User user);
User findByUsername(@Param("username") String username); User findByUsername(@Param("username") String username);
User findByEmail(@Param("email") String email); User findByEmail(@Param("email") String email);
User findById(@Param("id") String id);
void update(@Param("user") User user);
} }

View File

@ -1,5 +1,6 @@
create table users ( create table users (
username varchar(255) primary key, id varchar(255) primary key,
username varchar(255) UNIQUE,
password varchar(255), password varchar(255),
email varchar(255) UNIQUE, email varchar(255) UNIQUE,
bio text, bio text,

View File

@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.spring.infrastructure.user.UserMapper"> <mapper namespace="io.spring.infrastructure.user.UserMapper">
<insert id="save"> <insert id="insert">
insert into users (username, email, password, bio, image) values( insert into users (id, username, email, password, bio, image) values(
#{user.id},
#{user.username}, #{user.username},
#{user.email}, #{user.email},
#{user.password}, #{user.password},
@ -10,15 +11,30 @@
#{user.image} #{user.image}
) )
</insert> </insert>
<update id="update">
update users
<set>
<if test="user.username != ''">username = #{user.username},</if>
<if test="user.email != ''">email = #{user.email},</if>
<if test="user.password != ''">password = #{user.password},</if>
<if test="user.bio != ''">bio = #{user.bio},</if>
<if test="user.image != ''">image = #{user.image}</if>
</set>
where id = #{user.id}
</update>
<select id="findByUsername" resultMap="user"> <select id="findByUsername" resultMap="user">
select * from users where username = #{username} select * from users where username = #{username}
</select> </select>
<select id="findByEmail" resultMap="user"> <select id="findByEmail" resultMap="user">
select username, email, password, bio, image from users where email = #{email} select id, username, email, password, bio, image from users where email = #{email}
</select>
<select id="findById" resultMap="user">
select id, username, email, password, bio, image from users where id = #{id}
</select> </select>
<resultMap id="user" type="io.spring.core.user.User" > <resultMap id="user" type="io.spring.core.user.User" >
<id column="username" property="username"/> <id column="id" property="id"/>
<result column="username" property="username"/>
<result column="email" property="email"/> <result column="email" property="email"/>
<result column="password" property="password"/> <result column="password" property="password"/>
<result column="bio" property="bio"/> <result column="bio" property="bio"/>

View File

@ -6,7 +6,6 @@ import io.spring.application.user.UserData;
import io.spring.application.user.UserReadService; import io.spring.application.user.UserReadService;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.core.user.UserRepository; import io.spring.core.user.UserRepository;
import io.spring.infrastructure.service.DefaultJwtService;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -16,9 +15,13 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@ -38,24 +41,32 @@ public class CurrentUserApiTest {
@Autowired @Autowired
private JwtService jwtService; private JwtService jwtService;
private User user;
private UserData userData;
private String token;
private String email;
private String username;
private String defaultAvatar;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
RestAssured.port = port; RestAssured.port = port;
email = "john@jacob.com";
username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
user = new User(email, username, "123", "", defaultAvatar);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userReadService.findOne(eq(username))).thenReturn(userData);
token = jwtService.toToken(userData);
} }
@Test @Test
public void should_get_current_user_with_token() throws Exception { public void should_get_current_user_with_token() throws Exception {
String email = "john@jacob.com";
String username = "johnjacob";
User user = new User(email, username, "123", "", "https://static.productionready.io/images/smiley-cyrus.jpg");
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.of(user));
UserData userData = new UserData(email, username, "", "https://static.productionready.io/images/smiley-cyrus.jpg");
when(userReadService.findOne(eq(username))).thenReturn(userData);
String token = jwtService.toToken(userData);
given() given()
.header("Authorization", "Token " + token) .header("Authorization", "Token " + token)
@ -92,4 +103,42 @@ public class CurrentUserApiTest {
.then() .then()
.statusCode(401); .statusCode(401);
} }
@Test
public void should_update_current_user_profile() throws Exception {
String newEmail = "newemail@example.com";
String newBio = "updated";
Map<String, Object> param = new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>() {{
put("email", newEmail);
put("bio", newBio);
}});
}};
given()
.contentType("application/json")
.header("Authorization", "Token " + token)
.body(param)
.when()
.put("/user")
.then()
.statusCode(200);
assertThat(user.getEmail(), is(newEmail));
assertThat(user.getBio(), is(newBio));
assertThat(user.getImage(), is(defaultAvatar));
}
@Test
public void should_get_401_if_not_login() throws Exception {
given()
.contentType("application/json")
.body(new HashMap<String, Object>() {{
put("user", new HashMap<String, Object>());
}})
.when()
.put("/user")
.then().statusCode(401);
}
} }

View File

@ -54,7 +54,7 @@ public class UsersApiTest {
String username = "johnjacob"; String username = "johnjacob";
when(jwtService.toToken(any())).thenReturn("123"); when(jwtService.toToken(any())).thenReturn("123");
UserData userData = new UserData(email, username, "", defaultAvatar); UserData userData = new UserData("123", email, username, "", defaultAvatar);
when(userReadService.findOne(eq(username))).thenReturn(userData); when(userReadService.findOne(eq(username))).thenReturn(userData);
when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty()); when(userRepository.findByUsername(eq(username))).thenReturn(Optional.empty());
@ -175,7 +175,7 @@ public class UsersApiTest {
String password = "123"; String password = "123";
User user = new User(email, username, password, "", defaultAvatar); User user = new User(email, username, password, "", defaultAvatar);
UserData userData = new UserData(email, username, "", defaultAvatar); UserData userData = new UserData("123", email, username, "", defaultAvatar);
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user)); when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userReadService.findOne(eq(username))).thenReturn(userData); when(userReadService.findOne(eq(username))).thenReturn(userData);
@ -209,7 +209,7 @@ public class UsersApiTest {
String password = "123"; String password = "123";
User user = new User(email, username, password, "", defaultAvatar); User user = new User(email, username, password, "", defaultAvatar);
UserData userData = new UserData(email, username, "", defaultAvatar); UserData userData = new UserData(user.getId(), email, username, "", defaultAvatar);
when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user)); when(userRepository.findByEmail(eq(email))).thenReturn(Optional.of(user));
when(userReadService.findOne(eq(username))).thenReturn(userData); when(userReadService.findOne(eq(username))).thenReturn(userData);

View File

@ -24,7 +24,7 @@ public class DefaultJwtServiceTest {
public void should_generate_and_parse_token() throws Exception { public void should_generate_and_parse_token() throws Exception {
String username = "aisensiy"; String username = "aisensiy";
UserData userData = new UserData("aisensiy@163.com", username, "", ""); UserData userData = new UserData("123", "aisensiy@163.com", username, "", "");
String token = jwtService.toToken(userData); String token = jwtService.toToken(userData);
assertThat(token, notNullValue()); assertThat(token, notNullValue());
Optional<String> optional = jwtService.getSubFromToken(token); Optional<String> optional = jwtService.getSubFromToken(token);

View File

@ -2,6 +2,7 @@ package io.spring.infrastructure.user;
import io.spring.core.user.User; import io.spring.core.user.User;
import io.spring.core.user.UserRepository; import io.spring.core.user.UserRepository;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest; import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
@ -20,14 +21,37 @@ import static org.junit.Assert.*;
public class MyBatisUserRepositoryTest { public class MyBatisUserRepositoryTest {
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
private User user;
@Before
public void setUp() throws Exception {
user = new User("aisensiy@163.com", "aisensiy", "123", "", "default");
}
@Test @Test
public void should_save_and_fetch_user_success() throws Exception { public void should_save_and_fetch_user_success() throws Exception {
User user = new User("aisensiy@163.com", "aisensiy", "123", "", "default");
userRepository.save(user); userRepository.save(user);
Optional<User> userOptional = userRepository.findByUsername("aisensiy"); Optional<User> userOptional = userRepository.findByUsername("aisensiy");
assertThat(userOptional.get(), is(user)); assertThat(userOptional.get(), is(user));
Optional<User> userOptional2 = userRepository.findByEmail("aisensiy@163.com"); Optional<User> userOptional2 = userRepository.findByEmail("aisensiy@163.com");
assertThat(userOptional2.get(), is(user)); assertThat(userOptional2.get(), is(user));
} }
@Test
public void should_update_user_success() throws Exception {
String newEmail = "newemail@email.com";
user.update(newEmail, "", "", "", "");
userRepository.save(user);
Optional<User> optional = userRepository.findByUsername(user.getUsername());
assertThat(optional.isPresent(), is(true));
assertThat(optional.get().getEmail(), is(newEmail));
String newUsername = "newUsername";
user.update("", newUsername, "", "", "");
userRepository.save(user);
optional = userRepository.findByEmail(user.getEmail());
assertThat(optional.isPresent(), is(true));
assertThat(optional.get().getUsername(), is(newUsername));
assertThat(optional.get().getImage(), is(user.getImage()));
}
} }