Skip to content

Commit

Permalink
Merge pull request #102 from SWM-Flash/integration
Browse files Browse the repository at this point in the history
Integration
  • Loading branch information
ChoiWonYu authored Nov 28, 2024
2 parents a3aaac3 + cd38ba2 commit 44cd10e
Show file tree
Hide file tree
Showing 46 changed files with 713 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.first.flash.climbing.favorite.application;

import com.first.flash.climbing.favorite.application.dto.MemberFavoriteGymResponseDto;
import com.first.flash.climbing.favorite.domain.MemberFavoriteGym;
import com.first.flash.climbing.favorite.domain.MemberFavoriteGymRepository;
import com.first.flash.climbing.gym.domian.ClimbingGymIdConfirmRequestedEvent;
import com.first.flash.global.event.Events;
import com.first.flash.global.util.AuthUtil;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MemberFavoriteGymService {

private final MemberFavoriteGymRepository memberFavoriteGymRepository;

public MemberFavoriteGymResponseDto toggleMemberFavoriteGym(final Long gymId) {
Events.raise(ClimbingGymIdConfirmRequestedEvent.of(gymId));
UUID memberId = AuthUtil.getId();
Optional<MemberFavoriteGym> favoriteGym = memberFavoriteGymRepository.findByMemberIdAndGymId(memberId, gymId);

if (favoriteGym.isPresent()) {
memberFavoriteGymRepository.delete(favoriteGym.get());
} else {
MemberFavoriteGym memberFavoriteGym = MemberFavoriteGym.createDefault(memberId, gymId);
memberFavoriteGymRepository.save(memberFavoriteGym);
}
return MemberFavoriteGymResponseDto.toDtoByStatus(favoriteGym.isPresent());
}

public List<Long> findFavoriteGymIdsByMemberId(final UUID memberId) {
return memberFavoriteGymRepository.findByMemberId(memberId).stream()
.map(MemberFavoriteGym::getGymId)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.first.flash.climbing.favorite.application.dto;

public record MemberFavoriteGymResponseDto(String message) {

public static MemberFavoriteGymResponseDto toDtoByStatus(final boolean present) {
if (present) {
return new MemberFavoriteGymResponseDto("즐겨찾기에서 제거되었습니다.");
}
return new MemberFavoriteGymResponseDto("즐겨찾기에 추가되었습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.first.flash.climbing.favorite.domain;

import com.first.flash.global.domain.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import java.util.UUID;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
@Getter
@ToString
public class MemberFavoriteGym extends BaseEntity {

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

protected MemberFavoriteGym(final UUID memberId, final Long gymId) {
this.memberId = memberId;
this.gymId = gymId;
}

public static MemberFavoriteGym createDefault(final UUID memberId, final Long gymId) {
return new MemberFavoriteGym(memberId, gymId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.first.flash.climbing.favorite.domain;

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

public interface MemberFavoriteGymRepository {

MemberFavoriteGym save(final MemberFavoriteGym memberFavoriteGym);

Optional<MemberFavoriteGym> findById(final Long id);

List<MemberFavoriteGym> findByMemberId(final UUID memberId);

Optional<MemberFavoriteGym> findByMemberIdAndGymId(final UUID memberId, final Long gymId);

void delete(final MemberFavoriteGym memberFavoriteGym);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.first.flash.climbing.favorite.infrastructure;

import com.first.flash.climbing.favorite.domain.MemberFavoriteGym;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberFavoriteGymJpaRepository extends JpaRepository<MemberFavoriteGym, Long> {

MemberFavoriteGym save(final MemberFavoriteGym memberFavoriteGym);

Optional<MemberFavoriteGym> findById(final Long id);

List<MemberFavoriteGym> findByMemberId(final UUID memberId);

Optional<MemberFavoriteGym> findByMemberIdAndGymId(final UUID memberId, final Long gymId);

void delete(final MemberFavoriteGym memberFavoriteGym);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.first.flash.climbing.favorite.infrastructure;

import com.first.flash.climbing.favorite.domain.MemberFavoriteGym;
import com.first.flash.climbing.favorite.domain.MemberFavoriteGymRepository;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class MemberFavoriteGymRepositoryImpl implements MemberFavoriteGymRepository {

private final MemberFavoriteGymJpaRepository memberFavoriteGymJpaRepository;

@Override
public MemberFavoriteGym save(final MemberFavoriteGym memberFavoriteGym) {
return memberFavoriteGymJpaRepository.save(memberFavoriteGym);
}

@Override
public Optional<MemberFavoriteGym> findById(final Long id) {
return memberFavoriteGymJpaRepository.findById(id);
}

@Override
public List<MemberFavoriteGym> findByMemberId(final UUID memberId) {
return memberFavoriteGymJpaRepository.findByMemberId(memberId);
}

@Override
public Optional<MemberFavoriteGym> findByMemberIdAndGymId(final UUID memberId,
final Long gymId) {
return memberFavoriteGymJpaRepository.findByMemberIdAndGymId(memberId, gymId);
}

@Override
public void delete(final MemberFavoriteGym memberFavoriteGym) {
memberFavoriteGymJpaRepository.delete(memberFavoriteGym);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.first.flash.climbing.favorite.ui;

import com.first.flash.climbing.favorite.application.MemberFavoriteGymService;
import com.first.flash.climbing.favorite.application.dto.MemberFavoriteGymResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "favorite", description = "즐겨찾기 API")
@RestController
@RequiredArgsConstructor
public class MemberFavoriteGymController {

private final MemberFavoriteGymService memberFavoriteGymService;

@Operation(summary = "클라이밍장 즐겨찾기 생성/삭제", description = "클라이밍장 id로 즐겨찾기 토글")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "즐겨찾기 생성 및 삭제",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = MemberFavoriteGymResponseDto.class))),
@ApiResponse(responseCode = "400", description = "유효하지 않은 요청 형식",
content = @Content(mediaType = "application/json"))
})
@PutMapping("/favorites/{gymId}")
public ResponseEntity<MemberFavoriteGymResponseDto> toggleMemberFavoriteGym(
@PathVariable final Long gymId) {
return ResponseEntity.status(HttpStatus.OK)
.body(memberFavoriteGymService.toggleMemberFavoriteGym(gymId));
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.first.flash.climbing.gym.application;

import com.first.flash.climbing.favorite.application.MemberFavoriteGymService;
import com.first.flash.climbing.gym.application.dto.ClimbingGymCreateRequestDto;
import com.first.flash.climbing.gym.application.dto.ClimbingGymCreateResponseDto;
import com.first.flash.climbing.gym.application.dto.ClimbingGymDetailResponseDto;
import com.first.flash.climbing.gym.application.dto.ClimbingGymResponseDto;
import com.first.flash.climbing.gym.infrastructure.dto.ClimbingGymResponseDto;
import com.first.flash.climbing.gym.domian.ClimbingGym;
import com.first.flash.climbing.gym.domian.ClimbingGymRepository;
import com.first.flash.climbing.gym.domian.vo.Difficulty;
import com.first.flash.climbing.gym.exception.exceptions.ClimbingGymNotFoundException;
import com.first.flash.climbing.gym.infrastructure.dto.SectorInfoResponseDto;
import com.first.flash.global.util.AuthUtil;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,6 +23,7 @@
public class ClimbingGymService {

private final ClimbingGymRepository climbingGymRepository;
private final MemberFavoriteGymService memberFavoriteGymService;

@Transactional
public ClimbingGymCreateResponseDto save(final ClimbingGymCreateRequestDto createRequestDto) {
Expand All @@ -32,21 +37,21 @@ public ClimbingGym findClimbingGymById(final Long id) {
}

public List<ClimbingGymResponseDto> findAllClimbingGyms() {
return climbingGymRepository.findAll().stream()
.map(ClimbingGymResponseDto::toDto)
.toList();
UUID memberId = AuthUtil.getId();
List<Long> favoriteGymIds = memberFavoriteGymService.findFavoriteGymIdsByMemberId(memberId);
return climbingGymRepository.findAllWithFavorites(favoriteGymIds);
}

public ClimbingGymDetailResponseDto findClimbingGymDetail(final Long id) {
ClimbingGym climbingGym = findClimbingGymById(id);
List<String> sectorNames = findSectorNamesById(id);
List<SectorInfoResponseDto> sectorNames = findSectorNamesById(id);
List<String> difficultyNames = getDifficultyNames(climbingGym);
return new ClimbingGymDetailResponseDto(climbingGym.getGymName(),
climbingGym.getMapImageUrl(), climbingGym.getCalendarImageUrl(),
difficultyNames, sectorNames);
}

private List<String> findSectorNamesById(final Long id) {
private List<SectorInfoResponseDto> findSectorNamesById(final Long id) {
return climbingGymRepository.findGymSectorNamesById(id);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.first.flash.climbing.gym.application.dto;

import com.first.flash.climbing.gym.infrastructure.dto.SectorInfoResponseDto;
import java.util.List;

public record ClimbingGymDetailResponseDto(String gymName, String mapImageUrl,
String calendarImageUrl,
List<String> difficulties,
List<String> sectors) {
List<SectorInfoResponseDto> sectors) {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.first.flash.climbing.gym.domian;

import com.first.flash.climbing.gym.infrastructure.dto.ClimbingGymResponseDto;
import com.first.flash.climbing.gym.infrastructure.dto.SectorInfoResponseDto;
import java.util.List;
import java.util.Optional;

Expand All @@ -9,7 +11,7 @@ public interface ClimbingGymRepository {

Optional<ClimbingGym> findById(final Long id);

List<ClimbingGym> findAll();
List<ClimbingGymResponseDto> findAllWithFavorites(final List<Long> favoriteGymIds);

List<String> findGymSectorNamesById(final Long id);
List<SectorInfoResponseDto> findGymSectorNamesById(final Long id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.first.flash.climbing.gym.infrastructure;

import com.first.flash.climbing.gym.domian.ClimbingGym;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -10,6 +9,4 @@ public interface ClimbingGymJpaRepository extends JpaRepository<ClimbingGym, Lon
ClimbingGym save(final ClimbingGym gym);

Optional<ClimbingGym> findById(final Long id);

List<ClimbingGym> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.first.flash.climbing.gym.infrastructure;

import static com.first.flash.climbing.gym.domian.QClimbingGym.climbingGym;
import static com.first.flash.climbing.sector.domain.QSector.sector;

import com.first.flash.climbing.gym.infrastructure.dto.ClimbingGymResponseDto;
import com.first.flash.climbing.gym.infrastructure.dto.SectorInfoResponseDto;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,12 +18,29 @@ public class ClimbingGymQueryDslRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<String> findSortedSectorNamesByGymId(final Long gymId) {
return jpaQueryFactory.select(sector.sectorName.name)
public List<SectorInfoResponseDto> findSortedSectorNamesByGymId(final Long gymId) {
return jpaQueryFactory.select(
Projections.constructor(SectorInfoResponseDto.class, sector.sectorName.name,
sector.selectedImageUrl))
.from(sector)
.where(sector.gymId.eq(gymId), sector.removalInfo.isExpired.isFalse())
.distinct()
.orderBy(sector.sectorName.name.asc())
.fetch();
}

public List<ClimbingGymResponseDto> findAllWithFavorites(final List<Long> favoriteGymIds) {
return jpaQueryFactory.select(
Projections.constructor(ClimbingGymResponseDto.class, climbingGym.id,
climbingGym.gymName, climbingGym.thumbnailUrl, climbingGym.id.in(favoriteGymIds))
)
.from(climbingGym)
.orderBy(
new CaseBuilder()
.when(climbingGym.id.in(favoriteGymIds)).then(1)
.otherwise(0).desc(),
climbingGym.gymName.asc()
)
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.first.flash.climbing.gym.domian.ClimbingGym;
import com.first.flash.climbing.gym.domian.ClimbingGymRepository;
import com.first.flash.climbing.gym.infrastructure.dto.ClimbingGymResponseDto;
import com.first.flash.climbing.gym.infrastructure.dto.SectorInfoResponseDto;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,12 +27,12 @@ public Optional<ClimbingGym> findById(final Long id) {
}

@Override
public List<ClimbingGym> findAll() {
return climbingGymJpaRepository.findAll();
}
public List<ClimbingGymResponseDto> findAllWithFavorites(final List<Long> favoriteGymIds){
return climbingGymQueryDslRepository.findAllWithFavorites(favoriteGymIds);
};

@Override
public List<String> findGymSectorNamesById(final Long id) {
public List<SectorInfoResponseDto> findGymSectorNamesById(final Long id) {
return climbingGymQueryDslRepository.findSortedSectorNamesByGymId(id);
}
}
Loading

0 comments on commit 44cd10e

Please sign in to comment.