文章目录
- 概述
- 结构
- Service层接口及其实现类
- 单元测试
- Github地址
概述
既然是商铺编辑,肯定要根据入参shopId获取shop信息,然后用户在客户端修改店铺信息后,提交到后台更新商铺信息。
所以同样的对于Service层来讲 有2个方法 (DAO层也有对应的两个方法,只不过updateShop我们复用了)
- Shop getShopById(long shopId);
- ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException;
结构
Service层接口及其实现类
com.artisan.o2o.service.ShopService 接口新增两个接口方法如下
代码语言:javascript复制/**
*
*
* @Title: getShopById
*
* @Description: 根据shopId查询商铺
*
* @param shopId
* @return
*
* @return: Shop
*/
Shop getShopById(long shopId);
/**
*
*
* @Title: modifyShop
*
* @Description: 编辑商铺信息
*
* @param shop
* @param shopFileInputStream
* @param fileName
* @return
*
* @return: ShopExecution
*/
ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException;
com.artisan.o2o.service.impl.ShopServiceImpl.java实现类
代码语言:javascript复制 @Override
public Shop getShopById(long shopId) {
return shopDao.selectShopById(shopId);
}
@Override
@Transactional
public ShopExecution modifyShop(Shop shop, InputStream shopFileInputStream, String fileName) throws ShopOperationException {
if (shop == null || shop.getShopId() == null) {
return new ShopExecution(ShopStateEnum.NULL_SHOP_INFO);
}else{
try {
// 1. 判断是否需要处理图片
if (shopFileInputStream != null && fileName != null && !"".equals(fileName)) {
// 1.1 删除掉旧的图片
// 查询入参shop对应数据库表中的shopImg路径
Shop tempShop = shopDao.selectShopById(shop.getShopId());
if (tempShop != null) {
// 删除就的缩略图
ImageUtil.deleteStorePath(tempShop.getShopImg());
}
// 1.2 用新的图片生成缩略图
addShopImg(shop, shopFileInputStream, fileName);
}
// 2. 更新店铺信息
// 2.1 更新一些必要属性
shop.setLastEditTime(new Date());
// 2.2 更新店铺
int effectedNum = shopDao.updateShop(shop);
if (effectedNum <= 0) {
throw new ShopOperationException(ShopStateEnum.INNER_ERROR.getStateInfo());
}
return new ShopExecution(ShopStateEnum.SUCCESS, shop);
} catch (Exception e) {
e.printStackTrace();
throw new ShopOperationException("modify shop error:" e.getMessage());
}
}
}
因为用户有可能更新图片,其中为了删除旧的文件或者目录,com.artisan.o2o.util.ImageUtil.java 新增了工具类
代码语言:javascript复制/**
*
*
* @Title: deleteStorePath
*
* @Description: 判断storePath是否为目录,为目录的话删掉目录下的所有文件,否则删掉文件
*
* @param storePath
*
* @return: void
*/
public static void deleteStorePath(String storePath) {
File fileOrMenu = new File(FileUtil.getImgBasePath() storePath);
if (fileOrMenu != null) {
if (fileOrMenu.isDirectory()) {
File[] files = fileOrMenu.listFiles();
for (int i = 0; i < files.length; i ) {
files[i].delete();
}
}
fileOrMenu.delete();
}
}
单元测试
代码语言:javascript复制@Test
public void testModifyShop() {
Shop shop = new Shop();
Area area = new Area();
ShopCategory shopCategory = new ShopCategory();
shop.setShopId(28L);
area.setAreaId(2);
shopCategory.setShopCategoryId(2L);
shop.setArea(area);
shop.setShopCategory(shopCategory);
shop.setShopName("Modify咖啡店");
shop.setShopDesc("Modify小工匠的咖啡店");
shop.setShopAddr("Modify-NanJing");
shop.setPhone("123456");
shop.setPriority(78);
File shopFile = new File("D:/o2o/artisan.jpg");
ShopExecution se = null;
InputStream ins = null;
try {
ins = new FileInputStream(shopFile);
se = shopService.modifyShop(shop, ins, shopFile.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Assert.assertEquals(ShopStateEnum.SUCCESS.getState(), se.getState());
}
运行符合预期。
控制台日志
代码语言:javascript复制Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@3fed2870] will be managed by Spring
==> Preparing: SELECT s.shop_id, s.shop_name, s.shop_desc, s.shop_addr, s.phone, s.shop_img, s.priority, s.create_time, s.last_edit_time, s.enable_status, s.advice, a.area_id, a.area_name, sc.shop_category_id, sc.shop_category_name FROM tb_shop s, tb_area a, tb_shop_category sc WHERE s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id AND s.shop_id = ?
==> Parameters: 28(Long)
<== Columns: shop_id, shop_name, shop_desc, shop_addr, phone, shop_img, priority, create_time, last_edit_time, enable_status, advice, area_id, area_name, shop_category_id, shop_category_name
<== Row: 28, Modify咖啡店, Modify小工匠的咖啡店, Modify-NanJing, 123456, uploaditemshopImage282018060301211217157.jpg, 78, 2018-05-28 23:13:37.0, 2018-06-03 01:21:14.0, 0, null, 1, 北京, 1, 咖啡奶茶
<== Total: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6] from current transaction
==> Preparing: update tb_shop SET shop_name=?, shop_desc=?, shop_addr=?, phone=?, shop_img=?, priority=?, last_edit_time=?, area_id=?, shop_category_id=? where shop_id = ?
==> Parameters: Modify咖啡店(String), Modify小工匠的咖啡店(String), Modify-NanJing(String), 123456(String), uploaditemshopImage282018060301223045572.jpg(String), 78(Integer), 2018-06-03 01:22:31.968(Timestamp), 2(Integer), 2(Long), 28(Long)
<== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@61544ae6]
库表字段正常
旧的图片被删除,新的图片OK
Github地址
代码地址: https://github.com/yangshangwei/o2o