Skip to content

Commit

Permalink
Merge pull request #22 from tomato-market/feature/14_favorite
Browse files Browse the repository at this point in the history
[Feature/14 favorite] 관심 등록
  • Loading branch information
Hyunuk17 authored Nov 10, 2023
2 parents 3e667ee + 7b42dd4 commit 627a50c
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 3 deletions.
50 changes: 50 additions & 0 deletions src/main/java/com/tomato/market/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tomato.market.data.dto.FavoriteDto;
import com.tomato.market.data.dto.ImageDto;
import com.tomato.market.data.dto.PageDto;
import com.tomato.market.data.dto.PostDto;
import com.tomato.market.data.dto.PostListResponseDto;
import com.tomato.market.data.dto.PostResponseDto;
import com.tomato.market.data.dto.ResponseDto;
import com.tomato.market.service.BoardService;

import jakarta.validation.Valid;
Expand Down Expand Up @@ -157,4 +160,51 @@ public PostResponseDto getPost(Integer postNum) { // 게시글 조회
.imageList(imageList)
.build();
}

// 관심등록
/* flow
PostDetail에서 버튼을 클릭
관심등록(좋아요 +1), 재클릭시 취소(삭제) -> 삭제말고 status로 하는게?
각 사용자당 각각 처리되어야 함
UserEntity에 ManyToOne : 다 대 일 연관관계 매핑?
별도 Table로 분리?
UserId, PostNum
*/


// 관심 등록/취소
@PostMapping("/board/favorite")
public ResponseDto<FavoriteDto> addFavorite(@RequestBody FavoriteDto favoriteDto) {
logger.info("BoardController.addFavorite() is called");

// status가 "on"이면 현재 등록된 상태
FavoriteDto result = boardService.addFavorite(favoriteDto.getUserId(), favoriteDto.getPostNum(),
favoriteDto.getStatus());
String message = "";
if (result.getStatus() == 1) {
message = "관심 등록 성공";
} else {
message = "관심 등록 취소 성공";
}
logger.info(result.toString());

// 좋아요 전체 개수를 리턴? // boardEntity 자체를 수정?
return ResponseDto.<FavoriteDto>builder().status(HttpStatus.OK).message(message).data(result)
.build();
}

