Skip to content

Commit

Permalink
[FEAT] get all plans api 및 불필요한 dto 요소 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Nov 29, 2023
1 parent 4bf92e6 commit aee589a
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand All @@ -14,14 +13,15 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

import lombok.*;

import org.hibernate.annotations.Where;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Where;

@Entity
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.entity.OAuth2Info;

import com.twtw.backend.domain.plan.entity.PlanMember;
import java.util.List;
import java.util.Set;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
Expand All @@ -23,4 +27,11 @@ public interface MemberMapper {
default OAuth2Info convertOauth(OAuthRequest request) {
return new OAuth2Info(request.getToken(), request.getAuthType());
}

@IterableMapping(elementTargetType = MemberResponse.class)
List<MemberResponse> toMemberResponses(Set<PlanMember> planMembers);

@Mapping(target = "id", source = "planMember.member.id")
@Mapping(target = "nickname", source = "planMember.member.nickname")
MemberResponse toMemberResponse(PlanMember planMember);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.member.mapper.MemberMapper;
import com.twtw.backend.domain.member.repository.MemberRepository;
import com.twtw.backend.domain.plan.entity.Plan;
import com.twtw.backend.global.exception.EntityNotFoundException;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.stereotype.Service;

@Service
public class MemberService {
Expand Down Expand Up @@ -55,4 +55,8 @@ public SearchMemberResponse getMemberByNickname(String nickname) {
public MemberResponse getResponseByMember(Member member) {
return memberMapper.toMemberResponse(member);
}

public List<MemberResponse> getMemberResponses(final Plan plan) {
return memberMapper.toMemberResponses(plan.getPlanMembers());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ public class Place implements Auditable {
@Column(nullable = false)
private String placeName;

private Integer distance;

private String placeUrl;

private String categoryName;

private CategoryGroupCode categoryGroupCode;

@Embedded private Address address;
private String roadAddressName;

@Embedded private Coordinate coordinate;

Expand All @@ -55,20 +51,15 @@ public class Place implements Auditable {
@Builder
public Place(
final String placeName,
final Integer distance,
final String placeUrl,
final String categoryName,
final CategoryGroupCode categoryGroupCode,
final String addressName,
final String roadAddressName,
final Double longitude,
final Double latitude) {
this.placeName = placeName;
this.distance = distance;
this.placeUrl = placeUrl;
this.categoryName = categoryName;
this.categoryGroupCode = categoryGroupCode;
this.address = new Address(addressName, roadAddressName);
this.roadAddressName = roadAddressName;
this.coordinate = new Coordinate(longitude, latitude);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

import com.twtw.backend.domain.place.entity.Place;
import com.twtw.backend.domain.plan.dto.client.PlaceDetails;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface PlaceMapper {
@Mapping(target = "distance")
Place toEntity(PlaceDetails detail);

@Mapping(target = "distance")
PlaceDetails toPlaceResponse(Place place);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.twtw.backend.domain.plan.dto.response.PlanInfoResponse;
import com.twtw.backend.domain.plan.dto.response.PlanResponse;
import com.twtw.backend.domain.plan.service.PlanService;

import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -20,8 +20,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("plans")
@RequiredArgsConstructor
Expand Down Expand Up @@ -60,4 +58,9 @@ public ResponseEntity<Void> outPlan(@RequestBody PlanMemberRequest request) {
planService.outPlan(request);
return ResponseEntity.noContent().build();
}

@GetMapping
public ResponseEntity<List<PlanInfoResponse>> getPlans() {
return ResponseEntity.ok(planService.getPlans());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.twtw.backend.domain.plan.controller.advice;

import com.twtw.backend.domain.plan.exception.InvalidPlanMemberException;
import com.twtw.backend.domain.plan.exception.PlanMakerNotExistsException;
import com.twtw.backend.global.advice.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class PlanControllerAdvice {

@ExceptionHandler(InvalidPlanMemberException.class)
public ResponseEntity<ErrorResponse> refreshTokenInfoMismatch(
final InvalidPlanMemberException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(PlanMakerNotExistsException.class)
public ResponseEntity<ErrorResponse> refreshTokenInfoMismatch(
final PlanMakerNotExistsException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
@AllArgsConstructor
public class PlaceDetails {
private String placeName;
private Integer distance;
private String placeUrl;
private String categoryName;
private String addressName;
private String roadAddressName;
private Double longitude;
private Double latitude;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
public class PlanInfoResponse {
private UUID planId;
private UUID placeId;
private PlaceDetails placeDetail;
private GroupInfoResponse groupInfoResponse;
private UUID planMakerId;
private PlaceDetails placeDetails;
private GroupInfoResponse groupInfo;
private List<MemberResponse> members;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.place.entity.Place;
import com.twtw.backend.domain.plan.exception.InvalidPlanMemberException;
import com.twtw.backend.domain.plan.exception.PlanMakerNotExistsException;
import com.twtw.backend.global.audit.AuditListener;
import com.twtw.backend.global.audit.Auditable;
import com.twtw.backend.global.audit.BaseTime;
Expand Down Expand Up @@ -63,13 +64,13 @@ public class Plan implements Auditable {
private BaseTime baseTime;

public Plan(Member member, Place place, Group group) {
addMember(member);
this.planMembers.add(new PlanMember(this, member, true));
addPlace(place);
addGroup(group);
}

public void addMember(final Member member) {
this.planMembers.add(new PlanMember(this, member));
this.planMembers.add(new PlanMember(this, member, false));
}

public void deleteMember(final Member member) {
Expand All @@ -91,4 +92,12 @@ public void addGroup(final Group group) {
this.group = group;
this.group.addToGroup(this);
}

public UUID getPlanMakerId() {
return this.planMembers.stream()
.filter(PlanMember::getIsPlanMaker)
.findAny()
.orElseThrow(PlanMakerNotExistsException::new)
.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ public class PlanMember implements Auditable {
@ManyToOne(fetch = FetchType.LAZY)
private Member member;

private Boolean isPlanMaker;

@Setter
@Embedded
@Column(nullable = false)
private BaseTime baseTime;

@Builder
public PlanMember(final Plan plan, final Member member) {
public PlanMember(final Plan plan, final Member member, final Boolean isPlanMaker) {
this.plan = plan;
this.member = member;
this.isPlanMaker = isPlanMaker;
}

public boolean hasSameMember(final Member member) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.twtw.backend.domain.plan.exception;

public class PlanMakerNotExistsException extends IllegalStateException {

private static final String MESSAGE = "계획을 생성한 멤버가 존재하지 않습니다.";

public PlanMakerNotExistsException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.twtw.backend.domain.plan.mapper;

import com.twtw.backend.domain.group.dto.response.GroupInfoResponse;
import com.twtw.backend.domain.member.dto.response.MemberResponse;
import com.twtw.backend.domain.plan.dto.client.PlaceDetails;
import com.twtw.backend.domain.plan.dto.client.SearchDestinationResponse;
import com.twtw.backend.domain.plan.dto.response.PlanDestinationResponse;
import com.twtw.backend.domain.plan.dto.response.PlanInfoResponse;
import com.twtw.backend.domain.plan.dto.response.PlanResponse;
import com.twtw.backend.domain.plan.entity.Plan;

import java.util.List;
import org.mapstruct.IterableMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
Expand All @@ -12,4 +19,18 @@ public interface PlanMapper {
@Mapping(target = "planId", source = "id")
@Mapping(target = "groupId", source = "group.id")
PlanResponse toPlanResponse(Plan plan);

@Mapping(target = "results", source = "response.documents")
@Mapping(target = "isLast", source = "response.meta.isEnd")
PlanDestinationResponse toPlanDestinationResponse(SearchDestinationResponse response);

@Mapping(target = "planId", source = "plan.id")
@Mapping(target = "placeId", source = "plan.place.id")
@Mapping(target = "planMakerId", source = "plan.planMakerId")
@Mapping(target = "groupInfo", source = "groupInfoResponse")
@Mapping(target = "members", source = "memberResponses")
PlanInfoResponse toPlanInfoResponse(Plan plan, PlaceDetails placeDetails, GroupInfoResponse groupInfoResponse, List<MemberResponse> memberResponses);

@IterableMapping(elementTargetType = PlanInfoResponse.class)
List<PlanInfoResponse> toPlanInfoResponses(List<Plan> plans);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.twtw.backend.domain.plan.repository;

import com.twtw.backend.domain.member.entity.Member;
import com.twtw.backend.domain.plan.entity.Plan;

import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.UUID;
public interface PlanRepository extends JpaRepository<Plan, UUID> {

public interface PlanRepository extends JpaRepository<Plan, UUID> {}
@Query("select p from Plan p join fetch p.planMembers pm join fetch pm.member m where m.id = :member")
List<Plan> findAllByMember(Member member);
}
Loading

0 comments on commit aee589a

Please sign in to comment.