Skip to content

Commit

Permalink
feat: 명소 즐겨찾기 기능 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangyoo committed Jan 17, 2023
1 parent 615eef4 commit aa58485
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public ResponseEntity patchAttraction(@PathVariable("attraction-id") @Positive l
}

// 3. 명소 1개 정보 요청을 처리하는 핸들러
// 반환하는 정보 : 명소 정보(Id,이름, 설명, 주소, 이미지 주소), 좋아요 수, 좋아요 눌렀는지
// 반환하는 정보 : 명소 정보(Id,이름, 설명, 주소, 이미지 주소), 좋아요 수, 좋아요 눌렀는지, 즐겨찾기 수, 즐겨찾기 눌렀는지
@GetMapping("/{attraction-id}")
public ResponseEntity getAttraction(HttpServletRequest request,
@PathVariable("attraction-id") @Positive long attractionId){
Expand All @@ -111,6 +111,7 @@ public ResponseEntity getAttraction(HttpServletRequest request,
AttractionDetailResponseDto response =
mapper.attractionToAttractionDetailResponseDto(attraction);
response.setIsVoted(attractionService.isVoted(member, attraction));
response.setIsSaved(attractionService.isSaved(member,attraction));
return new ResponseEntity<>(new DataResponseDto<>(response), HttpStatus.OK);
}

Expand All @@ -124,18 +125,18 @@ public ResponseEntity getFilteredAttractions(@Positive @RequestParam(required =
Page<Attraction> attractionPage = attractionService.findFilteredAttractions(filterDto.getProvinces(), page-1, size);
List<Attraction> attractions = attractionPage.getContent();
return new ResponseEntity<>(new MultiResponseDto<>(
mapper.attractionsToAttractionResponses(attractions),attractionPage), HttpStatus.OK);
mapper.attractionsToAttractionResponseDtos(attractions),attractionPage), HttpStatus.OK);
}

// 5. 명소 Id를 기준으로 명소 여러개의 정보 요청을 처리하는 핸들러
// 반환하는 정보 : 명소 정보(id, 이름, 이미지 주소), 좋아요 수, 즐겨찾기 수(아직 구현안됨)
// 반환하는 정보 : 명소 정보(id, 이름, 이미지 주소), 좋아요 수, 즐겨찾기 수
@GetMapping
public ResponseEntity getAttractions(@Positive @RequestParam(required = false, defaultValue = "1") int page,
@Positive @RequestParam(required = false, defaultValue = "9") int size){
Page<Attraction> attractionPage = attractionService.findAttractions(page-1, size);
List<Attraction> attractions = attractionPage.getContent();
return new ResponseEntity(new MultiResponseDto<>(
mapper.attractionsToAttractionResponses(attractions),attractionPage), HttpStatus.OK);
mapper.attractionsToAttractionResponseDtos(attractions),attractionPage), HttpStatus.OK);
}


Expand Down Expand Up @@ -167,6 +168,23 @@ public ResponseEntity voteAttraction(HttpServletRequest request,
return new ResponseEntity(new DataResponseDto<>(response), HttpStatus.OK);
}

// 8. 명소 즐겨찾기
@PostMapping("/saves/{attraction-id}")
public ResponseEntity saveAttraction(HttpServletRequest request,
@PathVariable("attraction-id") @Positive long attractionId){
String userEmail = extractedUsername(request);
Member member = memberService.findMember(userEmail);

// 명소 정보를 찾는다
Attraction attraction = attractionService.findAttraction(attractionId);

boolean status = attractionService.saveAttraction(member, attraction);

AttractionSaveResponseDto response = new AttractionSaveResponseDto();
response.setIsSaved(status);
return new ResponseEntity(new DataResponseDto<>(response), HttpStatus.OK);
}

