首先我们的service完成了店铺注册
步骤:
1.接受要注册的商铺的类(里面的属性用户能设置的都设置了)和商铺的图片
2.检查所需的的信息是否有空值
3.设置管理员需要设置的shop的属性
4.插入这条记录
5.将商铺的照片存入属性,并且将照片地址更新到数据库中
6.操作全部成功的话返回一个ShopExecution对象,并且构造参数是代表成功创建的枚举对象
首先我们要知道,Service层是具有事务的性质的,如果出错会回滚,但是只有抛出运行时错误,才回滚,所以我们新建一个运行时错误类继承自RuntimeExcuption
注意:service是需要事务来保证的,所以要注解解释是事务操作
package storepro.execuptions;
//Shop运行异常类,
public class ShopOperationException extends RuntimeException{
public ShopOperationException(String msg){
super(msg);
}
}
这样每当出错了就抛出这样的异常,能够事务回滚
package storepro.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import storepro.dao.ShopDao;
import storepro.dto.ShopExecution;
import storepro.entity.Shop;
import storepro.enums.ShopStateEnum;
import storepro.execuptions.ShopOperationException;
import storepro.service.ShopService;
import storepro.util.ImageUtil;
import storepro.util.PathUtil;
import java.beans.Transient;
import java.io.File;
import java.util.Date;
@Service
public class ShopServiceImpl implements ShopService{
@Autowired
private ShopDao shopDao;//自动生成shopDao
/*
* 注册商铺的service层操作
* */
@Override
@Transactional //说明是事物操作,错误的话要回滚,只有抛出运行时异常才能回滚
public ShopExecution addShop(Shop shop, File shopImg) {
if (shop==null) {//检查shop是否为空
return new ShopExecution((ShopStateEnum.NULL_SHOP));//为空则调用创建店铺失败的构造器
}
try{
shop.setEnableStatus(0);//过了非空检查只要不出错,则会创建成功,那么EnableStatus的值为 0说明此时正在审核。
shop.setCreateTime(new Date());//存入创建时间
shop.setLastEditTime(new Date());//此时创建时间就是最后修改时间
int effectedNum=shopDao.insertShop(shop);//插入记录
if(effectedNum<=0) {//说明没有插入成功,抛出异常
throw new ShopOperationException("店铺创建失败");
}else{//创建成功
if(shopImg!=null) {
try{
addShopImg(shop,shopImg);//存储商铺图片
}catch (Exception e){
throw new ShopOperationException("addShopImg error:"+e.getMessage());
}
//更新店铺的图片地址
effectedNum=shopDao.updateShop(shop);
if(effectedNum<=0){
throw new ShopOperationException("更新图片地址失败");
}
}
}
}catch (Exception e){
throw new ShopOperationException("addrShop error"+e.getMessage());//显示错误信息
}
return new ShopExecution(ShopStateEnum.CHECK,shop);//操作成功
}
private void addShopImg(Shop shop,File shopImg) {//添加图片的方法
String dest= PathUtil.getShopImagePath(shop.getShopId());//通过id得知照片应该存在哪个文件夹下
String ShopImgAddr= ImageUtil.generateThumnail(shopImg,dest);//得到文件的全限定名,并存入相应的路径
shop.setShopImg(ShopImgAddr);//存入图片路径
}
}
我们的private void addShopImg(Shop shop,File shopImg) 这个方法是通过shop实体和图片文件来获得图片存储的路径并且将文件存入应该存储的路径
接下来test:
创建test类:
代码实现:
package storepro.service;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import storepro.BaseTest;
import storepro.dto.ShopExecution;
import storepro.entity.Area;
import storepro.entity.PersonInfo;
import storepro.entity.Shop;
import storepro.entity.ShopCategory;
import storepro.enums.ShopStateEnum;
import java.io.File;
import java.util.Date;
public class ShopServiceTest extends BaseTest {
@Autowired
private ShopService shopService;
@Test
public void testAddShop(){
Shop shop=new Shop();
PersonInfo personInfo=new PersonInfo();
ShopCategory shopCategory=new ShopCategory();
Area area =new Area();
personInfo.setUserId(1L);//long型后面加L
shopCategory.setShopCategoryId(1L);
area.setAreaId(2);
shop.setOwner(personInfo);
shop.setArea(area);
shop.setShopName("测试的店铺1");
shop.setShopCategory(shopCategory);
shop.setShopAddr("test1");
shop.setPhone("test1");
shop.setShopDesc("test1");
shop.setShopImg("test1");
shop.setCreateTime(new Date());
shop.setAdvice("审核中");
shop.setEnableStatus(ShopStateEnum.CHECK.getState());
File file=new File("C:/Users/YF/Desktop/yufei.jpg");
ShopExecution se=shopService.addShop(shop,file);
Assert.assertEquals(ShopStateEnum.CHECK.getState(),se.getState());
}
}
讲解:我们通过手动配置一个shop对象并手动提供一个图片,
调用shopService的实现类来完成addShop方法。
判断:因为成功创建店铺后,dto中ShopExecution的类中的state是一个审核中的状态码,也就是CHECK,我们判断我check的state码和我们创建的店铺的state比较可得正确与否