Skip to content

Commit

Permalink
Merge pull request #194 from prgrms-web-devcourse-final-project/develop
Browse files Browse the repository at this point in the history
[merge] main 브랜치 워크플로우 작동
  • Loading branch information
DongWooKim4343 authored Dec 5, 2024
2 parents 0847c63 + e91d265 commit 0ebffad
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
import com.mallangs.domain.board.entity.BoardStatus;
import com.mallangs.domain.board.entity.BoardType;
import com.mallangs.domain.board.service.BoardService;
import com.mallangs.domain.member.entity.embadded.UserId;
import com.mallangs.global.exception.ErrorCode;
import com.mallangs.global.exception.MallangsCustomException;
import com.mallangs.global.jwt.entity.CustomMemberDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

@Tag(name = "커뮤니티 & 실종신고-목격제보 API", description = "커뮤니티/실종신고-목격제보 관련 API")
@RestController
Expand All @@ -35,16 +37,19 @@ public class BoardController {
@PostMapping("/community")
public ResponseEntity<Long> createCommunity(@Valid @RequestBody CommunityCreateRequest request,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails) {
if (customMemberDetails == null) throw new MallangsCustomException(ErrorCode.LOGIN_REQUIRED);
Long boardId = boardService.createCommunityBoard(request, customMemberDetails.getMemberId());
return ResponseEntity.created(URI.create("/api/board/community/" + boardId)).body(boardId);
}

@Operation(summary = "커뮤니티 게시글 전체 조회", description = "커뮤니티 게시글을 모두 조회합니다.")
@GetMapping("/community/")
public ResponseEntity<PageResponse<CommunityListResponse>> getAllCommunity(@RequestParam(defaultValue = "1") int page) {
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getAllCommunityBoard(pageRequest)));
}

@Operation(summary = "커뮤니티 게시글 키워드 조회", description = "커뮤니티 게시글을 키워드로 조회합니다.")
@GetMapping("/community/keyword")
public ResponseEntity<PageResponse<CommunityListResponse>> searchCommunityPosts(
@RequestParam String keyword,
Expand All @@ -54,39 +59,43 @@ public ResponseEntity<PageResponse<CommunityListResponse>> searchCommunityPosts(
return ResponseEntity.ok(PageResponse.from(boardService.searchCommunityBoards(keyword, pageRequest)));
}

@GetMapping("/community/member/{memberId}")
@Operation(summary = "커뮤니티 게시글 상세 조회(By 회원 ID)", description = "커뮤니티 게시글을 회원 ID로 조회합니다.")
@GetMapping("/community/member/{stringUserId}")
public ResponseEntity<PageResponse<CommunityListResponse>> getMemberCommunityPosts(
@PathVariable Long memberId,
@PathVariable String stringUserId,
@RequestParam(defaultValue = "1") int page
) {
UserId userId = new UserId(stringUserId);
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getMemberCommunityBoards(memberId, pageRequest)));
return ResponseEntity.ok(PageResponse.from(boardService.getMemberCommunityBoards(userId, pageRequest)));
}

@GetMapping("/community/category/{categoryId}")
public ResponseEntity<PageResponse<CommunityListResponse>> getCommunityPostByCategory(
@PathVariable Long categoryId,
@RequestParam(defaultValue = "1") int page
) {
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getCommunityBoardsByCategory(categoryId, pageRequest)));
}

@Operation(summary = "커뮤니티 게시글 상세 조회", description = "특정 커뮤니티 게시글의 상세 내용을 조회합니다.")
@Operation(summary = "커뮤니티 게시글 상세 조회(By 게시글 번호)", description = "커뮤니티 게시글을 게시글 번호로 조회합니다.")
@GetMapping("/community/{boardId}")
public ResponseEntity<CommunityDetailResponse> getCommunityPost(
@Parameter(description = "게시글 ID") @PathVariable Long boardId
) {
return ResponseEntity.ok(boardService.getCommunityBoard(boardId));
}

