Skip to content

Commit

Permalink
Merge pull request #24 from team-nabi/NABI-124--beommo-feat--jjim-CD
Browse files Browse the repository at this point in the history
NABI-124--찜 생성, 삭제, 나의 찜 목록 조회 구현
  • Loading branch information
BeommoKoo-dev authored Nov 9, 2023
2 parents cd6a139 + c6ce36b commit 97bb32e
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import lombok.RequiredArgsConstructor;

import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.*;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO;
import org.prgrms.nabimarketbe.domain.card.entity.CardStatus;
import org.prgrms.nabimarketbe.domain.card.service.CardService;
import org.prgrms.nabimarketbe.domain.category.entity.CategoryEnum;
Expand All @@ -16,6 +18,8 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import lombok.RequiredArgsConstructor;

import java.util.List;

@RestController
Expand Down Expand Up @@ -47,7 +51,6 @@ public ResponseEntity<SingleResult<CardCreateResponseDTO>> createCard(

@GetMapping("/{cardId}")
public ResponseEntity<SingleResult<CardSingleReadResponseDTO>> getCardById(@PathVariable Long cardId) {
cardService.updateViews(cardId);
CardSingleReadResponseDTO cardSingleReadResponseDTO = cardService.getCardById(cardId);

return ResponseEntity.ok(ResponseFactory.getSingleResult(cardSingleReadResponseDTO));
Expand Down
42 changes: 32 additions & 10 deletions src/main/java/org/prgrms/nabimarketbe/domain/card/entity/Card.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package org.prgrms.nabimarketbe.domain.card.entity;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.prgrms.nabimarketbe.domain.item.entity.Item;
import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.prgrms.nabimarketbe.global.BaseEntity;
import org.prgrms.nabimarketbe.domain.item.entity.Item;
import org.prgrms.nabimarketbe.global.error.BaseException;
import org.prgrms.nabimarketbe.global.error.ErrorCode;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table
@Table(name = "cards")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Card extends BaseEntity {
Expand Down Expand Up @@ -109,4 +122,13 @@ public void updateThumbNailImage(String url) {
public void updateViewCount() {
this.viewCount += 1;
}

// TODO : dibCount 동시성
public void increaseDibCount() {
this.dibCount += 1;
}

public void decreaseDibCount() {
this.dibCount -= 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.prgrms.nabimarketbe.domain.card.service;

import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;

import org.prgrms.nabimarketbe.domain.card.dto.request.CardCreateRequestDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.*;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardCreateResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardListReadPagingResponseDTO;
import org.prgrms.nabimarketbe.domain.card.dto.response.CardSingleReadResponseDTO;
import org.prgrms.nabimarketbe.domain.card.entity.Card;
import org.prgrms.nabimarketbe.domain.card.entity.CardStatus;
import org.prgrms.nabimarketbe.domain.card.repository.CardRepository;
Expand All @@ -18,7 +21,6 @@
import org.prgrms.nabimarketbe.domain.item.entity.Item;
import org.prgrms.nabimarketbe.domain.item.entity.PriceRange;
import org.prgrms.nabimarketbe.domain.item.repository.ItemRepository;

import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.prgrms.nabimarketbe.domain.user.repository.UserRepository;
import org.prgrms.nabimarketbe.domain.user.service.CheckService;
Expand All @@ -28,8 +30,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -107,11 +108,13 @@ public CardCreateResponseDTO createCard(
);
}

@Transactional(readOnly = true)
@Transactional
public CardSingleReadResponseDTO getCardById(Long cardId) { // TODO: User 완성되면 User에 대한 정보도 추가해서 내려주기
Card card = cardRepository.findById(cardId)
.orElseThrow();

card.updateViewCount();

Item item = card.getItem();

// image 순서 맞춰서 응답 내려주는 부분
Expand Down Expand Up @@ -160,14 +163,6 @@ public CardListReadPagingResponseDTO getCardsByCondition(
);
}

@Transactional
public void updateViews(Long cardId) {
Card card = cardRepository.findById(cardId)
.orElseThrow(() -> new RuntimeException("해당 카드가 존재하지 않습니다."));

card.updateViewCount();
}

@Transactional(readOnly = true)
public CardListResponseDTO<SuggestionAvailableCardResponseDTO> getSuggestionAvailableCards(
String token,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.prgrms.nabimarketbe.domain.dibs.api;

import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibCreateResponseDTO;
import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO;
import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibResponseDTO;
import org.prgrms.nabimarketbe.domain.dibs.service.DibService;
import org.prgrms.nabimarketbe.global.util.ResponseFactory;
import org.prgrms.nabimarketbe.global.util.model.CommonResult;
import org.prgrms.nabimarketbe.global.util.model.SingleResult;
import org.springframework.http.ResponseEntity;
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;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RequestMapping("/api/v1/dibs")
@RestController
public class DibController {
private final DibService dibService;

@PostMapping("/{cardId}")
public ResponseEntity<SingleResult<DibResponseDTO<DibCreateResponseDTO>>> createDib(
@RequestHeader(name = "authorization") String token,
@PathVariable Long cardId
) {
DibResponseDTO<DibCreateResponseDTO> dibResponseDTO = dibService.createDib(token, cardId);

return ResponseEntity.ok(ResponseFactory.getSingleResult(dibResponseDTO));
}

@DeleteMapping("/{cardId}")
public ResponseEntity<CommonResult> deleteDib(
@RequestHeader(name = "authorization") String token,
@PathVariable Long cardId
) {
dibService.deleteDib(token, cardId);

return ResponseEntity.ok(ResponseFactory.getSuccessResult());
}

@GetMapping
public ResponseEntity<SingleResult<DibListReadPagingResponseDTO>> getUserDibsByUserId(
@RequestHeader(name = "authorization") String token,
@RequestParam(required = false) Long cursorId,
@RequestParam Integer size
) {
DibListReadPagingResponseDTO dibListReadPagingResponseDTO = dibService.getUserDibsByUserId(
token,
cursorId,
size
);

return ResponseEntity.ok(ResponseFactory.getSingleResult(dibListReadPagingResponseDTO));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.prgrms.nabimarketbe.domain.dibs.dto.response;

import java.time.LocalDateTime;

import org.prgrms.nabimarketbe.domain.dibs.entity.Dib;

import lombok.Builder;

@Builder
public record DibCreateResponseDTO(
Long dibId,
Long cardId,
Long userId,
LocalDateTime createdAt,
LocalDateTime modifiedAt
) {
public static DibCreateResponseDTO from(Dib dib) {
return DibCreateResponseDTO.builder()
.dibId(dib.getDibId())
.userId(dib.getUser().getUserId())
.cardId(dib.getCard().getCardId())
.createdAt(dib.getCreatedDate())
.modifiedAt(dib.getModifiedDate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prgrms.nabimarketbe.domain.dibs.dto.response;

import java.util.List;

public record DibListReadPagingResponseDTO (
List<DibListReadResponseDTO> dibList,
Long nextCursorId
) {
public static DibListReadPagingResponseDTO of(
List<DibListReadResponseDTO> dibList,
Long nextCursorId
) {
return new DibListReadPagingResponseDTO(dibList, nextCursorId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.prgrms.nabimarketbe.domain.dibs.dto.response;

import org.prgrms.nabimarketbe.domain.item.entity.PriceRange;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class DibListReadResponseDTO {
private Long dibId;

private Long cardId;

private String cardTitle;

private String itemName;

private PriceRange priceRange;

private String thumbNail;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.prgrms.nabimarketbe.domain.dibs.dto.response;

public record DibResponseDTO<T>(
T dibInfo
) {
}
46 changes: 46 additions & 0 deletions src/main/java/org/prgrms/nabimarketbe/domain/dibs/entity/Dib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.prgrms.nabimarketbe.domain.dibs.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.prgrms.nabimarketbe.domain.card.entity.Card;
import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.prgrms.nabimarketbe.global.BaseEntity;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "dibs")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Dib extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "dib_id", nullable = false)
private Long dibId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "card_id", nullable = false)
private Card card;

public Dib(
User user,
Card card
) {
this.user = user;
this.card = card;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prgrms.nabimarketbe.domain.dibs.repository;

import java.util.Optional;

import org.prgrms.nabimarketbe.domain.card.entity.Card;
import org.prgrms.nabimarketbe.domain.dibs.entity.Dib;
import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DibRepository extends JpaRepository<Dib, Long>, DibRepositoryCustom{
Optional<Dib> findDibByUserAndCard(User user, Card card);

boolean existsDibByCardAndUser(Card card, User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.prgrms.nabimarketbe.domain.dibs.repository;

import org.prgrms.nabimarketbe.domain.dibs.dto.response.DibListReadPagingResponseDTO;

public interface DibRepositoryCustom {
DibListReadPagingResponseDTO getUserDibsByUserId(
Long userId,
Long cursorId,
Integer size
);
}
Loading

0 comments on commit 97bb32e

Please sign in to comment.