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;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.application.user.UserQueryService;
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.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping(path = "/user")
public class CurrentUserApi {
private UserQueryService userQueryService;
private UserRepository userRepository;
@Autowired
public CurrentUserApi(UserQueryService userQueryService) {
public CurrentUserApi(UserQueryService userQueryService, UserRepository userRepository) {
this.userQueryService = userQueryService;
this.userRepository = userRepository;
}
@RequestMapping(path = "/user", method = RequestMethod.GET)
@GetMapping
public ResponseEntity currentUser(@AuthenticationPrincipal User currentUser,
@RequestHeader(value = "Authorization") String authorization) {
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
@JsonRootName("user")
public class UserData {
private String email;
@Id
private String id;
private String email;
private String username;
private String bio;
private String image;

View File

@@ -5,10 +5,13 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.UUID;
@Getter
@NoArgsConstructor
@EqualsAndHashCode(of = {"username"})
public class User {
private String id;
private String email;
private String username;
private String password;
@@ -16,10 +19,33 @@ public class User {
private String image;
public User(String email, String username, String password, String bio, String image) {
this.id = UUID.randomUUID().toString();
this.email = email;
this.username = username;
this.password = password;
this.bio = bio;
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 {
void save(User user);
Optional<User> findById(String id);
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);

View File

@@ -18,7 +18,16 @@ public class MyBatisUserRepository implements UserRepository {
@Override
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

View File

@@ -8,8 +8,12 @@ import org.springframework.stereotype.Component;
@Component
@Mapper
public interface UserMapper {
void save(@Param("user") User user);
void insert(@Param("user") User user);
User findByUsername(@Param("username") String username);
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 (
username varchar(255) primary key,
id varchar(255) primary key,
username varchar(255) UNIQUE,
password varchar(255),
email varchar(255) UNIQUE,
bio text,

View File

@@ -1,8 +1,9 @@
<?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" >
<mapper namespace="io.spring.infrastructure.user.UserMapper">
<insert id="save">
insert into users (username, email, password, bio, image) values(
<insert id="insert">
insert into users (id, username, email, password, bio, image) values(
#{user.id},
#{user.username},
#{user.email},
#{user.password},
@@ -10,15 +11,30 @@
#{user.image}
)
</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 * from users where username = #{username}
</select>
<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>
<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="password" property="password"/>
<result column="bio" property="bio"/>