Skip to content

Commit

Permalink
Merge pull request #127 from HelloWorld-AICC/feat#126
Browse files Browse the repository at this point in the history
내가 작성한 글 조회api
  • Loading branch information
jjjuhoon authored Nov 15, 2024
2 parents f190409 + 3fcbc72 commit caef3f1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

import com.example.helloworldmvc.apiPayload.GeneralException;
import com.example.helloworldmvc.apiPayload.code.status.ErrorStatus;
import com.example.helloworldmvc.domain.Center;
import com.example.helloworldmvc.domain.File;
import com.example.helloworldmvc.domain.Summary;
import com.example.helloworldmvc.domain.User;
import com.example.helloworldmvc.domain.*;
import com.example.helloworldmvc.domain.mapping.Reservation;
import com.example.helloworldmvc.repository.UserRepository;
import com.example.helloworldmvc.web.dto.CenterResponseDTO;
import com.example.helloworldmvc.web.dto.MyPageRequestDTO;
import com.example.helloworldmvc.web.dto.MyPageResponseDTO;
import org.springframework.data.domain.Page;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -111,4 +104,22 @@ public static MyPageResponseDTO.AllReservationListRes toAllReservationListRes(Pa
.allReservationList(allReservationRes)
.build();
}



public static MyPageResponseDTO.MyCommunityResDTO toMyCommunityRes(Community community) {
return MyPageResponseDTO.MyCommunityResDTO.builder()
.communityId(community.getId())
.title(community.getTitle())
.uploadedAt(community.getCreatedAt())
.category(community.getCommunityCategory().name())
.build();
}

public static MyPageResponseDTO.MyCommunityListResDTO toAllMyCommunityListRes(Page<MyPageResponseDTO.MyCommunityResDTO> communityList, Long userId) {
return MyPageResponseDTO.MyCommunityListResDTO.builder()
.userId(userId)
.allMyCommunityList(communityList.getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.helloworldmvc.repository;

import com.example.helloworldmvc.domain.Community;
import com.example.helloworldmvc.domain.User;
import com.example.helloworldmvc.domain.enums.CommunityCategory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CommunityRepository extends JpaRepository<Community, Long> {
Page<Community> findAllByCommunityCategory(CommunityCategory category, Pageable pageable);

Page<Community> findAllByUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.example.helloworldmvc.service;

import com.example.helloworldmvc.domain.Center;
import com.example.helloworldmvc.domain.Summary;
import com.example.helloworldmvc.domain.User;
import com.example.helloworldmvc.domain.mapping.Reservation;
import com.example.helloworldmvc.web.dto.MyPageRequestDTO;
import com.example.helloworldmvc.web.dto.MyPageResponseDTO;
import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;

public interface MyPageService {
User getUser(String userId);
Expand All @@ -17,4 +16,7 @@ public interface MyPageService {
void setUserProfile(String gmail, MyPageRequestDTO.PatchProfile request);

String deactivateUser(String userId);

MyPageResponseDTO.MyCommunityListResDTO getCommunityList(String userId, Integer page, Integer size);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.example.helloworldmvc.apiPayload.GeneralException;
import com.example.helloworldmvc.apiPayload.code.status.ErrorStatus;
import com.example.helloworldmvc.apiPayload.handler.UserHandler;
import com.example.helloworldmvc.domain.Center;
import com.example.helloworldmvc.domain.File;
import com.example.helloworldmvc.domain.Summary;
import com.example.helloworldmvc.domain.User;
import com.example.helloworldmvc.converter.MyPageConverter;
import com.example.helloworldmvc.domain.*;
import com.example.helloworldmvc.domain.mapping.Reservation;
import com.example.helloworldmvc.repository.*;
import com.example.helloworldmvc.web.dto.MyPageRequestDTO;
import com.example.helloworldmvc.web.dto.MyPageResponseDTO;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

import static com.example.helloworldmvc.apiPayload.code.status.ErrorStatus.USER_NOT_FOUND;
Expand All @@ -31,6 +31,7 @@ public class MyPageServiceImpl implements MyPageService{
private final ReservationRepository reservationRepository;
private final FileRepository fileRepository;
private final S3Service s3Service;
private final CommunityRepository communityRepository;



Expand Down Expand Up @@ -88,4 +89,16 @@ public String deactivateUser(String userId) {
userRepository.save(user);
return userId+" 유저가 삭제 되었습니다";
}

@Override
public MyPageResponseDTO.MyCommunityListResDTO getCommunityList(String userId, Integer page, Integer size) {
User user = userRepository.findByEmail(userId)
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));

PageRequest pageRequest = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<Community> communityPage = communityRepository.findAllByUserId(user.getId(), pageRequest);
Page<MyPageResponseDTO.MyCommunityResDTO> communityResPage = communityPage.map(MyPageConverter::toMyCommunityRes);

return MyPageConverter.toAllMyCommunityListRes(communityResPage, user.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,22 @@ public ApiResponse<String> deactivateUser(@RequestHeader(name = "Authorization")
String userId = jwtTokenProvider.getGoogleEmail(accessToken);
return ApiResponse.onSuccess(myPageService.deactivateUser(userId));
}

@GetMapping("/AllMyCommunity")
@Operation(summary = "내가 작성한 전체 글 조회 API", description = "내가 작성한 전체 글 조회 API 화면 API입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4001", description = "사용자를 찾을수 없습니다.")
})
@Parameters({
@Parameter(name = "Authorization", description = "RequestHeader - 로그인한 사용자 토큰"),
@Parameter(name = "page", description = "query string(RequestParam) - 몇번째 페이지인지 가리키는 page 변수 입니다! (0부터 시작)"),
@Parameter(name = "size", description = "query string(RequestParam) - 몇 개씩 불러올지 개수를 세는 변수입니다. (1 이상 자연수로 설정)"),
})
public ApiResponse<?> getAllMyCommunity(@RequestHeader("Authorization") String accessToken,
@RequestParam(name = "page") Integer page,
@RequestParam(name = "size") Integer size) {
String gmail = jwtTokenProvider.getGoogleEmail(accessToken);
return ApiResponse.onSuccess(myPageService.getCommunityList(gmail, page, size));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.example.helloworldmvc.web.dto;

import com.example.helloworldmvc.domain.File;
import com.example.helloworldmvc.domain.User;
import com.example.helloworldmvc.domain.enums.SummaryStatus;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -88,4 +84,23 @@ public static class AllReservationRes{
public static class PatchProfileEmail{
String email;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MyCommunityListResDTO {
Long userId;
List<MyCommunityResDTO> allMyCommunityList;
}
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MyCommunityResDTO {
Long communityId;
String title;
LocalDateTime uploadedAt;
String category;
}
}

0 comments on commit caef3f1

Please sign in to comment.