Skip to content

Commit

Permalink
feat : #4 User 조회 생성 API 작성 및 H2 설정 추가
Browse files Browse the repository at this point in the history
* findAll, create, validation 작성
* H2 config 작성
* UserDTO 작성
  • Loading branch information
sehajyang committed Oct 19, 2020
1 parent 061e5cf commit 9bbbf96
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 7 deletions.
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UserDto> findAll(UserDto.SearchQuery userDto){
return userService.findAll();
}

@PostMapping
public void save(UserDto.InsertUser user) {
userService.create(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/**
* Created by sehajyang
* DateTime : 2020/10/09
* Descrpition :
*/
public enum RoleType {
USER, ADMIN
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/resumers/server/oauth/model/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -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 {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* Created by sehajyang
* DateTime : 2020/10/09
* Descrpition :
*/

@Entity
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/resumers/server/oauth/model/entity/User.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +14,8 @@
* DateTime : 2020/10/03
*/

@Getter
@NoArgsConstructor
@Entity
@Table(name = "user")
public class User extends BaseEntity {
Expand All @@ -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)
Expand Down Expand Up @@ -65,4 +71,9 @@ public class User extends BaseEntity {
joinColumns = @JoinColumn(name ="user_id"), inverseJoinColumns= @JoinColumn(name="role_id"))
private Set<Role> roles = new LinkedHashSet<>();

public User(UserDto userDto) {
this.email = userDto.getEmail();
this.nickname = userDto.getNickname();
this.description = userDto.getDescription();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, Long> {
List<User> findAll();
Optional<User> findAllByEmailOrNickname(String email, String nickname);
}
39 changes: 39 additions & 0 deletions src/main/java/com/resumers/server/oauth/service/UserService.java
Original file line number Diff line number Diff line change
@@ -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<UserDto> findAll() {
List<User> 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"));
}
}

0 comments on commit 9bbbf96

Please sign in to comment.