卖家创建商品
请参
private String userId;//卖家userId
private String name;//商品名称
private Double price;//商品价格
private String ins;//商品简介
private String img;//商品图片
private Integer num;//商品库存
代码
/**
controller
*/
/** 商品/获取创建
* @param request
* @param httpServletRequest
* @return
*/
@ApiOperation(value = "create goods")
@RequestMapping(value = "/goods/create", method = RequestMethod.POST)
public ApiResponse goods(@RequestBody GoodCreateParam request, HttpServletRequest httpServletRequest) {
return goodService.good(request.getName(), request.getImg(), request.getPrice(), request.getNum(),
request.getIns(), request.getUserId());
}
/**
service
*/
@Override
public ApiResponse good(String name, String img, Double price, Integer num, String ins, String userId) {
//根据userId 判断用户是否存在. 存在创建,不存在不做处理
Boolean userExist = userFacade.checkUserExist(userId, 1);
if (userExist) {
String goodId= goodFacade.good(name, img, price, num, ins, userId);
if (Strings.isNullOrEmpty(goodId)) {
return new ApiResponse(CodeEnum.FAILED);
}
} else {
return new ApiResponse(CodeEnum.USER_NO_EXIST);
}
return new ApiResponse(CodeEnum.SUCCESS);
}
/**
facade
*/
//method no.1
userFacade.checkUserExist(String param,Integer type);
//method no.2
@Override
public String good(String name, String img, Double price, Integer num, String ins, String userId) {
try {
String id = new ObjectId().toString();
DBObject dbObject =new BasicDBObject();
DecimalFormat df = new DecimalFormat("0.00");
price = Double.valueOf(df.format(price));
dbObject.put("id", id);
dbObject.put("name", name);
dbObject.put("img", img);
dbObject.put("price", price);
dbObject.put("num", num);
dbObject.put("ins", ins);
dbObject.put("userId", userId);
dbObject.put("createTime", new Date());
dbObject.put("ModifyTime", new Date());
dbObject.put("isDel", 0);
MongoTemplate.save(dbObject,CollectionNameEnum.GOOD_TABLE.getName());
return id;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
思路:根据userId 判断用户是否存在
存在--> 调用goodFacade.good 创建商品.
不存在-->返回code码
修改商品属性
请参
试试
代码
/*controller */
/** 商品/货物属性修改
* @param goodsId
* @param request
* @param httpServletRequest
* @return
*/
@ApiOperation(value = "RequestMethod=put,modify goods if RequestMehtod=DELETE del goods;")
@RequestMapping(value = "/goods/{goods_id}/", method = {RequestMethod.PUT,RequestMethod.DELETE})
public ApiResponse goods(@PathVariable(value = "goods_id") String goodsId,
@RequestBody(required=false) HashMap<String, Object> request, HttpServletRequest httpServletRequest) {
//请求为PUT方法是修改
if ("PUT".equals(httpServletRequest.getMethod())) {
return goodService.good(goodsId, String.valueOf(request.get("name")), String.valueOf(request.get("img")),
(Double) request.get("price"), Integer.valueOf((String) request.get("num")), String.valueOf(request.get("ins")),
String.valueOf(request.get("userId")));
}
//请求方法是DELETE 是删除商品
if ("DELETE".equals(httpServletRequest.getMethod())) {
return goodService.goodDel(goodsId);
}
return new ApiResponse(CodeEnum.ILLEGAL_REQUEST);
}
/*service */
@Override
public ApiResponse good(String id, String name, String img, Double price, Integer num, String ins, String userId) {
if (userFacade.checkUserExist(userId, 1)) {
if (goodFacade.goodExist(id, userId)) {
Boolean update= goodFacade.good(id, name, img, price, num, ins, userId);
if (update) {
return new ApiResponse(CodeEnum.SUCCESS);
}
}
}
return new ApiResponse(CodeEnum.FAILED);
}
/*facade */
//method 1
userFacade.checkUserExist(userId, 1);
//method 2
@Override
public Boolean goodExist(String goodId,String ownerId) {
Query query=new BasicQuery(new Document());
query.addCriteria(Criteria.where("id").is(goodId).and("userId").is(ownerId));
return MongoTemplate.exists(query,CollectionNameEnum.GOOD_TABLE.getName());
}
//method 3
@Override
public Boolean good(String id, String name, String img, Double price, Integer num, String ins, String userId) {
DecimalFormat df = new DecimalFormat("0.00");
Query query = new Query();
query.addCriteria(Criteria.where("ids").is(id).and("userId").is(userId));
Update update = new Update().set("name", name).set("img", img).set("price", Double.valueOf(df.format(price)))
.set("num", num).set("ins", ins);
UpdateResult updateResult = MongoTemplate.updateFirst(query, update, CollectionNameEnum.GOOD_TABLE.getName());
if (updateResult.getModifiedCount()<=0) {
return false;
}
return true;
}
思路:验证用户是否存在.
验证此商品是否属于该用户.
修改商品属性;