7-5 development after deleting commodity category and acquisition of currentShop

Keywords: SQL xml Attribute

I. dao level

1. Develop dao interface

public interface ProductCategoryDao {
	/**
	 * Delete specified product category
	 * @param productCategoryId
	 * @param shopId
	 * @return effectedNum
	 */
	int deleteProductCategory(@Param("productCategoryId") long productCategory,@Param("shopId") long shopId);
}

2. Develop mapper implementation class

Note here that there is no parameterType label in < delete > and the value of {} is directly taken in SQL statement. This is because @ Param annotation is used for value passing in dao layer

<mapper namespace="com.imooc.o2o.dao.ProductCategoryDao">
 	<!-- delete -->
 	<delete id="deleteProductCategory">
 		DELETE FROM
 		tb_product_category
 		WHERE
 		product_category_id = #{productCategoryId}
 		AND shop_id = #{shopId}
 		<!-- If used@Param,You don't need to mapper.xml Set in parameterType Attribute,So the top two lines are dao Parameters of the interface -->
 	</delete>
 </mapper>

3, test

After adding and deleting, insert, query and output form a closed-loop operation. You can use the @ fixmethodorder (methodsorters. Name "assembling) annotation to execute multiple unit tests in name order. Here, change the method name to ABC.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductCategoryDaoTest extends BaseTest{
	@Autowired
	private ProductCategoryDao productCategoryDao;
	
	@Test
	public void testBQueryByShopId() {
		long shopId = 1L;
		List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
		System.out.println("The number of custom categories in this store is:" + productCategoryList.size());
	}
	@Test
	public void testABatchInsertProductCategory() {
		ProductCategory productCategory = new ProductCategory();
		productCategory.setProductCategoryName("Commodity category 1");
		productCategory.setPriority(1);
		productCategory.setCreateTime(new Date());
		productCategory.setShopId(1L);
		ProductCategory productCategory2 = new ProductCategory();
		productCategory2.setProductCategoryName("Commodity category 2");
		productCategory2.setPriority(2);
		productCategory2.setCreateTime(new Date());
		productCategory2.setShopId(1L);
		List<ProductCategory> productCategoryList = new ArrayList<ProductCategory>();
		productCategoryList.add(productCategory);
		productCategoryList.add(productCategory2);
		int effectedNum = productCategoryDao.batchInsertProductCategory(productCategoryList);
		assertEquals(2, effectedNum);
	}
	@Test
	public void testCDeleteProductCategory() {
		long shopId = 1L;
		List<ProductCategory> productCategoryList = productCategoryDao.queryProductCategoryList(shopId);
		for(ProductCategory pc : productCategoryList) {
			if("Commodity category 1".equals(pc.getProductCategoryName())||"Commodity category 2".equals(pc.getProductCategoryName())) {
				int effectedNum = productCategoryDao.deleteProductCategory(pc.getProductCategoryId(), shopId);
				assertEquals(1, effectedNum);
			}
			
		}
		
	}
}

II. service layer

1. Develop service interface

public interface ProductCategoryService {
	/**
	 * Set the category id of the commodity in this category to blank, and then delete the commodity category
	 * @param productCategoryId
	 * @param shopId
	 * @return
	 * @throws ProductCategoryOperationException
	 */
	ProductCategoryExecution deleteProductCategory(long productCategoryId,long shopId)
			throws ProductCategoryOperationException;
}

2. Develop service implementation class

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService{
	@Override
	@Transactional
	public ProductCategoryExecution deleteProductCategory(long productCategoryId, long shopId)
			throws ProductCategoryOperationException {
		// TODO sets the category id of the goods in this category to null
		try {
			int effectedNum = productCategoryDao.deleteProductCategory(productCategoryId, shopId);
			if(effectedNum <= 0) {
				throw new ProductCategoryOperationException("Product category deletion failed");
			} else {
				return new ProductCategoryExecution(ProductCategoryStateEnum.SUCCESS);
			}
		}catch (Exception e) {
			throw new ProductCategoryOperationException("deleteProductCategory error:" + e.getMessage());
		}
	}
}

III. controller layer

@Controller
@RequestMapping("/shopadmin")
public class ProductCategoryManagementController {
	@Autowired
	private ProductCategoryService productCategoryService;
	@RequestMapping(value = "/removeproductcategory",method = RequestMethod.POST)
	@ResponseBody
	private Map<String,Object> removeProductCategory(Long productCategoryId,HttpServletRequest request){
		Map<String,Object> modelMap = new HashMap<String,Object>();
		if(productCategoryId != null && productCategoryId > 0) {
			try {
				/*getAttribute The currentShop in ShopManagementController
				getshopmanagementinfo In setAttribute("currentShop", currentShop).
				*/
				Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
				ProductCategoryExecution pe = productCategoryService.deleteProductCategory(productCategoryId, currentShop.getShopId());
				if(pe.getState() == ProductCategoryStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				} else {
					modelMap.put("success", false);
					modelMap.put("errMsg", pe.getStateInfo());
				}
			}catch (ProductCategoryOperationException e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Please select at least one product category");
		}
		return modelMap;
	}
}

 

Posted by ralph4100 on Sat, 09 Nov 2019 10:28:35 -0800