private String extractedUsername(HttpServletRequest request) {
String authorization = request.getHeader("authorization");
String substring = authorization.substring(7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
public class AttractionDetailResponseDto {
private Long attractionId;
private Long likes;

// private Long saves;
private Long saves;
private Boolean isVoted;

// private Boolean isSaved;
private Boolean isSaved;
private String attractionName;
private String attractionDescription;
private String attractionAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AttractionResponseDto {
private String attractionName;
private String attractionImageUrl;
private Long likes;
// private Long postNumber;
private Long saves;

// private Long saves;
// private Long postNumber;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.main36.picha.domain.attraction.dto;

import lombok.Data;

@Data
public class AttractionSaveResponseDto {
private Boolean isSaved;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class Attraction extends Auditable {
@ColumnDefault("0")
private Long likes;

// @Column(name = "saves", nullable = false)
// @ColumnDefault("0")
// private Long saves;
@Column(name = "saves", nullable = false)
@ColumnDefault("0")
private Long saves;
@Lob
@Column( name = "attraction_description", nullable = false)
private String attractionDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ default AttractionDetailResponseDto attractionToAttractionDetailResponseDto(Attr
.build();
};

List<AttractionResponseDto> attractionsToAttractionResponses(List<Attraction> attractions);
List<AttractionResponseDto> attractionsToAttractionResponseDtos(List<Attraction> attractions);
}

/*@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.main36.picha.domain.attraction_likes.entity.AttractionLikes;
import com.main36.picha.domain.attraction_likes.repository.AttractionLikesRepository;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.save.entity.Save;
import com.main36.picha.domain.save.repository.SaveRepository;
import com.main36.picha.global.exception.BusinessLogicException;
import com.main36.picha.global.exception.ExceptionCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,8 +27,8 @@
public class AttractionService {
private final AttractionRepository attractionRepository;
private final AttractionImageService attractionImageService;

private final AttractionLikesRepository attractionLikesRepository;
private final SaveRepository saveRepository;


public Attraction createAttraction(Attraction attraction){
Expand Down Expand Up @@ -110,6 +112,35 @@ public boolean isVoted(Member member, Attraction attraction) {
return likes.isPresent();
}

public boolean saveAttraction(Member member, Attraction attraction){
// 즐겨찾기를 누른적이 있나?
Optional<Save> save = saveRepository.findByMemberAndAttraction(member, attraction);

// 즐겨찾기를 이미 눌렀다면
if(save.isPresent()){
// 즐겨찾기 데이터를 삭제하고
saveRepository.delete(save.get());
// 명소의 saves를 하나 감소시킨다
attraction.setSaves(attraction.getSaves()-1);
// 지금은 즐겨찾기를 누르지 않은 상태라는것을 반환한다.
return false;
}
// 즐겨찾기를 누르지 않았으면
else{
// 즐겨찾기 데이터를 생성하고
saveRepository.save(Save.builder().attraction(attraction).member(member).build());
// 명소의 saves를 하나 증가시킨다.
attraction.setSaves(attraction.getSaves()+1);
// 지금은 즐겨찾기를 누른 상태라는것을 반환한다.
return true;
}
}

public boolean isSaved(Member member, Attraction attraction) {
Optional<Save> save = saveRepository.findByMemberAndAttraction(member, attraction);
return save.isPresent();
}

private Attraction findVerifiedAttraction(long attractionId){
return attractionRepository.findById(attractionId)
.orElseThrow(()-> new BusinessLogicException(ExceptionCode.ATTRACTION_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.main36.picha.domain.member.dto;

import com.main36.picha.domain.post.entity.Post;
import com.main36.picha.domain.save.entity.Save;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Data
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import com.main36.picha.domain.save.entity.Save;
import com.main36.picha.global.audit.Auditable;
import lombok.*;
import org.hibernate.annotations.DynamicInsert;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.main36.picha.domain.save.repository;

import com.main36.picha.domain.attraction.entity.Attraction;
import com.main36.picha.domain.attraction_likes.entity.AttractionLikes;
import com.main36.picha.domain.member.entity.Member;
import com.main36.picha.domain.save.entity.Save;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface SaveRepository extends JpaRepository<Save, Long> {
Optional<Save> findByMemberAndAttraction(Member member, Attraction attraction);
}

This file was deleted.

0 comments on commit aa58485

Please sign in to comment.