In the previous blog posts, we have completed all the functions of the user module, so in the next few blog posts, we will complete the classification management function module.
Let’s first look at the functional points of the classification management in the backend
Backend category management is actually a management of products, which is mainly divided into adding categories, updating category names, obtaining category nodes at the same level, and obtaining category IDs and sub-node categories.
1. Classification management module-Implementation of added category functions
Let's look at the Service layer first
// Add category public ServerResponse addCategory(String categoryName, Integer parentId){ if(parentId == null || StringUtils.isBlank(categoryName)){ return ServerResponse.createByErrorMessage("parameter error"); } Category category = new Category(); category.setName(categoryName); category.setParentId(parentId); category.setStatus(true); int rowCount = categoryMapper.insert(category); if(rowCount > 0){ return ServerResponse.createBySuceessMessage("Add category successfully"); } return ServerResponse.createByErrorMessage("Add category failed"); }It is relatively simple to add categories. It's a bit similar to the previous registration logic. First, check whether the categoryName and parentId transmitted from the front end exist. If it does not exist, the parameter error is prompted. Otherwise, continue to use JavaBean instances to increase the category. Similarly, after adding it with JavaBean, insert the result into the database. If the number of effective rows returned is greater than 0, the category is added successfully, otherwise the category is added fails.
Let's look at the Controller layer
/** * Manage category-add category* @param categoryName * @param parentId * @param session * @return */ @RequestMapping(value = "add_category.do") @ResponseBody public ServerResponse addCategory(String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId, HttpSession session) { User user = (User) session.getAttribute(Const.CURRENT_USER); if (user == null) { return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "The user is not logged in, please log in"); } // Verify whether it is an administrator if (iUserService.checkAdmin(user).isSuccess()) { return iCategoryService.addCategory(categoryName, parentId); } else { return ServerResponse.createByErrorMessage("No permission operation, please log in to the administrator"); } }First of all, there is a different place with the value value of RequestMapping, which only has one interface name, but does not specify the method of interface request, because the category management module is a background management by the website administrator and belongs to the background module. For background modules, they are used by internal employees of the company and do not need to be disclosed to the outside world, so you can just use the default GET method to request.
The general logic of background function management is to first verify whether the user is in the login state. If the user is in the login state, then verify whether the currently logged in is a website administrator. If it is not an administrator, you do not have the right to perform relevant management operations. If it is an administrator, you can perform background management. In the logic of background function management, it is generally enough to directly return the method processing results in the Service layer.
In the above method, it is necessary to determine the user's login status, so it is necessary to introduce user services and then directly call the corresponding method.
2. Classification management module-Implementation of update category name function
Let's look at the Service layer first
// Update category name public ServerResponse updateCategoryName(String categoryName, Integer categoryId){ if(categoryId == null || StringUtils.isBlank(categoryName)){ return ServerResponse.createByErrorMessage("Update category parameter error"); } Category category = new Category(); category.setId(categoryId); category.setName(categoryName); int rowCount = categoryMapper.updateByPrimaryKeySelective(category); if(rowCount > 0){ return ServerResponse.createBySuceessMessage("Update category name successfully"); } return ServerResponse.createByErrorMessage("Update category name failed"); }It is exactly the same as the previous processing logic, and I will not go into details here.
Let's look at the Controller layer
/** * Manage category-Update category name* @param categoryName * @param categoryId * @param session * @return */ @RequestMapping(value = "update_category_name") @ResponseBody public ServerResponse updateCategoryName(String categoryName, Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "The user is not logged in, please log in"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.updateCategoryName(categoryName, categoryId); }else{ return ServerResponse.createByErrorMessage("No permission operation, please log in to the administrator"); } }It is exactly the same as the previous processing logic, and I will not go into details here.
3. Classification management module-Implementation of the function of obtaining the horizontal category node (backend product search)
Service layer
//Parent-level query category node public ServerResponse<List<Category>> getChildrenParalleCategory(Integer categoryId){ List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); if(CollectionUtils.isEmpty(categoryList)){ logger.info("The subcategory of the current classification was not found"); } return ServerResponse.createBySuccess(categoryList); }When processing a set of product information, it often uses the method of collection. Its application is different depending on the type of collection. Here, I am using List collections. First, considering that List collections are convenient for traversal operations and management. Because it is to manage products, the generic type of the List collection is specified as Category, and the product id query is performed through the selectCategoryChildrenByParentId method of categoryMapper. In logical judgment, use the CollectionUtils tool class encapsulated in Java to determine whether the return result of the collection is empty. If it is empty, print a line of logs, otherwise the successful execution of the categoryList result can be returned. The logger here is a log printing tool that you encapsulate. Let me briefly mention its usage.
private org.slf4j.Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
Note that this logger uses the slf4j package, don't export the wrong package, and then LoggerFactory.getLogger(class), you need to pass a parameter, which is the class that currently needs to print the log, such as CategoryServiceImpl.class here. You can see the print results of the log in the console.
Controller layer
/** * Manage category-get nodes of the same category* @param categoryId * @param session * @return */ @RequestMapping(value = "get_category.do") @ResponseBody public ServerResponse getChildrenParalleCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "The user is not logged in, please log in"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.getChildrenParalleCategory(categoryId); }else { return ServerResponse.createByErrorMessage("No permission operation, please log in to the administrator"); } }For practical reasons, when the quantity of goods is 0, no need to manage the product category, so the defaultValue="0" annotated by RequestParam is used to specify the default value of a parameter. The rest of the logic is exactly the same as before.
4. Classification management module-retrieve the implementation of category id and sub-node functions
Look at the Service layer
public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){ Set<Category> categorySet = Sets.newHashSet(); findChildCategory(categoryId, categorySet); List<Integer> categoryIdList = Lists.newArrayList(); if(categoryId != null){ for(Category categoryItem : categorySet){ categoryIdList.add(categoryItem.getId()); } } return ServerResponse.createBySuccess(categoryIdList); } // Recursive algorithm to calculate the child node private Set<Category> findChildCategory(Integer categoryId, Set<Category> categorySet){ Category category = categoryMapper.selectByPrimaryKey(categoryId); if(category != null){ categorySet.add(category); } // Find child node List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); for(Category categoryItem : categoryList){ findChildCategory(categoryItem.getId(), categorySet); } return categorySet; }The main method is selectCategoryAndChildrenById, and the auxiliary method is findChildCategory, which calculates the child nodes by recursively. In the auxiliary method, the product id information is queried through categoryId, and added to the Set collection, and then the product child nodes are traversed through the foreach loop, and finally the categorySet is returned. In the main method, the auxiliary method is called, the product id and child nodes are found, and then the product id and child nodes are placed in the List collection, and then the result we want is traversed through the foreach loop, and finally the categoryIdList is directly returned.
Controller layer
/** * Manage category-get id and child node category* @param categoryId * @param session * @return */ @RequestMapping(value = "get_deep_category.do") @ResponseBody public ServerResponse getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ User user = (User)session.getAttribute(Const.CURRENT_USER); if(user == null){ return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "The user is not logged in, please log in"); } if(iUserService.checkAdmin(user).isSuccess()){ return iCategoryService.selectCategoryAndChildrenById(categoryId); }else{ return ServerResponse.createByErrorMessage("No permission operation, please log in to the administrator"); } }It is exactly the same as the previous logical processing of obtaining the same-level nodes in the category, so I will not go into details here.
5. Supplementary user module
In the background category management module, a method of verifying whether the currently logged in user is an administrator is used. This is written in the user module. I forgot to write it before, so I will make a supplement here.
Service layer of user module
// User background - Verify whether it is an administrator public ServerResponse checkAdmin(User user){ if(user != null && user.getRole().intValue() == Const.Role.ROLE_ADMIN){ return ServerResponse.createBySuccess(); } return ServerResponse.createByError(); }Because it is related to administrators, it only needs to perform logical processing in the Service layer and does not need to be declared in the Controller. This method passes in a user object and makes permission judgments through the encapsulated Role interface. If the return value is ADMIN, it is considered an administrator and returns it directly, otherwise it will fail.
Having written this, the category management module in the background is finished. Because the module has fewer functional interfaces, it took a long time to write them all in a blog post, which also makes it easier for everyone to learn the backend category management module at one time.
In the next blog post, we will continue to promote the progress of the project and will bring you the development of back-end product modules. We hope that everyone will keep up with the progress.
If you encounter any problems in your previous blog post, please leave a message and feedback, and I will try my best to solve the problem for you.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.