@Operation(summary = "커뮤니티 게시글 수정", description = "기존 커뮤니티 게시글을 수정합니다.")
@Operation(summary = "커뮤니티 게시글 상세 조회(By 카테고리 이름)", description = "커뮤니티 게시글을 카테고리 이름으로 검색합니다.")
@GetMapping("/community/category/{categoryName}")
public ResponseEntity<PageResponse<CommunityListResponse>> getCommunityPostByCategory(
@PathVariable String categoryName,
@RequestParam(defaultValue = "1") int page
) {
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getCommunityBoardsByCategory(categoryName, pageRequest)));
}

@Operation(summary = "커뮤니티 특정 게시글 수정", description = "특정 커뮤니티 게시글을 수정합니다.")
@PutMapping("/community/{boardId}")
public ResponseEntity<Void> updateCommunityPost(
@PathVariable Long boardId,
@Valid @RequestBody CommunityUpdateRequest request,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails
) {
if (customMemberDetails == null) throw new MallangsCustomException(ErrorCode.LOGIN_REQUIRED);
boardService.updateCommunityBoard(boardId, request, customMemberDetails.getMemberId());
return ResponseEntity.noContent().build();
}
Expand All @@ -98,16 +107,19 @@ public ResponseEntity<Long> createSightingPost(
@Valid @RequestBody SightingCreateRequest request,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails
) {
if (customMemberDetails == null) throw new MallangsCustomException(ErrorCode.LOGIN_REQUIRED);
Long boardId = boardService.createSightingBoard(request, customMemberDetails.getMemberId());
return ResponseEntity.created(URI.create("/api/board/sighting/" + boardId)).body(boardId);
}

@Operation(summary = "실종신고-목격제보 게시글 전체 조회", description = "실종신고-목격제보 게시글을 모두 조회합니다.")
@GetMapping("/sighting")
public ResponseEntity<PageResponse<SightingListResponse>> getAllSighting(@RequestParam(defaultValue = "1") int page) {
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getAllSightingBoard(pageRequest)));
}

@Operation(summary = "실종신고-목격제보 게시글 키워드 조회", description = "실종신고-목격제보 게시글을 키워드로 조회합니다.")
@GetMapping("/sighting/keyword")
public ResponseEntity<PageResponse<SightingListResponse>> searchSightingPosts(
@RequestParam String keyword,
Expand All @@ -117,28 +129,32 @@ public ResponseEntity<PageResponse<SightingListResponse>> searchSightingPosts(
return ResponseEntity.ok(PageResponse.from(boardService.searchSightingBoards(keyword, pageRequest)));
}

@GetMapping("/sighting/member/{memberId}")
@Operation(summary = "실종신고-목격제보 게시글 상세 조회(By 회원 ID)", description = "실종신고-목격제보 게시글을 회원 ID로 조회합니다.")
@GetMapping("/sighting/member/{stringUserId}")
public ResponseEntity<PageResponse<SightingListResponse>> getMemberSightingPosts(
@PathVariable Long memberId,
@PathVariable String stringUserId,
@RequestParam(defaultValue = "1") int page
) {
UserId userId = new UserId(stringUserId);
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getMemberSightingBoards(memberId, pageRequest)));
return ResponseEntity.ok(PageResponse.from(boardService.getMemberSightingBoards(userId, pageRequest)));
}

@Operation(summary = "실종신고-목격제보 게시글 상세 조회(By 게시글 번호)", description = "실종신고-목격제보 게시글을 게시글 번호로 조회합니다.")
@GetMapping("/sighting/{boardId}")
public ResponseEntity<SightingDetailResponse> getSightingPost(
@Parameter(description = "게시글 ID")@PathVariable Long boardId) {
return ResponseEntity.ok(boardService.getSightingBoard(boardId));
}

@GetMapping("/sighting/category/{categoryId}")
@Operation(summary = "실종신고-목격제보 게시글 상세 조회(By 카테고리 이름)", description = "실종신고-목격제보 게시글을 카테고리 이름으로 검색합니다.")
@GetMapping("/sighting/category/{categoryName}")
public ResponseEntity<PageResponse<SightingListResponse>> getSightingPostsByCategory(
@PathVariable Long categoryId,
@PathVariable String categoryName,
@RequestParam(defaultValue = "1") int page
) {
PageRequest pageRequest = PageRequest.of(page - 1, 10);
return ResponseEntity.ok(PageResponse.from(boardService.getSightingBoardsByCategory(categoryId, pageRequest)));
}

