Skip to content

Commit

Permalink
Merge pull request #107 from prgrms-be-devcourse/feature/NAYB-8
Browse files Browse the repository at this point in the history
[NAYB-8] 관리자는 상품을 삭제할 수 있다.
  • Loading branch information
Seongju-Lee authored Sep 15, 2023
2 parents 2d8d68d + f968fbe commit b0c598e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/prgrms/nabmart/domain/item/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.prgrms.nabmart.domain.order.OrderItem;
import com.prgrms.nabmart.domain.review.Review;
import com.prgrms.nabmart.global.BaseTimeEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -23,10 +22,14 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Getter
@Entity
@Where(clause = "is_deleted = false")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE item SET is_deleted = true WHERE item_id = ?")
public class Item extends BaseTimeEntity {

@Id
Expand Down Expand Up @@ -64,13 +67,16 @@ public class Item extends BaseTimeEntity {
@JoinColumn(name = "sub_category_id")
private SubCategory subCategory;

@OneToMany(mappedBy = "item", cascade = CascadeType.REMOVE)
@Column(nullable = false)
private boolean isDeleted = Boolean.FALSE;

@OneToMany(mappedBy = "item")
private List<Review> reviews = new ArrayList<>();

@OneToMany(mappedBy = "item", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "item")
private List<LikeItem> likeItems = new ArrayList<>();

@OneToMany(mappedBy = "item", cascade = CascadeType.REMOVE)
@OneToMany(mappedBy = "item")
private List<OrderItem> orderItems = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -109,4 +110,10 @@ public ResponseEntity<Void> saveItem(
URI location = URI.create(BASE_URI + savedItemId);
return ResponseEntity.created(location).build();
}

@DeleteMapping("/{itemId}")
public ResponseEntity<Void> deleteItem(@PathVariable Long itemId) {
itemService.deleteById(itemId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public void updateItem(UpdateItemCommand updateItemCommand) {
mainCategory, subCategory, updateItemCommand.discount());
}

@Transactional
public void deleteById(Long itemId) {
itemRepository.deleteById(itemId);
}

private SubCategory getSubCategoryById(Long subCategoryId) {
SubCategory subCategory = null;
if (subCategoryId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.prgrms.nabmart.domain.user.User;
import com.prgrms.nabmart.domain.user.repository.UserRepository;
import com.prgrms.nabmart.domain.user.support.UserFixture;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -66,6 +69,9 @@ public class DeliveryIntegrationTest {
@Autowired
RiderRepository riderRepository;

@Autowired
EntityManagerFactory emf;

@BeforeAll
static void beforeAll() {
Properties properties = System.getProperties();
Expand Down Expand Up @@ -140,7 +146,11 @@ void tearDown() {
deliveryRepository.deleteAll();
riderRepository.deleteAll();
orderRepository.deleteAll();
itemRepository.deleteAll();
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.createQuery("delete from Item").executeUpdate(); // 소프트 딜리트 아이템 강제 삭제
tx.commit();
subCategoryRepository.deleteAll();
mainCategoryRepository.deleteAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
Expand Down Expand Up @@ -297,13 +298,11 @@ public void updateItem() throws Exception {

}


@Nested
@DisplayName("상품 등록 api 호출 시")
class SaveItem {

RegisterItemRequest registerItemRequest = ItemFixture.registerItemRequest();
Long ITEM_ID = 1L;

@Test
@DisplayName("성공")
Expand Down Expand Up @@ -341,4 +340,30 @@ public void registerItem() throws Exception {
);
}
}

@Nested
@DisplayName("상품 삭제 api 호출 시")
class DeleteItem {

private static final Long ITEM_ID = 1L;

@Test
@DisplayName("성공")
public void deleteItem() throws Exception {

// When
ResultActions resultActions = mockMvc.perform(
delete("/api/v1/items/{itemId}", ITEM_ID)
.contentType(MediaType.APPLICATION_JSON));

// Then
resultActions.andExpect(status().isNoContent())
.andDo(document(
"Delete Item",
pathParameters(
parameterWithName("itemId").description("상품 ID")
))
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.prgrms.nabmart.domain.category.repository.MainCategoryRepository;
import com.prgrms.nabmart.domain.category.repository.SubCategoryRepository;
import com.prgrms.nabmart.domain.item.Item;
import com.prgrms.nabmart.domain.item.support.ItemFixture;
import jakarta.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -127,6 +129,29 @@ public void findByPriceLessThanAndMainCategoryOrderByPriceDescItemIdDesc() {
assertThat(items.size()).isEqualTo(5);
}

@Test
@DisplayName("삭제된 아이템은 조회되지 않는다.")
public void deletedItemIsNotSearch() {
//Given
mainCategoryRepository.save(mainCategory);
subCategoryRepository.save(subCategory);
for (int i = 0; i < 50; i++) {
Item item = new Item("item" + (i + 1), (int) (Math.random() * 1000), "0", 0, 0, 0,
mainCategory,
subCategory);
itemRepository.save(item);
}
for (long i = 1; i <= 30; i++) {
itemRepository.deleteById(i);
}

// When
List<Item> items = itemRepository.findAll();

// Then
assertThat(items.size()).isEqualTo(20);
}

@Test
@DisplayName("금액 낮은 순으로 조회된다.")
public void findByPriceGreaterThanAndMainCategoryOrderByPriceAscItemIdDesc() {
Expand All @@ -149,4 +174,21 @@ public void findByPriceGreaterThanAndMainCategoryOrderByPriceAscItemIdDesc() {
assertThat(items.size()).isEqualTo(5);
}
}

@Test
@DisplayName("아이템 삭제 시, 아이템 조회가 안된다.")
public void deleteItem() {
// Given
MainCategory mainCategory = new MainCategory("main");
SubCategory subCategory = new SubCategory(mainCategory, "sub");
Item item = ItemFixture.item(mainCategory, subCategory);
Item savedItem = itemRepository.save(item);

// When
itemRepository.deleteById(savedItem.getItemId());

// Then
Optional<Item> findItem = itemRepository.findByItemId(savedItem.getItemId());
assertThat(findItem.isEmpty()).isEqualTo(true);
}
}

0 comments on commit b0c598e

Please sign in to comment.