update user info
This commit is contained in:
@@ -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 = "";
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user