From 9bbbf9660b0e2026d61ba1e7c44e17c1b1f28147 Mon Sep 17 00:00:00 2001 From: sehajyang Date: Mon, 19 Oct 2020 19:46:19 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20#4=20User=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20API=20=EC=9E=91=EC=84=B1=20=EB=B0=8F=20H2?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * findAll, create, validation 작성 * H2 config 작성 * UserDTO 작성 --- gradle/wrapper/gradle-wrapper.properties | 5 ++- .../oauth/controller/SocialController.java | 4 +- .../oauth/controller/UserController.java | 37 ++++++++++++++++++ .../resumers/server/oauth/enums/RoleType.java | 1 - .../server/oauth/model/dto/UserDto.java | 30 ++++++++++++++ .../server/oauth/model/entity/Penalty.java | 1 - .../server/oauth/model/entity/User.java | 13 ++++++- .../oauth/repository/UserRepository.java | 5 +++ .../server/oauth/service/UserService.java | 39 +++++++++++++++++++ 9 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/resumers/server/oauth/controller/UserController.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 12d38de..2630c7e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Oct 15 19:57:46 KST 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/src/main/java/com/resumers/server/oauth/controller/SocialController.java b/src/main/java/com/resumers/server/oauth/controller/SocialController.java index 16e4cf3..59730f2 100644 --- a/src/main/java/com/resumers/server/oauth/controller/SocialController.java +++ b/src/main/java/com/resumers/server/oauth/controller/SocialController.java @@ -1,7 +1,7 @@ package com.resumers.server.oauth.controller; -import com.resumers.oauth.service.SocialService; -import com.resumers.oauth.service.UserService; +import com.resumers.server.oauth.service.SocialService; +import com.resumers.server.oauth.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/com/resumers/server/oauth/controller/UserController.java b/src/main/java/com/resumers/server/oauth/controller/UserController.java new file mode 100644 index 0000000..fa79736 --- /dev/null +++ b/src/main/java/com/resumers/server/oauth/controller/UserController.java @@ -0,0 +1,37 @@ +package com.resumers.server.oauth.controller; + +import com.resumers.server.oauth.model.dto.UserDto; +import com.resumers.server.oauth.model.entity.User; +import com.resumers.server.oauth.service.UserService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Created by sehajyang + * DateTime : 2020/10/19 + * Descrpition : + */ + +@Slf4j +@Controller +@RestController +@RequiredArgsConstructor +@RequestMapping("/users") +public class UserController { + private final UserService userService; + + // TODO : Response 객체 작성 + @GetMapping + public List findAll(UserDto.SearchQuery userDto){ + return userService.findAll(); + } + + @PostMapping + public void save(UserDto.InsertUser user) { + userService.create(user); + } +} diff --git a/src/main/java/com/resumers/server/oauth/enums/RoleType.java b/src/main/java/com/resumers/server/oauth/enums/RoleType.java index 980e338..1c0245d 100644 --- a/src/main/java/com/resumers/server/oauth/enums/RoleType.java +++ b/src/main/java/com/resumers/server/oauth/enums/RoleType.java @@ -3,7 +3,6 @@ /** * Created by sehajyang * DateTime : 2020/10/09 - * Descrpition : */ public enum RoleType { USER, ADMIN diff --git a/src/main/java/com/resumers/server/oauth/model/dto/UserDto.java b/src/main/java/com/resumers/server/oauth/model/dto/UserDto.java index 357ec73..fb1d44b 100644 --- a/src/main/java/com/resumers/server/oauth/model/dto/UserDto.java +++ b/src/main/java/com/resumers/server/oauth/model/dto/UserDto.java @@ -1,8 +1,38 @@ package com.resumers.server.oauth.model.dto; +import com.resumers.server.oauth.model.entity.User; +import lombok.Getter; +import lombok.NoArgsConstructor; + /** * Created by sehajyang * DateTime : 2020/10/03 */ + +@Getter +@NoArgsConstructor public class UserDto { + private Long userId; + private String email; + private String nickname; + private String description; + + public UserDto(User user) { + this.userId = user.getUserId(); + this.email = user.getEmail(); + this.nickname = user.getNickname(); + this.description = user.getDescription(); + } + + @Getter + public class SearchQuery extends UserDto{ + public SearchQuery(){ + super(); + } + } + + @Getter + @NoArgsConstructor + public class InsertUser extends UserDto { + } } diff --git a/src/main/java/com/resumers/server/oauth/model/entity/Penalty.java b/src/main/java/com/resumers/server/oauth/model/entity/Penalty.java index 1ff45c8..4c36008 100644 --- a/src/main/java/com/resumers/server/oauth/model/entity/Penalty.java +++ b/src/main/java/com/resumers/server/oauth/model/entity/Penalty.java @@ -5,7 +5,6 @@ /** * Created by sehajyang * DateTime : 2020/10/09 - * Descrpition : */ @Entity diff --git a/src/main/java/com/resumers/server/oauth/model/entity/User.java b/src/main/java/com/resumers/server/oauth/model/entity/User.java index fbdf59f..8441ddd 100644 --- a/src/main/java/com/resumers/server/oauth/model/entity/User.java +++ b/src/main/java/com/resumers/server/oauth/model/entity/User.java @@ -1,6 +1,9 @@ package com.resumers.server.oauth.model.entity; import com.resumers.server.oauth.enums.UserStatusType; +import com.resumers.server.oauth.model.dto.UserDto; +import lombok.Getter; +import lombok.NoArgsConstructor; import javax.persistence.*; import java.util.LinkedHashSet; @@ -11,6 +14,8 @@ * DateTime : 2020/10/03 */ +@Getter +@NoArgsConstructor @Entity @Table(name = "user") public class User extends BaseEntity { @@ -24,7 +29,8 @@ public class User extends BaseEntity { private String password; private String nickname; - @OneToOne(mappedBy = "user") + @OneToOne + @JoinColumn(name = "file_id") private File file; @Column(length = 1000) @@ -65,4 +71,9 @@ public class User extends BaseEntity { joinColumns = @JoinColumn(name ="user_id"), inverseJoinColumns= @JoinColumn(name="role_id")) private Set roles = new LinkedHashSet<>(); + public User(UserDto userDto) { + this.email = userDto.getEmail(); + this.nickname = userDto.getNickname(); + this.description = userDto.getDescription(); + } } diff --git a/src/main/java/com/resumers/server/oauth/repository/UserRepository.java b/src/main/java/com/resumers/server/oauth/repository/UserRepository.java index 05b0a08..bb5a3c3 100644 --- a/src/main/java/com/resumers/server/oauth/repository/UserRepository.java +++ b/src/main/java/com/resumers/server/oauth/repository/UserRepository.java @@ -3,9 +3,14 @@ import com.resumers.server.oauth.model.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + /** * Created by sehajyang * DateTime : 2020/10/03 */ public interface UserRepository extends JpaRepository { + List findAll(); + Optional findAllByEmailOrNickname(String email, String nickname); } diff --git a/src/main/java/com/resumers/server/oauth/service/UserService.java b/src/main/java/com/resumers/server/oauth/service/UserService.java index eaed7c2..6006eb3 100644 --- a/src/main/java/com/resumers/server/oauth/service/UserService.java +++ b/src/main/java/com/resumers/server/oauth/service/UserService.java @@ -1,12 +1,51 @@ package com.resumers.server.oauth.service; +import com.common.Utils; +import com.resumers.server.oauth.model.dto.UserDto; +import com.resumers.server.oauth.model.entity.User; +import com.resumers.server.oauth.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityExistsException; +import java.util.List; +import java.util.stream.Collectors; /** * Created by sehajyang * DateTime : 2020/10/03 */ +@Slf4j @Service +@RequiredArgsConstructor public class UserService { + private final UserRepository userRepository; + + public List findAll() { + List users = userRepository.findAll(); + return users.stream() + .map(UserDto::new) + .collect(Collectors.toList()); + } + + @Transactional + public void create(UserDto userDto) { + if (Utils.isNotEmpty(userDto)) { + validation(userDto.getEmail(), userDto.getNickname()); + User user = new User(userDto); + userRepository.save(user); + } + } + + // TODO : CustomException 만들어서 log error 라인 수정 + private void validation(String email, String nickname) { + if (Utils.isEmpty(email) || Utils.isEmpty(nickname)) { + log.error("BAD REQUEST EXCEPTION"); + } + userRepository.findAllByEmailOrNickname(email, nickname) + .ifPresent(user -> log.error("DUPLICATION ERROR")); + } }