Skip to content

Commit

Permalink
🥕Feat #25: [가계부 공유] 게시글 상세 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
DDonghyeo committed Jul 23, 2023
1 parent a87f1a6 commit d1ca7ca
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ public ResponseEntity<?> searchAll(@RequestParam("keyword") String keyword, @Req
* @param RequestDto
*/

@PostMapping("")
public ResponseEntity<?> createPost(@RequestBody SharingDto.Request req) {
accountBookSharingService.createPost(req);
return new ResponseEntity<>(HttpStatus.OK);
}



/*
* [가계부 공유] 게시글 상세조회
*
* @PathVariable accountBookId
*/
@GetMapping("/{accountBookId}")
public ResponseEntity<?> getPost(@PathVariable("accountBookId") int board_id) {
return ResponseEntity.ok(accountBookSharingService.getBoard(board_id));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.umc.DongnaeFriend.domain.account.sharing.dto;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard;
import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard;
import com.umc.DongnaeFriend.domain.type.DongnaeBoardCategory;
import com.umc.DongnaeFriend.domain.type.SharingCategory;
import com.umc.DongnaeFriend.domain.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -21,6 +27,34 @@ public static class Request {
private String content;

private List<String> images;

public SharingBoard toEntity(User user) {
return SharingBoard.builder()
.user(user)
.category(SharingCategory.valueOf(category))
.title(title)
.content(content)
.view(0)
.build();
}
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class Response{
String profileImage;
String nickname;
int category;
String title;
String content;
List<String> images;
String createdAt;
boolean isWriter;
boolean likeOrNot;
boolean scrapOrNot;
int view;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingImg;
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeImg;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface SharingImgRepository extends JpaRepository<SharingImg, Long> {

@Query(value = "SELECT * FROM sharing_img WHERE sharing_board_id = ?1 ORDER BY created_at ASC LIMIT 1", nativeQuery = true)
Optional<SharingImg> findFirst(long sharing_board_id);

List<SharingImg> findAllBySharingBoard_Id(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public interface AccountBookSharingService {
List<SharingDto.ListResponse> searchByKeyword(String keyword, int category, Pageable pageable);

void createPost(SharingDto.Request req);

/*
* [가계부 공유] 게시글 상세 조회
*/
SharingDto.Response getBoard(long board_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.umc.DongnaeFriend.global.util.TimeUtil.getTime;
Expand All @@ -32,7 +34,7 @@ public class AccountBookSharingServiceImpl implements AccountBookSharingService

//임시 유저
Dongnae dongnae = Dongnae.builder().id(1L).gu("서울구").dong("서울동").city("서울시").townName("무슨마을").build();
User user = User.builder().id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build();
User user = User.builder().profileImage("profileImage").id(1L).age(Age.AGE10).email("email").dongnae(dongnae).gender(Gender.FEMALE).infoCert(YesNo.NO).townCert(YesNo.NO).townCertCnt(10).id(1L).kakaoId(90L).nickname("nickname").refreshToken("refreshToken").build();

@Autowired
private SharingBoardRepository sharingBoardRepository;
Expand Down Expand Up @@ -66,16 +68,49 @@ public List<SharingDto.ListResponse> searchByKeyword(String keyword, int categor
*/
@Override
public void createPost(SharingDto.Request req) {
sharingBoardRepository.save(SharingBoard.builder()
.category(SharingCategory.valueOf(req.getCategory()))
.title(req.getTitle())
.content(req.getContent())
.build()
);
sharingBoardRepository.save(req.toEntity(user));
//TODO : Img 파일 업로드
}


/*
* [가계부 공유] 게시글 상세 조회
*/
@Override
public SharingDto.Response getBoard(long board_id) {
//TODO : User 식별자 필요.
Optional<SharingBoard> board = sharingBoardRepository.findById(board_id);
if (board.isEmpty()) {
throw new CustomException(ErrorCode.NO_CONTENT_FOUND);
}

//Get Images
List<SharingImg> images = sharingImgRepository.findAllBySharingBoard_Id(board_id);

//Writer인지 검사
boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId());

//LikeOrNot 검사
boolean likeOrNot = !sharingSympathyRepository.findByUser_Id(user.getId()).isEmpty();

//TODO: ScrapRepository 필요
//scrapOrNot 검사
boolean scrapOrNot = false;

return SharingDto.Response.builder()
.profileImage(user.getProfileImage())
.nickname(user.getNickname())
.category(board.get().getCategory().getValue())
.title(board.get().getTitle())
.content(board.get().getContent())
.images(images.stream().map(SharingImg::getImageUrl).collect(Collectors.toList()))
.createdAt(getTime(board.get().getCreatedAt()))
.isWriter(isWriter)
.likeOrNot(likeOrNot)
.scrapOrNot(scrapOrNot)
.view(board.get().getView()).build();
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static class Response {

private boolean likeOrNot;

private boolean ScrapOrNot;
private boolean scrapOrNot;

private int view;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public void createBoard(DongnaeBoardDto.Request req) {
@Transactional(propagation = Propagation.REQUIRED)
public DongnaeBoardDto.Response getBoard(long board_id) {
//TODO : User 식별자 필요.
int user_id = 1;
Optional<DongnaeBoard> board = dongnaeBoardRepository.findById(board_id);
if (board.isEmpty()) {
throw new RuntimeException();
Expand All @@ -140,10 +139,10 @@ public DongnaeBoardDto.Response getBoard(long board_id) {
List<DongnaeImg> images = dongnaeImgRepository.findAllByDongnaeBoard_Id(board_id);

//Writer인지 검사
boolean isWriter = board.get().getUser().getId() == user_id;
boolean isWriter = Objects.equals(board.get().getUser().getId(), user.getId());

//LikeOrNot 검사
boolean likeOrNot = !dongnaeSympathyRepository.findByUser_Id(user_id).isEmpty();
boolean likeOrNot = !dongnaeSympathyRepository.findByUser_Id(user.getId()).isEmpty();

//TODO: ScrapRepository 필요
//scrapOrNot 검사
Expand All @@ -163,7 +162,7 @@ public DongnaeBoardDto.Response getBoard(long board_id) {
.townCertification(user.getTownCertCnt())
.isWriter(isWriter)
.likeOrNot(likeOrNot)
.ScrapOrNot(scrapOrNot)
.scrapOrNot(scrapOrNot)
.view(board.get().getView()).build();
}

Expand Down

0 comments on commit d1ca7ca

Please sign in to comment.