Skip to content

Commit

Permalink
[BE] 조회수 증가만을 위한 Partial Update 적용 (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvalentine6 committed Jul 25, 2023
2 parents dd7f32e + 03be1a3 commit efccd0a
Show file tree
Hide file tree
Showing 5 changed files with 10,584 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public ResponseDto<ItemDetailReturnDto> showItemDetail(HttpServletRequest httpSe
ItemIdxDto itemIdxDto) {
ItemDetailReturnDto itemDetailReturnDto = itemService.showItemDetail(httpServletRequest,
itemIdxDto.getItemIdx());
log.info("DTO 날짜값 : " + itemDetailReturnDto.getLastModifiedAt());
return ResponseDto.of(RESPONSE_SUCCESS, itemDetailReturnDto);
}

Expand Down
13 changes: 7 additions & 6 deletions be/src/main/java/codesquad/secondhand/entity/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.AccessLevel;
Expand All @@ -31,6 +31,7 @@
@Getter
@Entity
@Table(name = "item")
@DynamicUpdate
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
Expand Down Expand Up @@ -62,8 +63,6 @@ public class Item {
@Column(name = "posted_at")
private LocalDateTime postedAt;

@LastModifiedDate
@Setter
@Column(name = "last_modified_at")
private LocalDateTime lastModifiedAt;

Expand All @@ -83,11 +82,9 @@ public class Item {
@Column(name = "status", nullable = false)
private String status;

// Item과 ItemImage는 OneToMany 관계
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<ItemImage> itemImages = new ArrayList<>();

// Item과 Interest는 OneToMany 관계
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Interest> interests = new ArrayList<>();

Expand All @@ -103,9 +100,9 @@ public Item(Member seller, Category category, Location location, ItemImage itemI
this.price = price;
this.view = view;
this.status = status;
this.lastModifiedAt = LocalDateTime.now();
}

// Item 클래스
public void updateItem(Member seller, Category category, Location location, String name, String description, Integer price, String status) {
this.seller = seller;
if (category != null) {
Expand All @@ -127,6 +124,10 @@ public void updateItem(Member seller, Category category, Location location, Stri
this.status = status;
}
}
public void updateView(Integer view) {
this.view = view;
}


@Override
public String toString() {
Expand Down
15 changes: 8 additions & 7 deletions be/src/main/java/codesquad/secondhand/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,22 @@ public void editItem(Long memberIdx, Long itemIdx, ItemDetailDto itemDetailDto)
itemToEdit.updateItem(member, category, location, itemDetailDto.getName(), itemDetailDto.getDescription(),
itemDetailDto.getPrice(),
itemDetailDto.getStatus());
// new ItemIdxDto(itemToEdit.getItemIdx());
}

public ItemDetailReturnDto showItemDetail(HttpServletRequest httpServletRequest, Long itemIdx) {
Item item = itemRepository.findById(itemIdx)
.orElseThrow();

LocalDateTime localDateTime = item.getLastModifiedAt();

Long memberIdx;
if (httpServletRequest.getAttribute("memberIdx") == null) {
memberIdx = -1L;
} else {
memberIdx = (Long)httpServletRequest.getAttribute("memberIdx");
}

int view;
if (memberIdx.equals(-1L) || !memberIdx.equals(item.getSeller().getMemberIdx())) {
view = item.getView();
item.setView(++view);
incrementView(item);
itemRepository.save(item);
}

int interest = interestRepository.countByItem(item);
Expand All @@ -194,11 +190,16 @@ public ItemDetailReturnDto showItemDetail(HttpServletRequest httpServletRequest,
List<String> imageUrl = itemImageRepository.findAllByItemItemIdx(itemIdx).stream()
.map(ItemImage::getImageUrl).collect(Collectors.toList());

item.setLastModifiedAt(localDateTime);
return ItemDetailReturnDto.of(item, sellerDto, categoryWithoutImageDto, chatRooms, interest, interestChecked,
imageUrl);
}

public void incrementView(Item item) {
Integer currentView = item.getView();
item.updateView(currentView + 1);
itemRepository.save(item);
}

public void deleteItem(HttpServletRequest httpServletRequest, ItemIdxDto itemIdxDto) {
Item item = itemRepository.findById(itemIdxDto.getItemIdx())
.orElseThrow();
Expand Down

0 comments on commit efccd0a

Please sign in to comment.