Skip to content

Commit

Permalink
✅ 버킷리스트 공개, 비공개 설정 메서드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdals4716 committed Oct 31, 2024
1 parent a1dd411 commit 934306f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,11 @@ public ResponseEntity<List<ResponseDto>> getAllBucket(@Validated @PathVariable S
public ResponseEntity<List<ResponseDto>> searchTitleAndContent(@RequestParam String keyword, @PathVariable String uid, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(bucketService.searchTitleAndContent(keyword, uid, userDetails));
}

// 특정 버킷리스트의 공개 여부 변경
@Operation(summary = "특정 버킷리스트의 공개 여부 변경")
@PostMapping("/{uid}/{bucketId}") // 요청 URL에 bucketId 포함
public ResponseEntity<ResponseDto> updateBucketVisibility(@PathVariable Long bucketId, @PathVariable String uid, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(bucketService.updateBucketVisibility(bucketId, uid, userDetails));
}
}
27 changes: 26 additions & 1 deletion src/main/java/com/example/moyeothon/Service/BucketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
Expand All @@ -22,6 +24,8 @@
@Slf4j
@Transactional
public class BucketService {

private static final Logger logger = LoggerFactory.getLogger(BucketService.class);
private final UserRepository userRepository;
private final BucketRepository bucketRepository;

Expand Down Expand Up @@ -88,7 +92,11 @@ public List<ResponseDto> getAllBucket(String uid, UserDetails userDetails) {
if (!userDetails.getUsername().equals(uid)) {
throw new RuntimeException("인증되지 않은 유저입니다.");
}
return bucketRepository.findAll().stream().map(ResponseDto::entityToDto).collect(Collectors.toList());
return bucketRepository.findAll()
.stream()
.filter(bucket -> bucket.isPublic())
.map(ResponseDto::entityToDto)
.collect(Collectors.toList());
}

// 제목, 내용 키워드별로 버킷리스트 검색하기
Expand All @@ -101,4 +109,21 @@ public List<ResponseDto> searchTitleAndContent(String keyword, String uid, UserD
.map(ResponseDto::entityToDto)
.collect(Collectors.toList());
}

// 특정 버킷리스트의 공개 여부 변경
public ResponseDto updateBucketVisibility(Long bucketId, String uid, UserDetails userDetails) {
if (!userDetails.getUsername().equals(uid)) {
throw new RuntimeException("인증되지 않은 유저입니다.");
}
BucketlistEntity bucketlist = bucketRepository.findById(bucketId)
.orElseThrow(() -> new RuntimeException("버킷리스트를 찾을 수 없습니다."));
if (!bucketlist.getUser().getUid().equals(uid)) {
throw new RuntimeException("권한이 없습니다.");
}
boolean newIsPublic = !bucketlist.isPublic();
bucketlist.setPublic(newIsPublic);
bucketRepository.save(bucketlist);
logger.info("버킷리스트 ID {}의 공개 여부가 {}로 변경되었습니다.", bucketId, newIsPublic);
return ResponseDto.entityToDto(bucketlist);
}
}

0 comments on commit 934306f

Please sign in to comment.