Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/in27sung/stock-mate.git
Browse files Browse the repository at this point in the history
…into dashboard1
  • Loading branch information
BowWowBow committed Dec 11, 2024
2 parents 7d85aa6 + 70109e6 commit c0ea2bc
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ public String handleException(
String referer = request.getHeader("Referer"); // 이전 페이지로 이동
return "redirect:" + (referer != null ? referer : "/");
}
}
}
2 changes: 1 addition & 1 deletion stockMate/src/main/java/com/stockm8/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class WebConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
// Intercepter 적용
registry.addInterceptor(authorizationInterceptor)
.addPathPatterns("/product/**") // 인터셉터를 적용할 경로
.addPathPatterns("/product/**", "/category/**") // 인터셉터를 적용할 경로
.excludePathPatterns( // 인터셉터를 제외할 경로
"/", // HomeController 경로
"/favicon.ico", // 브라우저 기본 요청
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand All @@ -12,39 +16,71 @@
import org.springframework.web.bind.annotation.RequestParam;

import com.stockm8.domain.vo.CategoryVO;
import com.stockm8.domain.vo.UserVO;
import com.stockm8.service.CategoryService;
import com.stockm8.service.UserService;

@Controller
@RequestMapping("/category")
public class CategoryController {

private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);

@Inject
private CategoryService cService;

@Inject
private UserService uService;

// http://localhost:8088/category/register
// 카테고리 등록 페이지 호출 (GET)
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String registerCategoryGET(Model model) throws Exception {
public String registerCategoryGET(Model model, HttpServletRequest request) throws Exception {
logger.info("registerCategoryGET(Model model, HttpServletRequest request) 호출");
// 세션에서 userId 가져오기
HttpSession session = request.getSession(false);
Long userId = (session != null) ? (Long)session.getAttribute("userId") : null;
logger.info("session: {}", userId);

// userId로 사용자 정보 조회
UserVO user = uService.getUserById(userId);
int businessId = user.getBusinessId();
logger.info("businessId: {}", businessId);

// 필요한 데이터 (예: 상위 카테고리 목록 등) 전달
List<CategoryVO> parentCategories = cService.getAllCategories();
model.addAttribute("parentCategories", parentCategories);
List<CategoryVO> categoryList = cService.getCategoriesByBusinessId(businessId);
model.addAttribute("categoryList", categoryList);
logger.info("Category List: {}", categoryList);

// 카테고리 등록 폼을 보여주는 페이지로 이동
return "category/register";
}

// 카테고리 등록 처리 (POST)
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String registerCategoryPOST(CategoryVO vo) throws Exception {
public String registerCategoryPOST(CategoryVO vo, HttpServletRequest request) throws Exception {
logger.info("registerCategoryPOST(CategoryVO vo, HttpServletRequest request) 실행");

logger.info(vo+"");
// 세션에서 userId 가져오기
HttpSession session = request.getSession(false);
Long userId = (session != null) ? (Long)session.getAttribute("userId") : null;

// userId로 사용자 정보 조회
UserVO user = uService.getUserById(userId);

// 서비스 호출: 부모 카테고리 체크와 카테고리 등록을 서비스에서 처리
cService.registerCategoryWithParentCheck(vo);

// 등록 후 목록 페이지로 리다이렉트
return "redirect:/category/list";
}


// http://localhost:8088/category/list
// 카테고리 목록 페이지 호출 (GET)
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String listCategoryGET(Model model) throws Exception {
logger.info("listCategoryGET(Model model) 호출");
// 카테고리 목록 조회
List<CategoryVO> categories = cService.getAllCategories();

Expand All @@ -56,24 +92,34 @@ public String listCategoryGET(Model model) throws Exception {
// 카테고리 수정 페이지 (GET)
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String editCategoryGET(@RequestParam("categoryId") int categoryId, Model model) throws Exception {
// 카테고리와 상위 카테고리 정보 가져오기
CategoryVO category = cService.getCategoryWithParents(categoryId);
model.addAttribute("category", category);

// 상위 카테고리 정보
if (category.getParentId() != null) {
CategoryVO parentCategory = cService.getCategoryWithParents(category.getParentId());
model.addAttribute("parentCategory", parentCategory);
}
logger.info("editCategoryGET(@RequestParam(\"categoryId\") int categoryId, Model model) 호출");
// 카테고리와 상위 카테고리 정보 가져오기
CategoryVO vo = cService.getCategoryWithParents(categoryId);
model.addAttribute("category", vo);

// 상위 카테고리 정보 (부모 카테고리 리스트 가져오기)
List<CategoryVO> parentCategories = cService.getParentCategories(); // 부모 카테고리 리스트를 가져오기
model.addAttribute("parentCategories", parentCategories);

return "category/edit";
}

// 카테고리 수정 처리 (POST)
@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editCategoryPOST(@ModelAttribute CategoryVO vo) throws Exception {
logger.info("editCategoryPOST(@ModelAttribute CategoryVO vo) 실행");

// 부모 카테고리가 비어 있으면 null로 처리
if (vo.getParentId() != null && vo.getParentId().equals("")) {
vo.setParentId(null);
logger.info("부모 카테고리가 비어있어 null로 설정됨, categoryId: {}", vo.getCategoryId());
}
// 카테고리 수정
cService.updateCategory(vo);
return "redirect:/category/list";
logger.info("카테고리 수정 완료, categoryId: {}", vo.getCategoryId());

return "redirect:/category/list"; // 수정 후 목록 페이지로 리다이렉트
}

// 카테고리 삭제 페이지 (GET)
Expand Down
16 changes: 4 additions & 12 deletions stockMate/src/main/java/com/stockm8/domain/vo/CategoryVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@

@Data
public class CategoryVO {
private int categoryId; // 카테고리 고유 ID
private int categoryId; // 카테고리 고유 ID
private Integer parentId; // 상위 카테고리 ID (NULL이면 대분류)
private int businessId; // 사업자 ID
private Integer businessId; // 사업자 ID (null 가능)
private String categoryName; // 카테고리 이름
private int level; // 카테고리 계층 수준 (1=대분류, 2=소분류)
private Timestamp createdAt; // 카테고리 생성 일자
private Timestamp updatedAt; // 카테고리 수정 일자
private Boolean isDeleted; // 논리 삭제 여부 (true: 삭제됨, false: 활성)

// Getter와 Setter 메서드 추가
public Boolean getIsDeleted() {
return isDeleted;
}

public void setIsDeleted(Boolean isDeleted) {
this.isDeleted = isDeleted;
}

// 다른 Getter/Setter와 필드들
// Getter와 Setter 메서드 (자동 생성되는 @Data에 의해 생략됨)

}
13 changes: 10 additions & 3 deletions stockMate/src/main/java/com/stockm8/persistence/CategoryDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ public interface CategoryDAO {

// 카테고리 목록 조회
public List<CategoryVO> selectAllCategories() throws Exception;

// 특정 사업자(businessId) 소속의 카테고리 목록 조회
public List<CategoryVO> selectCategoriesByBusinessId(@Param("businessId") int businessId) throws Exception;

// 카테고리명 조회
public String selectCategoryNameById(int categoryId) throws Exception;

// 카테고리 이름 중복 체크
public int selectCategoryCountByName(String categoryName) throws Exception;

// 하위 카테고리들의 부모 ID를 갱신하는 메서드
public void updateSubCategoryParentId(int parentId, int categoryId) throws Exception;

// 카테고리 존재 여부 확인
public boolean existsById(@Param("categoryId") int categoryId, @Param("businessId") int businessId);

// 카테고리 수정
public void updateCategory(CategoryVO vo) throws Exception;

Expand All @@ -32,5 +38,6 @@ public interface CategoryDAO {
// 카테고리 논리 삭제 처리
public void deleteCategory(@Param("categoryId") int categoryId) throws Exception;

// 부모 카테고리만 조회
public List<CategoryVO> selectParentCategories() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class CategoryDAOImpl implements CategoryDAO {

// 카테고리 등록
@Override
public void insertCategory(CategoryVO category) throws Exception {
sqlSession.insert(NAMESPACE + "insertCategory", category);
public void insertCategory(CategoryVO vo) throws Exception {
sqlSession.insert(NAMESPACE + "insertCategory", vo);
}

// 카테고리 목록 조회 (삭제되지 않은 카테고리만 조회)
Expand All @@ -43,6 +43,22 @@ public String selectCategoryNameById(int categoryId) throws Exception {
return sqlSession.selectOne(NAMESPACE + "selectCategoryNameById", categoryId);
}

// 카테고리 이름 중복 체크
@Override
public int selectCategoryCountByName(String categoryName) throws Exception {
// SQL 쿼리 실행
return sqlSession.selectOne(NAMESPACE + "selectCategoryCountByName", categoryName);
}

// 하위 카테고리들의 부모 ID를 갱신하는 메서드
public void updateSubCategoryParentId(int parentId, int categoryId) throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("parentId", parentId);
params.put("categoryId", categoryId);

sqlSession.update(NAMESPACE + "updateSubCategoryParentId", params);
}

// 카테고리 존재 여부 확인
@Override
public boolean existsById(int categoryId, int businessId) {
Expand All @@ -54,6 +70,7 @@ public boolean existsById(int categoryId, int businessId) {
return count != null && count > 0;
}


// 카테고리 수정
@Override
public void updateCategory(CategoryVO category) throws Exception {
Expand All @@ -72,6 +89,7 @@ public void deleteCategory(int categoryId) throws Exception {
sqlSession.update(NAMESPACE + "deleteCategory", categoryId);
}

// 부모 카테고리만 조회
@Override
public List<CategoryVO> selectParentCategories() throws Exception {
return sqlSession.selectList(NAMESPACE + "selectParentCategories");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ public interface CategoryService {
// 카테고리 목록 조회
public List<CategoryVO> getAllCategories() throws Exception;

// 부모 카테고리 조회
public List<CategoryVO> getParentCategories() throws Exception;

// 카테고리 이름 중복 체크
public boolean checkCategoryNameExists(String categoryName) throws Exception;

// 특정 사업자(businessId) 소속의 카테고리 목록을 조회
public List<CategoryVO> getCategoriesByBusinessId(int businessId) throws Exception;

// 카테고리ID로 카테고리명 조회
public void getCategoryNameById(int categoryId) throws Exception;

// 카테고리 수정
// 카테고리 수정
public void updateCategory(CategoryVO vo) throws Exception;

// 카테고리 삭제
public void deleteCategory(int categoryId) throws Exception;

public List<CategoryVO> getParentCategories() throws Exception;

}
Loading

0 comments on commit c0ea2bc

Please sign in to comment.