Skip to content

Commit

Permalink
Merge pull request #15 from Team-MindWay/feature/14-erd-match-entity-…
Browse files Browse the repository at this point in the history
…created

🔀 :: erd에 맞게 엔티티 구성하기
  • Loading branch information
Umjiseung authored Apr 2, 2024
2 parents a34052c + a0df475 commit 3987fe2
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 52 deletions.
34 changes: 34 additions & 0 deletions src/main/java/com/mindway/server/v2/domain/Book/entity/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mindway.server.v2.domain.Book.entity;

import com.mindway.server.v2.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

private String author;

private String url;

private LocalDate created_at;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.mindway.server.v2.domain.auth.exception.ExpiredRefreshTokenException;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
import com.mindway.server.v2.domain.auth.service.LogoutService;
import com.mindway.server.v2.domain.member.entity.Member;
import com.mindway.server.v2.domain.member.util.MemberUtil;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.util.UserUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,12 +16,12 @@
public class LogoutServiceImpl implements LogoutService {

private final RefreshRepository refreshRepository;
private final MemberUtil memberUtil;
private final UserUtil userUtil;

public void execute() {
Member member = memberUtil.getCurrentMember();
User user = userUtil.getCurrentUser();

RefreshToken validRefreshToken = refreshRepository.findByMemberId(member.getId())
RefreshToken validRefreshToken = refreshRepository.findByMemberId(user.getId())
.orElseThrow(ExpiredRefreshTokenException::new);

refreshRepository.deleteById(validRefreshToken.getRefreshToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
import com.mindway.server.v2.domain.auth.service.ReissueTokenService;
import com.mindway.server.v2.domain.member.entity.Member;
import com.mindway.server.v2.domain.member.repository.MemberRepository;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import com.mindway.server.v2.global.security.jwt.JwtProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,19 +20,19 @@ public class ReissueTokenServiceImpl implements ReissueTokenService {

private final JwtProvider jwtProvider;
private final RefreshRepository refreshRepository;
private final MemberRepository memberRepository;
private final UserRepository userRepository;

public TokenResponse execute(String refreshToken) {
String parseRefreshToken = jwtProvider.parseRefreshToken(refreshToken);

RefreshToken refreshEntity = refreshRepository.findById(parseRefreshToken)
.orElseThrow(ExpiredRefreshTokenException::new);

Member member = memberRepository.findById(refreshEntity.getMemberId())
User user = userRepository.findById(refreshEntity.getMemberId())
.orElseThrow(MemberNotFoundException::new);

refreshRepository.deleteById(refreshEntity.getRefreshToken());

return jwtProvider.generateTokenDto(member.getId());
return jwtProvider.generateTokenDto(user.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
import com.mindway.server.v2.domain.auth.service.SignInService;
import com.mindway.server.v2.domain.member.entity.Member;
import com.mindway.server.v2.domain.member.entity.StudentNum;
import com.mindway.server.v2.domain.member.repository.MemberRepository;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.entity.StudentNum;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import com.mindway.server.v2.global.security.jwt.JwtProvider;
import gauth.GAuth;
import gauth.GAuthToken;
Expand All @@ -30,7 +30,7 @@ public class SignInServiceImpl implements SignInService {

private final GAuth gAuth;
private final RefreshRepository refreshRepository;
private final MemberRepository memberRepository;
private final UserRepository userRepository;
private final JwtProvider jwtProvider;

@Value("${gauth.clientId}")
Expand All @@ -50,12 +50,12 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
);
GAuthUserInfo userInfo = gAuth.getUserInfo(gAuthToken.getAccessToken());

Member member = memberRepository.findByEmail(userInfo.getEmail())
User user = userRepository.findByEmail(userInfo.getEmail())
.orElseGet(() -> saveMember(userInfo));

TokenResponse tokenResponse = jwtProvider.generateTokenDto(member.getId());
TokenResponse tokenResponse = jwtProvider.generateTokenDto(user.getId());

saveRefreshToken(tokenResponse, member);
saveRefreshToken(tokenResponse, user);

return tokenResponse;

Expand All @@ -68,24 +68,24 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
return null;
}

private Member saveMember(GAuthUserInfo gAuthUserInfo) {
Member member = Member.builder()
private User saveMember(GAuthUserInfo gAuthUserInfo) {
User user = User.builder()
.id(UUID.randomUUID())
.email(gAuthUserInfo.getEmail())
.name(gAuthUserInfo.getName())
.studentNum(new StudentNum(gAuthUserInfo.getGrade(), gAuthUserInfo.getClassNum(), gAuthUserInfo.getNum()))
.role(gAuthUserInfo.getRole())
.build();

memberRepository.save(member);
userRepository.save(user);

return member;
return user;
}

private void saveRefreshToken(TokenResponse tokenResponse, Member member) {
private void saveRefreshToken(TokenResponse tokenResponse, User user) {
RefreshToken refreshToken = RefreshToken.builder()
.refreshToken(tokenResponse.getRefreshToken())
.memberId(member.getId())
.memberId(user.getId())
.expiredAt(tokenResponse.getRefreshTokenExpiresIn())
.build();

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/mindway/server/v2/domain/event/entity/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mindway.server.v2.domain.event.entity;

import com.mindway.server.v2.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class Event {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

private String content;

private String img_url;

private LocalDate started_at;

private LocalDate ended_at;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

}
32 changes: 32 additions & 0 deletions src/main/java/com/mindway/server/v2/domain/goal/entity/Goal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mindway.server.v2.domain.goal.entity;

import com.mindway.server.v2.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Goal {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private LocalDate started_at;

private LocalDate ended_at;

private Long goal_count;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mindway.server.v2.domain.member.entity;
package com.mindway.server.v2.domain.user.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mindway.server.v2.domain.member.entity;
package com.mindway.server.v2.domain.user.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand All @@ -13,11 +13,10 @@
@AllArgsConstructor
@Getter
@Builder
public class Member {
public class User {

@Id
@GeneratedValue(generator = "UUID4")
@Column(name = "member_id")
private UUID id;

private String email;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mindway.server.v2.domain.user.repository;

import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import java.util.UUID;

public interface UserRepository extends JpaRepository<User, UUID> {
Optional<User> findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.mindway.server.v2.domain.member.util;
package com.mindway.server.v2.domain.user.util;

import com.mindway.server.v2.domain.auth.exception.MemberNotFoundException;
import com.mindway.server.v2.domain.member.entity.Member;
import com.mindway.server.v2.domain.member.repository.MemberRepository;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class MemberUtil {
public class UserUtil {

private final MemberRepository memberRepository;
private final UserRepository userRepository;

public Member getCurrentMember() {
public User getCurrentUser() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return memberRepository.findByEmail(email)
return userRepository.findByEmail(email)
.orElseThrow(MemberNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mindway.server.v2.global.auth;

import com.mindway.server.v2.domain.member.entity.Member;
import com.mindway.server.v2.domain.user.entity.User;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -9,7 +9,7 @@

@AllArgsConstructor
public class AuthDetails implements UserDetails {
private Member member;
private User user;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Expand All @@ -23,7 +23,7 @@ public String getPassword() {

@Override
public String getUsername() {
return member.getEmail();
return user.getEmail();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mindway.server.v2.global.auth;

import com.mindway.server.v2.domain.member.repository.MemberRepository;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import com.mindway.server.v2.global.exception.ErrorCode;
import com.mindway.server.v2.global.exception.MindWayException;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,11 +16,11 @@
@RequiredArgsConstructor
@Slf4j
public class AuthDetailsService implements UserDetailsService {
private final MemberRepository memberRepository;
private final UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
return memberRepository.findById(UUID.fromString(id))
return userRepository.findById(UUID.fromString(id))
.map(AuthDetails::new)
.orElseThrow(() -> new MindWayException(ErrorCode.MEMBER_NOT_FOUND));
}
Expand Down

0 comments on commit 3987fe2

Please sign in to comment.