Skip to content

Commit

Permalink
Merge pull request #11 from Fairy-Taless/feature/#8-user
Browse files Browse the repository at this point in the history
✨ FEAT. User 기능 추가
  • Loading branch information
junhaa authored May 1, 2024
2 parents 25ffc1b + 6ac781b commit 97a912c
Show file tree
Hide file tree
Showing 35 changed files with 1,171 additions and 36 deletions.
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ dependencies {
// AWS
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'


// Spring Security OAUTH 2.1
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-oauth2-client'

// JWT token
implementation 'io.jsonwebtoken:jjwt:0.12.3'

compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package fairytale.tbd.domain.user.converter;

import java.util.ArrayList;

import fairytale.tbd.domain.user.entity.User;
import fairytale.tbd.domain.user.web.dto.UserRequestDTO;
import fairytale.tbd.domain.user.web.dto.UserResponseDTO;

public class UserConverter {
public static User toUser(UserRequestDTO.AddUserDTO request){
public static User toUser(UserRequestDTO.AddUserDTO request, String encodedPassword) {
return User.builder()
.loginId(request.getLoginId())
.password(request.getPassword())
.password(encodedPassword)
.username(request.getUsername())
.gender(request.getGender())
.authorityList(new ArrayList<>())
.build();
}

public static UserResponseDTO.AddUserResultDTO toAddUserResultDTO(User user){
public static UserResponseDTO.AddUserResultDTO toAddUserResultDTO(User user) {
return UserResponseDTO.AddUserResultDTO.builder()
.userId(user.getId())
.createdAt(user.getCreatedAt())
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/fairytale/tbd/domain/user/entity/Authority.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fairytale.tbd.domain.user.entity;

import fairytale.tbd.domain.user.enums.Role;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Authority {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "authority_id")
private Long id;

@Column(name = "authority_role")
private Role role;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

// 연관 관계 편의 메서드
public void setUser(User user){
this.user = user;
}
}
22 changes: 21 additions & 1 deletion src/main/java/fairytale/tbd/domain/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fairytale.tbd.domain.user.entity;


import java.util.ArrayList;
import java.util.List;

import fairytale.tbd.domain.user.enums.Gender;
import fairytale.tbd.domain.voice.entity.Voice;
import fairytale.tbd.global.entity.BaseEntity;
Expand All @@ -13,6 +16,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -45,14 +49,30 @@ public class User extends BaseEntity {
@Column(name = "username", nullable = false)
private String username;

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@Column(name = "refresh_token", nullable = true)
private String refreshToken;

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private Voice voice;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Authority> authorityList = new ArrayList<>();


// 연관관계 편의 메서드
public void setVoice(Voice voice){
this.voice = voice;
voice.setUser(this);
}

public void addAuthority(Authority authority){
authorityList.add(authority);
authority.setUser(this);
}

// RefreshToken update
public void updateRefreshToken(String refreshToken){
this.refreshToken = refreshToken;
}

}
5 changes: 5 additions & 0 deletions src/main/java/fairytale/tbd/domain/user/enums/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fairytale.tbd.domain.user.enums;

public enum Role {
ROLE_USER, ROLE_ADMIN, ROLE_GUEST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fairytale.tbd.domain.user.exception;

import fairytale.tbd.global.enums.statuscode.BaseCode;
import fairytale.tbd.global.exception.GeneralException;

public class UserNotExistException extends GeneralException {
public UserNotExistException(BaseCode errorStatus) {
super(errorStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
boolean existsByUsername(String username);
Optional<User> findByLoginId(String loginId);

Optional<User> findById(Long userId);

Optional<User> findByRefreshToken(String refreshToken);

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package fairytale.tbd.domain.user.service;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fairytale.tbd.domain.user.converter.UserConverter;
import fairytale.tbd.domain.user.entity.Authority;
import fairytale.tbd.domain.user.entity.User;
import fairytale.tbd.domain.user.enums.Role;
import fairytale.tbd.domain.user.repository.UserRepository;
import fairytale.tbd.domain.user.web.dto.UserRequestDTO;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserCommandServiceImpl implements UserCommandService{
public class UserCommandServiceImpl implements UserCommandService {

private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

@Transactional
@Override
public User addUser(UserRequestDTO.AddUserDTO request) {
User user = UserConverter.toUser(request);
String encodedPassword = passwordEncoder.encode(request.getPassword());
User user = UserConverter.toUser(request, encodedPassword);
Authority authority = Authority.builder()
.role(Role.ROLE_USER)
.build();
user.addAuthority(authority);
return userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fairytale.tbd.domain.user.service;

import java.util.Optional;

import fairytale.tbd.domain.user.entity.User;

public interface UserQueryService {

Optional<User> getUserWithAuthorities(String loginId);

void updateRefreshToken(User user, String reIssuedRefreshToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fairytale.tbd.domain.user.service;

import java.util.Optional;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fairytale.tbd.domain.user.entity.User;
import fairytale.tbd.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserQueryServiceImpl implements UserQueryService {

private final UserRepository userRepository;

@Override
public Optional<User> getUserWithAuthorities(String loginId) {
User user = userRepository.findByLoginId(loginId).orElse(null);
user.getAuthorityList().size();
return Optional.ofNullable(user);
}

@Transactional
@Override
public void updateRefreshToken(User user, String reIssuedRefreshToken) {
user.updateRefreshToken(reIssuedRefreshToken);
userRepository.saveAndFlush(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UserRestController {
private final UserCommandService userCommandService;
private static final Logger LOGGER = LogManager.getLogger(UserRestController.class);

@PostMapping("")
@PostMapping("/signup")
public ApiResponse<UserResponseDTO.AddUserResultDTO> join(@Valid @RequestBody UserRequestDTO.AddUserDTO request) {
LOGGER.info("request = {}", request);
User user = userCommandService.addUser(request);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/fairytale/tbd/domain/voice/entity/Fairytale.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fairytale.tbd.domain.voice.entity;

import java.util.ArrayList;
import java.util.List;

import fairytale.tbd.global.entity.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Fairytale extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fairytale_id")
private Long id;

@Column(name = "fairytale_name", nullable = false)
private String name;

@OneToMany(mappedBy = "fairytale", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Segment> segmentList = new ArrayList<>();

// 연관 관계 편의 메서드
public void addSegment(Segment segment) {
segmentList.add(segment);
segment.setFairytale(this);
}

}
69 changes: 69 additions & 0 deletions src/main/java/fairytale/tbd/domain/voice/entity/Segment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package fairytale.tbd.domain.voice.entity;

import fairytale.tbd.domain.voice.enums.VoiceType;
import fairytale.tbd.global.entity.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "fairytale_segment")
public class Segment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fariytale_segment_id")
private Long id;

@Column(name = "segment_context", nullable = false)
private String context;

@Column(name = "is_main_character", nullable = false)
private boolean isMainCharacter;

@Column(name = "voice_type", nullable = false)
private VoiceType voiceType;

@Column(name = "segment_num", nullable = false)
private Long num;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fairytale_id", nullable = false)
private Fairytale fairytale;

@OneToOne(mappedBy = "segment", cascade = CascadeType.ALL, orphanRemoval = true)
private TTSSegment ttsSegment;

@OneToOne(mappedBy = "segment", cascade = CascadeType.ALL, orphanRemoval = true)
private UserTTSSegment userTTSSegment;

// 연관 관계 편의 메서드

public void setFairytale(Fairytale fairytale) {
this.fairytale = fairytale;
}

public void setTtsSegment(TTSSegment ttsSegment) {
this.ttsSegment = ttsSegment;
}

public void setUserTTSSegment(UserTTSSegment userTTSSegment) {
this.userTTSSegment = userTTSSegment;
}

}
Loading

0 comments on commit 97a912c

Please sign in to comment.