Skip to content

Commit

Permalink
Merge pull request #118 from beyond-sw-camp/item/company/list
Browse files Browse the repository at this point in the history
[Feat] 사업자 상품 목록 조회 기능 구현 #6
  • Loading branch information
dlrkdms125 authored Jul 19, 2024
2 parents 942fa45 + 8311d2f commit b0b0e4b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.team404x.greenplate.common.BaseResponse;
import com.team404x.greenplate.common.BaseResponseMessage;
import com.team404x.greenplate.item.model.request.ItemCreateReq;
import com.team404x.greenplate.item.model.request.ItemFindReq;
import com.team404x.greenplate.item.model.request.ItemSearchReq;
import com.team404x.greenplate.item.model.request.ItemUpdateReq;
import com.team404x.greenplate.item.model.response.*;
Expand All @@ -22,8 +23,6 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

@RestController
@RequestMapping("/item")
@RequiredArgsConstructor
Expand Down Expand Up @@ -72,6 +71,7 @@ public BaseResponse update(@AuthenticationPrincipal CustomUserDetails company, @
}
}


@Operation(summary = "[전체] 상품 전체 목록 조회를 위한 API")
@RequestMapping(method = RequestMethod.GET, value = "/list")
public BaseResponse list() {
Expand Down Expand Up @@ -118,26 +118,34 @@ public BaseResponse list(Long id) {
return new BaseResponse(BaseResponseMessage.ITEM_DETAILS_FAIL);
}
}
// @RequestMapping(method=RequestMethod.GET,value="/find")
// public BaseResponse find(@AuthenticationPrincipal CustomUserDetails customUserDetails, @RequestBody Long id) {
// try {
// itemService.findList(Long id);
// List<ItemListFindRes> itemListFindResList = itemService.findList(id);
// return new BaseResponse(BaseResponseMessage.USER_READ_LIST_SUCCESS,itemListFindResList);
// } catch (Exception e) {
// if (itemService.findList(id) == null){
// return new BaseResponse(BaseResponseMessage.USER_READ_LIST_FAIL_NULL); //해당 판매자의 상품이 없다
// } else {
// return new BaseResponse(BaseResponseMessage.USER_READ_LIST_FAIL);
// }
//
// }
// }


@RequestMapping(method=RequestMethod.GET,value="/company")
public BaseResponse listCompanyItem(@AuthenticationPrincipal CustomUserDetails company){
try {
List<ItemFindRes> result =itemService.listCompanyItem(company.getId());
return new BaseResponse(BaseResponseMessage.USER_READ_LIST_SUCCESS,result);
} catch (Exception e) {
return new BaseResponse(BaseResponseMessage.USER_READ_LIST_FAIL);
}

}



@RequestMapping(method=RequestMethod.GET,value="/search")
public BaseResponse search(@RequestBody ItemSearchReq itemSearchReq) {
List<ItemSearchRes> itemSearchReqList = itemService.search(itemSearchReq.getCompanyId());
return new BaseResponse(itemSearchReqList);
try {
List<ItemSearchRes> itemSearchReqList = itemService.search(itemSearchReq.getCompanyId());
itemService.search(itemSearchReq.getCompanyId());
return new BaseResponse(BaseResponseMessage.USER_SEARCH_SUCCESS,itemSearchReqList);
} catch (Exception e) {
if(itemSearchReq.getSearch() == null){ // 검색어에 맞는 상품이 없습니다
return new BaseResponse(BaseResponseMessage.USER_SEARCH_FAIL_NOTFOUND);
} else{
return new BaseResponse(BaseResponseMessage.USER_SEARCH_FAIL);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@AllArgsConstructor
@Getter
@Setter
public class ItemListFindReq {
public class ItemFindReq {
private Long companyId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Builder
@Getter
public class ItemListFindRes {
public class ItemFindRes {
private Long itemId;
private String name;
private Integer price;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.team404x.greenplate.item.service;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.team404x.greenplate.company.model.entity.Company;
import com.team404x.greenplate.company.repository.CompanyRepository;
import com.team404x.greenplate.item.category.entity.Category;
import com.team404x.greenplate.item.category.repository.CategoryRepository;
import com.team404x.greenplate.item.model.entity.Item;
import com.team404x.greenplate.item.model.request.ItemCreateReq;
import com.team404x.greenplate.item.model.response.*;
import com.team404x.greenplate.item.repository.ItemRepository;
import jakarta.persistence.metamodel.SingularAttribute;
import lombok.AllArgsConstructor;
import org.springframework.data.jpa.domain.AbstractPersistable;
import org.springframework.stereotype.Service;
import com.team404x.greenplate.item.model.request.ItemUpdateReq;

Expand All @@ -23,6 +21,7 @@
public class ItemService {
private final ItemRepository itemRepository;
private final CategoryRepository categoryRepository;
private final CompanyRepository companyRepository;
public void create(Long id, ItemCreateReq itemCreateReq) {
Category category = categoryRepository.findCategoryBySubCategory(itemCreateReq.getSubCategory());
Item item = Item.builder()
Expand Down Expand Up @@ -107,23 +106,29 @@ private List<ItemRes> getItemRes(List<Item> items) throws Exception {
}
return itemResList;
}
// public List<ItemCompanyListRes> findList(Long id) {
// List<Item> itemCompanylist = itemRepository.findAllByCompanyId(id);
// List<ItemCompanyListRes> itemCompanyListResList = new ArrayList<>();
// for(Item item : itemCompanylist) {
// ItemCompanyListRes responses = ItemCompanyListRes.builder()
// .itemId(item.getId())
// .name(item.getName())
// .price(item.getPrice())
// .stock(item.getStock())
// .state(item.getState())
// .imageUrl(item.getImageUrl())
// .discountPrice(item.getDiscountPrice())
// .build();
// itemCompanyListResList.add(responses);
// }
// return itemCompanyListResList;
// }


public List<ItemFindRes> listCompanyItem(Long id) throws Exception{
Company company = companyRepository.findById(id).orElseThrow();
List<Item> companyitemlist = company.getItems();
List<ItemFindRes> itemFindResList = new ArrayList<>();
for (Item item : companyitemlist) {
itemFindResList.add(
ItemFindRes.builder()
.itemId(item.getId())
.name(item.getName())
.price(item.getPrice())
.stock(item.getStock())
.state(item.getState())
.imageUrl(item.getImageUrl())
.discountPrice(item.getDiscountPrice())
.build());

}
return itemFindResList;
}


public List<ItemSearchRes> search(Long id) {
List<Item> itemSearchList = itemRepository.findAllByCompanyId((id));
List<ItemSearchRes> itemSearchResList = new ArrayList<>();
Expand Down

0 comments on commit b0b0e4b

Please sign in to comment.