Skip to content

Commit

Permalink
feat : 공지사항 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
lreowy committed Aug 20, 2024
1 parent 050fba8 commit b616b9b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.pickple.server.api.notice.dto.response.NoticeListGetByMoimResponse;
import com.pickple.server.api.notice.service.NoticeCommandService;
import com.pickple.server.api.notice.service.NoticeQueryService;
import com.pickple.server.global.common.annotation.HostId;
import com.pickple.server.global.response.ApiResponseDto;
import com.pickple.server.global.response.enums.SuccessCode;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -36,4 +38,11 @@ public ApiResponseDto<List<NoticeListGetByMoimResponse>> getNoticeListByMoimId(@
return ApiResponseDto.success(SuccessCode.NOTICE_LIST_GET_SUCCESS,
noticeQueryService.getNoticeListByMoimId(moimId));
}

@DeleteMapping("/v2/notice/{noticeId}")
public ApiResponseDto deleteNotice(@HostId Long hostId,
@PathVariable Long noticeId) {
noticeCommandService.deleteNotice(hostId, noticeId);
return ApiResponseDto.success(SuccessCode.NOTICE_DELETE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.pickple.server.api.notice.repository;

import com.pickple.server.api.notice.domain.Notice;
import com.pickple.server.global.exception.CustomException;
import com.pickple.server.global.response.enums.ErrorCode;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface NoticeRepository extends JpaRepository<Notice, Long> {

List<Notice> findNoticeByMoimIdOrderByCreatedAtDesc(Long moimId);

Optional<Notice> findNoticeById(Long id);

default Notice findNoticeByIdOrThrow(Long id) {
return findNoticeById(id)
.orElseThrow(() -> new CustomException(ErrorCode.NOTICE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.pickple.server.api.notice.domain.Notice;
import com.pickple.server.api.notice.dto.request.NoticeCreateRequest;
import com.pickple.server.api.notice.repository.NoticeRepository;
import com.pickple.server.global.exception.CustomException;
import com.pickple.server.global.response.enums.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -29,4 +31,12 @@ public void createNotice(Long moimId, NoticeCreateRequest request) {

noticeRepository.save(notice);
}

public void deleteNotice(Long hostId, Long noticeId) {
Notice notice = noticeRepository.findNoticeByIdOrThrow(noticeId);
if (!hostId.equals(notice.getMoim().getHost().getId())) {
throw new CustomException(ErrorCode.NOT_HOST);
}
noticeRepository.delete(notice);
}
}

0 comments on commit b616b9b

Please sign in to comment.