// 관심 등록 확인
@GetMapping("/board/favorite")
public ResponseDto<FavoriteDto> getFavorite(String userId, Integer postNum) {
logger.info("BoardController.getFavorite() is called");

FavoriteDto favoriteDto = boardService.getFavorite(userId, postNum);
logger.info("BoardController.getFavorite() : 관심 등록 조회 성공");

return ResponseDto.<FavoriteDto>builder()
.status(HttpStatus.OK)
.message("관심 등록 확인 성공")
.data(favoriteDto)
.build();
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/tomato/market/dao/BoardDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tomato.market.data.entity.FavoriteEntity;
import com.tomato.market.data.entity.ImageEntity;
import com.tomato.market.data.entity.PostEntity;

Expand All @@ -22,4 +23,8 @@ public interface BoardDao {
PostEntity findPostByPostNum(Integer postNum);

List<ImageEntity> findImageListByPostNum(Integer postNum);

FavoriteEntity save(FavoriteEntity favoriteEntity);

FavoriteEntity findByUserIdAndPostNum(String userId, Integer postNum);
}
34 changes: 33 additions & 1 deletion src/main/java/com/tomato/market/dao/impl/BoardDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import org.springframework.transaction.annotation.Transactional;

import com.tomato.market.dao.BoardDao;
import com.tomato.market.data.entity.FavoriteEntity;
import com.tomato.market.data.entity.ImageEntity;
import com.tomato.market.data.entity.PostEntity;
import com.tomato.market.data.repository.FavoriteRepository;
import com.tomato.market.data.repository.ImageRepository;
import com.tomato.market.data.repository.PostRepository;
import com.tomato.market.handler.exception.BoardException;
Expand All @@ -26,10 +28,14 @@ public class BoardDaoImpl implements BoardDao {
private final PostRepository postRepository;
private final ImageRepository imageRepository;

private final FavoriteRepository favoriteRepository;

@Autowired
public BoardDaoImpl(PostRepository postRepository, ImageRepository imageRepository) {
public BoardDaoImpl(PostRepository postRepository, ImageRepository imageRepository,
FavoriteRepository favoriteRepository) {
this.postRepository = postRepository;
this.imageRepository = imageRepository;
this.favoriteRepository = favoriteRepository;
}


Expand Down Expand Up @@ -130,4 +136,30 @@ public List<ImageEntity> findImageListByPostNum(Integer postNum) {
return imageEntities;
}
}

@Override
public FavoriteEntity save(FavoriteEntity favoriteEntity) {
logger.info("BoardDaoImpl.save() is called");

FavoriteEntity result = favoriteRepository.save(favoriteEntity);
if (result == null) {
logger.warn("BoardDaoImpl.save() : 데이터 저장 실패");
return null;
} else {
logger.info("BoardDaoImpl.save() : 데이터 저장 성공");
return result;
}
}

@Override
public FavoriteEntity findByUserIdAndPostNum(String userId, Integer postNum) {
logger.info("BoardDaoImpl.findByUserIdAndPostNum() is called");
FavoriteEntity favoriteEntity = favoriteRepository.findByUserIdAndPostNum(userId, postNum);
if (favoriteEntity == null) {
logger.warn("BoardDaoImpl.findByUserIdAndPostNum() : 데이터 조회 실패");
return null;
}
logger.info("BoardDaoImpl.findByUserIdAndPostNum() : 데이터 조회 성공");
return favoriteEntity;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/tomato/market/data/dto/FavoriteDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.tomato.market.data.dto;

import com.tomato.market.data.entity.FavoriteEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Builder
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class FavoriteDto {
private String userId;
private Integer postNum;
private Integer status;

public static FavoriteDto toFavoriteDto(FavoriteEntity favoriteEntity) {
return FavoriteDto.builder()
.userId(favoriteEntity.getUserId())
.postNum(favoriteEntity.getPostNum())
.status(favoriteEntity.getStatus())
.build();
}

public static FavoriteEntity toFavoriteEntity(FavoriteDto favoriteDto) {
return FavoriteEntity.builder()
.userId(favoriteDto.getUserId())
.postNum(favoriteDto.getPostNum())
.status(favoriteDto.getStatus())
.build();
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/tomato/market/data/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.tomato.market.data.dto;

import org.springframework.http.HttpStatus;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class ResponseDto<T> {
private HttpStatus status;
private String message;
private T data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class UserSignUpDto {
private String name;

@NotBlank(message = "닉네임을 입력하세요.")
@Pattern(regexp = "^[ㄱ-ㅎ가-힣a-z0-9-_]{2,10}$", message = "닉네임은 특수문자를 제외한 2~10자리여야 합니다.")
@Pattern(regexp = "^[ㄱ-ㅎ가-힣a-zA-Z0-9-_]{2,10}$", message = "닉네임은 특수문자를 제외한 2~10자리여야 합니다.")
private String nickName;

@NotBlank(message = "전화번호를 입력하세요.")
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/tomato/market/data/entity/FavoriteEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.tomato.market.data.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@Table(name = "favorite")
public class FavoriteEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer favoriteNum;
private String userId;
private Integer postNum;
private Integer status; // 1 등록 0 취소
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tomato.market.data.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.tomato.market.data.entity.FavoriteEntity;

public interface FavoriteRepository extends JpaRepository<FavoriteEntity, Integer> {
FavoriteEntity findByUserIdAndPostNum(String userId, Integer postNum);
}
5 changes: 5 additions & 0 deletions src/main/java/com/tomato/market/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import com.tomato.market.data.dto.FavoriteDto;
import com.tomato.market.data.dto.ImageDto;
import com.tomato.market.data.dto.PostDto;

Expand All @@ -24,4 +25,8 @@ public interface BoardService {
PostDto getPost(Integer postNum);

List<ImageDto> getPostImageList(Integer postNum);

FavoriteDto addFavorite(String userId, Integer postNum, Integer status);

FavoriteDto getFavorite(String userId, Integer postNum);
}
53 changes: 53 additions & 0 deletions src/main/java/com/tomato/market/service/impl/BoardServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.springframework.web.multipart.MultipartFile;

import com.tomato.market.dao.BoardDao;
import com.tomato.market.data.dto.FavoriteDto;
import com.tomato.market.data.dto.ImageDto;
import com.tomato.market.data.dto.PostDto;
import com.tomato.market.data.entity.FavoriteEntity;
import com.tomato.market.data.entity.ImageEntity;
import com.tomato.market.data.entity.PostEntity;
import com.tomato.market.handler.exception.BoardException;
Expand Down Expand Up @@ -184,4 +186,55 @@ public List<ImageDto> getPostImageList(Integer postNum) {
}
return imageList;
}

@Override
public FavoriteDto addFavorite(String userId, Integer postNum, Integer status) {
logger.info("BoardServiceImpl.addFavorite() is called");
logger.info("BoardServiceImpl.addFavorite() : 관심 등록 조회");
FavoriteEntity favoriteEntity = boardDao.findByUserIdAndPostNum(userId, postNum);
FavoriteEntity result;
if (favoriteEntity == null) {
logger.info("BoardServiceImpl.addFavorite() : 등록된 관심 등록 없음");
FavoriteEntity entity = FavoriteEntity.builder().userId(userId).postNum(postNum).status(1).build();
result = boardDao.save(entity);
if (result == null) {
logger.warn("BoardServiceImpl.addFavorite() : 관심 등록 실패");
throw new BoardException("관심 등록에 실패했습니다.");

}
logger.info("BoardServiceImpl.addFavorite() : 관심 등록 성공");
} else {
if (status == 1) {
favoriteEntity.setStatus(0);
result = boardDao.save(favoriteEntity);
if (result == null) {
logger.warn("BoardServiceImpl.addFavorite() : 관심 등록 취소 실패");
throw new BoardException("관심 등록 취소에 실패했습니다.");
}
logger.info("BoardServiceImpl.addFavorite() : 관심 등록 취소 성공");
} else {
favoriteEntity.setStatus(1);
result = boardDao.save(favoriteEntity);
if (result == null) {
logger.warn("BoardServiceImpl.addFavorite() : 관심 등록 실패");
throw new BoardException("관심 등록에 실패했습니다.");
}
logger.info("BoardServiceImpl.addFavorite() : 관심 등록 성공");
}
}

return FavoriteDto.toFavoriteDto(result);
}

@Override
public FavoriteDto getFavorite(String userId, Integer postNum) {
logger.info("BoardServiceImpl.getFavorite() is called");
FavoriteEntity result = boardDao.findByUserIdAndPostNum(userId, postNum);
if (result == null) {
logger.warn("BoardServiceImpl.getFavorite() : 데이터 조회 실패");
throw new BoardException("관심 등록 조회에 실패했습니다.");
}
logger.info("BoardServiceImpl.getFavorite() : 데이터 조회 성공");
return FavoriteDto.toFavoriteDto(result);
}
}
Loading

0 comments on commit 627a50c

Please sign in to comment.