@Operation(summary = "실종신고-목격제보 게시글 상세 조회", description = "특정 실종신고-목격제보 게시글의 상세 내용을 조회합니다.")
@GetMapping("/sighting/{boardId}")
public ResponseEntity<SightingDetailResponse> getSightingPost(@PathVariable Long boardId) {
return ResponseEntity.ok(boardService.getSightingBoard(boardId));
return ResponseEntity.ok(PageResponse.from(boardService.getSightingBoardsByCategory(categoryName, pageRequest)));
}

@Operation(summary = "실종신고-목격제보 게시글 수정", description = "기존 실종신고-목격제보 게시글을 수정합니다.")
Expand All @@ -148,6 +164,7 @@ public ResponseEntity<Void> updateSightingPost(
@Valid @RequestBody SightingUpdateRequest request,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails
) {
if (customMemberDetails == null) throw new MallangsCustomException(ErrorCode.LOGIN_REQUIRED);
boardService.updateSightingBoard(boardId, request, customMemberDetails.getMemberId());
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@
import com.mallangs.domain.board.dto.response.CategoryResponse;
import com.mallangs.domain.board.entity.CategoryStatus;
import com.mallangs.domain.board.service.CategoryService;
import com.mallangs.global.jwt.entity.CustomMemberDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

@Tag(name = "카테고리 API", description = "카테고리 관리 API")
@Tag(name = "카테고리 API", description = "관리자가 좋아하는 카테고리 API")
@RestController
@RequestMapping("/api/v1/category")
@RequiredArgsConstructor
Expand All @@ -32,14 +25,14 @@ public class CategoryController {

private final CategoryService categoryService;

@Operation(summary = "카테고리 생성 - 관리자 권한", description = "새로운 카테고리를 생성합니다.")
@Operation(summary = "카테고리 생성", description = "새로운 카테고리를 생성합니다.")
@PostMapping
public ResponseEntity<CategoryResponse> createCategory(@Valid @RequestBody CategoryCreateRequest request) {
CategoryResponse response = categoryService.createCategory(request);
return ResponseEntity.created(URI.create("/api/category/" + response.getCategoryId())).body(response);
}

@Operation(summary = "카테고리 수정 - 관리자 권한", description = "기존 카테고리를 수정합니다.")
@Operation(summary = "카테고리 수정", description = "기존 카테고리를 수정합니다.")
@PutMapping("/{categoryId}")
public ResponseEntity<CategoryResponse> updateCategory(
@PathVariable Long categoryId,
Expand All @@ -48,36 +41,32 @@ public ResponseEntity<CategoryResponse> updateCategory(
return ResponseEntity.ok(categoryService.updateCategory(categoryId, request));
}

@Operation(summary = "카테고리 상태 변경 - 관리자 권한", description = "요청 형태: PATCH /api/categories/{카테고리ID}/status?status={상태}")
@Operation(summary = "카테고리 상태 변경", description = "요청 형태: PATCH /api/categories/{카테고리ID}/status?status={상태}")
@PatchMapping("/{categoryId}/status")
public ResponseEntity<Void> changeCategoryStatus(@PathVariable Long categoryId, @RequestBody CategoryStatus status) {
categoryService.changeCategoryStatus(categoryId, status);
return ResponseEntity.noContent().build();
}

@Operation(summary = "카테고리 순서 변경 - 관리자 권한", description = "요청 형태: PATCH /api/category/{카테고리ID}/order?order={순서}")
@Operation(summary = "카테고리 순서 변경", description = "요청 형태: PATCH /api/category/{카테고리ID}/order?order={순서}")
@PatchMapping("/{categoryId}/order")
public ResponseEntity<Void> changeCategoryOrder(@PathVariable Long categoryId, @RequestParam(name = "order") int newOrder) {
categoryService.changeCategoryOrder(categoryId, newOrder);
return ResponseEntity.noContent().build();
}

@Operation(summary = "카테고리 검색 - 관리자 권한", description = "요청 형태: GET /api/category/search?name={카테고리 이름}")
@GetMapping("/search")
public ResponseEntity<List<CategoryResponse>> searchCategories(
@RequestParam String name,
@AuthenticationPrincipal CustomMemberDetails customMemberDetails
) {
boolean isAdmin = customMemberDetails.getAuthorities().stream().anyMatch(a -> a.getAuthority().equals("ROLE_ADMIN"));
return ResponseEntity.ok(categoryService.searchCategoriesByName(name, isAdmin));
}

@Operation(summary = "카테고리 전체 조회", description = "활성화된 모든 카테고리를 조회합니다.")
@Operation(summary = "카테고리 전체 조회", description = "모든 카테고리를 조회합니다.")
@GetMapping
public ResponseEntity<List<CategoryResponse>> getAllCategories() {
return ResponseEntity.ok(categoryService.getAllActiveCategories());
}

@Operation(summary = "카테고리 키워드로 조회", description = "요청 형태: GET /api/category/search?name={카테고리 이름}")
@GetMapping("/search")
public ResponseEntity<List<CategoryResponse>> searchCategories(@RequestParam String name) {
return ResponseEntity.ok(categoryService.searchCategoriesByName(name));
}

@Operation(summary = "특정 카테고리 조회", description = "ID로 특정 카테고리를 조회합니다.")
@GetMapping("/{categoryId}")
public ResponseEntity<CategoryResponse> getCategory(@PathVariable Long categoryId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mallangs.domain.board.entity.Board;
import com.mallangs.domain.board.entity.BoardStatus;
import com.mallangs.domain.board.entity.BoardType;
import com.mallangs.domain.member.entity.embadded.UserId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -20,23 +21,23 @@ public interface BoardRepository extends JpaRepository<Board, Long> {
""")
Page<Board> findAllByBoardType(@Param("boardType") BoardType boardType, Pageable pageable);

// 카테고리별 게시글 목록 조회 (최신순으로 정렬)
// 카테고리 이름으로 게시글 조회
@Query("""
SELECT b FROM Board b WHERE b.category.categoryId = :categoryId AND b.boardStatus = 'PUBLISHED' AND b.boardType = :boardType ORDER BY b.createdAt DESC
SELECT b FROM Board b WHERE b.category.name = :name AND b.boardStatus = 'PUBLISHED' AND b.boardType = :boardType ORDER BY b.createdAt DESC
""")
Page<Board> findByCategoryId(@Param("categoryId") Long categoryId, BoardType boardType, Pageable pageable);
Page<Board> findByCategoryName(@Param("name") String name, BoardType boardType, Pageable pageable);

// 키워드로 통합 검색 (제목 + 내용)
@Query("""
SELECT b FROM Board b WHERE (b.title LIKE %:keyword% OR b.content LIKE %:keyword%) AND b.boardStatus = 'PUBLISHED' AND b.boardType = :boardType ORDER BY b.createdAt DESC
""")
Page<Board> searchByTitleOrContent(@Param("keyword") String keyword, @Param("boardType") BoardType boardType, Pageable pageable);

// 특정 회원이 작성한 게시글 목록 조회
// 특정 회원이 작성한 게시글 회원 ID로 목록 조회
@Query("""
SELECT b FROM Board b WHERE b.member.memberId = :memberId AND b.boardStatus = 'PUBLISHED' AND b.boardType = :boardType ORDER BY b.createdAt DESC
SELECT b FROM Board b WHERE b.member.userId = :userId AND b.boardStatus = 'PUBLISHED' AND b.boardType = :boardType ORDER BY b.createdAt DESC
""")
Page<Board> findByMemberId(@Param("memberId") Long memberId, @Param("boardType") BoardType boardType, Pageable pageable);
Page<Board> findByUserId(@Param("userId") UserId userId, @Param("boardType") BoardType boardType, Pageable pageable);

// 관리자용 - 상태별 게시글 조회
@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
// 사용자용 기능
// 활성화된 모든 카테고리 조회
@Query("""
SELECT c FROM Category c WHERE c.categoryStatus = 'ACTIVE' ORDER BY c.categoryOrder ASC
SELECT c FROM Category c ORDER BY c.categoryId ASC
""")
List<Category> findAllActiveCategories();
List<Category> findAllCategories();

// 활성화 된 카테고리 중 특정 카테고리 검색
@Query("""
Expand Down
Loading

0 comments on commit 0ebffad

Please sign in to comment.