Skip to content

Commit

Permalink
Merge pull request #406 from woowacourse-teams/refactor/#403
Browse files Browse the repository at this point in the history
[refactor] Member 간접참조로 변경 #403
  • Loading branch information
Combi153 authored Sep 27, 2023
2 parents f7a3f20 + ab8ba43 commit 9f3472c
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
import dev.tripdraw.member.exception.MemberException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByOauthIdAndOauthType(String oauthId, OauthType oauthType);

boolean existsByNickname(String nickname);

@Query("SELECT m.nickname FROM Member m WHERE m.id = :id")
String getNicknameById(@Param("id") Long id);

default Member getById(Long id) {
return findById(id)
.orElseThrow(() -> new MemberException(MEMBER_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.tripdraw.member.exception;

import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;

import dev.tripdraw.common.exception.ExceptionType;
Expand All @@ -9,6 +10,7 @@
public enum MemberExceptionType implements ExceptionType {
DUPLICATE_NICKNAME(CONFLICT, "이미 존재하는 닉네임입니다."),
MEMBER_NOT_FOUND(NOT_FOUND, "존재하지 않는 회원입니다."),
MEMBER_NOT_REGISTERED(INTERNAL_SERVER_ERROR, "가입이 완료되지 않은 회원이 존재합니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import dev.tripdraw.common.auth.LoginUser;
import dev.tripdraw.file.application.FileUploader;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.post.domain.Post;
import dev.tripdraw.post.domain.PostCreateEvent;
import dev.tripdraw.post.domain.PostRepository;
Expand All @@ -27,14 +25,13 @@
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import dev.tripdraw.trip.exception.TripException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RequiredArgsConstructor
@Transactional
@Service
Expand All @@ -44,7 +41,6 @@ public class PostService {
private final PostRepository postRepository;
private final TripRepository tripRepository;
private final PointRepository pointRepository;
private final MemberRepository memberRepository;
private final FileUploader fileUploader;
private final ApplicationEventPublisher applicationEventPublisher;

Expand All @@ -53,12 +49,13 @@ public PostCreateResponse addAtCurrentPoint(
PostAndPointCreateRequest postAndPointCreateRequest,
MultipartFile file
) {
Member member = memberRepository.getById(loginUser.memberId());
Long memberId = loginUser.memberId();

Trip trip = tripRepository.getById(postAndPointCreateRequest.tripId());
trip.validateAuthorization(member);
trip.validateAuthorization(memberId);
Point point = pointRepository.save(postAndPointCreateRequest.toPoint(trip));

Post post = postAndPointCreateRequest.toPost(member, point);
Post post = postAndPointCreateRequest.toPost(memberId, point);
uploadImage(file, post, trip);
Post savedPost = postRepository.save(post);

Expand All @@ -80,12 +77,13 @@ public PostCreateResponse addAtExistingLocation(
PostRequest postRequest,
MultipartFile file
) {
Member member = memberRepository.getById(loginUser.memberId());
Long memberId = loginUser.memberId();

Trip trip = tripRepository.getById(postRequest.tripId());
trip.validateAuthorization(member);
trip.validateAuthorization(memberId);
Point point = pointRepository.getById(postRequest.pointId());

Post post = postRequest.toPost(member, point);
Post post = postRequest.toPost(memberId, point);
uploadImage(file, post, trip);
Post savedPost = postRepository.save(post);

Expand All @@ -111,11 +109,12 @@ public PostsResponse readAllByTripId(Long tripId) {
}

public void update(LoginUser loginUser, Long postId, PostUpdateRequest postUpdateRequest, MultipartFile file) {
Long memberId = loginUser.memberId();

Post post = postRepository.getByPostId(postId);
Member member = memberRepository.getById(loginUser.memberId());
post.validateAuthorization(member);
post.validateAuthorization(memberId);
Trip trip = tripRepository.getById(post.tripId());
trip.validateAuthorization(member);
trip.validateAuthorization(memberId);

post.changeTitle(postUpdateRequest.title());
post.changeWriting(postUpdateRequest.writing());
Expand All @@ -124,8 +123,7 @@ public void update(LoginUser loginUser, Long postId, PostUpdateRequest postUpdat

public void delete(LoginUser loginUser, Long postId) {
Post post = postRepository.getByPostId(postId);
Member member = memberRepository.getById(loginUser.memberId());
post.validateAuthorization(member);
post.validateAuthorization(loginUser.memberId());
postRepository.deleteById(postId);
}

Expand Down
19 changes: 7 additions & 12 deletions backend/src/main/java/dev/tripdraw/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package dev.tripdraw.post.domain;

import static dev.tripdraw.post.exception.PostExceptionType.NOT_AUTHORIZED_TO_POST;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import dev.tripdraw.common.entity.BaseEntity;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.post.exception.PostException;
import dev.tripdraw.trip.domain.Point;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import java.time.LocalDateTime;
import lombok.Getter;
Expand Down Expand Up @@ -48,31 +45,29 @@ public class Post extends BaseEntity {
@JoinColumn(name = "point_id")
private Point point;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "member_id")
private Member member;
private Long memberId;

private String postImageUrl;

private String routeImageUrl;

public Post(String title, Point point, String address, String writing, Member member, Long tripId) {
this(null, title, point, address, writing, member, tripId);
public Post(String title, Point point, String address, String writing, Long memberId, Long tripId) {
this(null, title, point, address, writing, memberId, tripId);
}

public Post(Long id, String title, Point point, String address, String writing, Member member, Long tripId) {
public Post(Long id, String title, Point point, String address, String writing, Long memberId, Long tripId) {
point.registerPost();
this.id = id;
this.title = title;
this.point = point;
this.address = address;
this.writing = writing;
this.member = member;
this.memberId = memberId;
this.tripId = tripId;
}

public void validateAuthorization(Member member) {
if (!this.member.equals(member)) {
public void validateAuthorization(Long memberId) {
if (!this.memberId.equals(memberId)) {
throw new PostException(NOT_AUTHORIZED_TO_POST);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.tripdraw.post.domain;

import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND;

import dev.tripdraw.post.exception.PostException;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import static dev.tripdraw.post.exception.PostExceptionType.POST_NOT_FOUND;

public interface PostRepository extends JpaRepository<Post, Long> {

@Query("SELECT p FROM Post p JOIN FETCH p.point where p.tripId = :tripId")
Expand All @@ -20,6 +20,6 @@ default Post getByPostId(Long id) {
}

@Modifying
@Query("DELETE FROM Post p WHERE p.member.id = :memberId")
void deleteByMemberId(@Param(value = "memberId") Long memberId);
@Query("DELETE FROM Post p WHERE p.memberId = :memberId")
void deleteByMemberId(@Param("memberId") Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.fasterxml.jackson.annotation.JsonFormat.Shape.STRING;

import com.fasterxml.jackson.annotation.JsonFormat;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.post.domain.Post;
import dev.tripdraw.trip.domain.Point;
import dev.tripdraw.trip.domain.Trip;
Expand Down Expand Up @@ -53,7 +52,7 @@ public Point toPoint(Trip trip) {
return new Point(latitude, longitude, recordedAt, trip);
}

public Post toPost(Member member, Point point) {
return new Post(title, point, address, writing, member, tripId);
public Post toPost(Long memberId, Point point) {
return new Post(title, point, address, writing, memberId, tripId);
}
}
5 changes: 2 additions & 3 deletions backend/src/main/java/dev/tripdraw/post/dto/PostRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.tripdraw.post.dto;

import dev.tripdraw.member.domain.Member;
import dev.tripdraw.post.domain.Post;
import dev.tripdraw.trip.domain.Point;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -30,7 +29,7 @@ public record PostRequest(
String writing
) {

public Post toPost(Member member, Point point) {
return new Post(title, point, address, writing, member, tripId);
public Post toPost(Long memberId, Point point) {
return new Post(title, point, address, writing, memberId, tripId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.tripdraw.trip.application;

import dev.tripdraw.common.auth.LoginUser;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.trip.domain.Point;
import dev.tripdraw.trip.domain.PointRepository;
Expand Down Expand Up @@ -39,16 +38,16 @@ public class TripService {
private final ApplicationEventPublisher applicationEventPublisher;

public TripCreateResponse create(LoginUser loginUser) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = Trip.from(member);
Long memberId = loginUser.memberId();

Trip trip = Trip.of(memberId, memberRepository.getNicknameById(memberId));
Trip savedTrip = tripRepository.save(trip);
return TripCreateResponse.from(savedTrip);
}

public PointCreateResponse addPoint(LoginUser loginUser, PointCreateRequest pointCreateRequest) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(pointCreateRequest.tripId());
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());

Point point = pointCreateRequest.toPoint();
point.setTrip(trip);
Expand All @@ -58,26 +57,23 @@ public PointCreateResponse addPoint(LoginUser loginUser, PointCreateRequest poin
}

public void deletePoint(LoginUser loginUser, Long pointId, Long tripId) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(tripId);
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());

Point point = pointRepository.getById(pointId);
pointRepository.delete(point);
}

@Transactional(readOnly = true)
public TripResponse readTripById(LoginUser loginUser, Long id) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(id);
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());
return TripResponse.from(trip);
}

@Transactional(readOnly = true)
public TripsSearchResponseOfMember readAllTripsOf(LoginUser loginUser) {
Member member = memberRepository.getById(loginUser.memberId());
List<Trip> trips = tripRepository.findAllByMemberId(member.id());
List<Trip> trips = tripRepository.findAllByMemberId(loginUser.memberId());
return TripsSearchResponseOfMember.from(trips);
}

Expand All @@ -95,9 +91,8 @@ public TripsSearchResponse readAll(TripSearchRequest tripSearchRequest) {
}

public void updateTripById(LoginUser loginUser, Long tripId, TripUpdateRequest tripUpdateRequest) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(tripId);
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());

trip.changeName(tripUpdateRequest.name());
trip.changeStatus(tripUpdateRequest.status());
Expand All @@ -107,18 +102,16 @@ public void updateTripById(LoginUser loginUser, Long tripId, TripUpdateRequest t

@Transactional(readOnly = true)
public PointResponse readPointByTripAndPointId(LoginUser loginUser, Long tripId, Long pointId) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(tripId);
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());

Point point = pointRepository.getById(pointId);
return PointResponse.from(point);
}

public void delete(LoginUser loginUser, Long tripId) {
Member member = memberRepository.getById(loginUser.memberId());
Trip trip = tripRepository.getById(tripId);
trip.validateAuthorization(member);
trip.validateAuthorization(loginUser.memberId());

tripRepository.delete(trip);
}
Expand Down
Loading

0 comments on commit 9f3472c

Please sign in to comment.