Skip to content
This repository has been archived by the owner on Mar 4, 2022. It is now read-only.

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
itning committed Feb 14, 2020
1 parent 9a5f67f commit 31ed25f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.sport.sportsmailserver.dto.LoginUser;
import com.sport.sportsmailserver.dto.RestModel;
import com.sport.sportsmailserver.security.MustUserLogin;
import com.sport.sportsmailserver.entity.CommodityDetail;
import com.sport.sportsmailserver.security.MustAdminLogin;
import com.sport.sportsmailserver.security.MustLogin;
import com.sport.sportsmailserver.service.CommodityDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

/**
* 商品详情
Expand All @@ -33,8 +33,22 @@ public CommodityDetailController(CommodityDetailService commodityDetailService)
* @return ResponseEntity
*/
@GetMapping("/commodityDetail/{commodityId}")
public ResponseEntity<?> findByCommodityId(@MustUserLogin LoginUser loginUser,
public ResponseEntity<?> findByCommodityId(@MustLogin(role = {MustLogin.ROLE.ADMIN, MustLogin.ROLE.USER}) LoginUser loginUser,
@PathVariable String commodityId) {
return RestModel.ok(commodityDetailService.findByCommodityId(commodityId));
}

/**
* 修改商品详情或新增
*
* @param loginUser 登录用户
* @param commodityDetail 商品详情
* @return ResponseEntity
*/
@PatchMapping("/commodityDetail")
public ResponseEntity<?> modifyOrAdd(@MustAdminLogin LoginUser loginUser,
@RequestBody CommodityDetail commodityDetail) {
commodityDetailService.modifyOrAdd(commodityDetail);
return RestModel.noContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ public interface CommodityDetailService {
* @return 商品详情
*/
CommodityDetail findByCommodityId(String commodityId);

/**
* <p>修改商品详情
* <p>如果商品详情ID不存在并且商品ID存在则会新增商品详情
*
* @param commodityDetail 商品详情
*/
void modifyOrAdd(CommodityDetail commodityDetail);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.sport.sportsmailserver.entity.Commodity;
import com.sport.sportsmailserver.entity.CommodityDetail;
import com.sport.sportsmailserver.exception.IdNotFoundException;
import com.sport.sportsmailserver.exception.NullFiledException;
import com.sport.sportsmailserver.repository.CommodityDetailRepository;
import com.sport.sportsmailserver.service.CommodityDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -28,4 +31,26 @@ public CommodityDetail findByCommodityId(String commodityId) {
commodity.setId(commodityId);
return commodityDetailRepository.findByCommodity(commodity).orElse(new CommodityDetail());
}

@Override
public void modifyOrAdd(CommodityDetail commodityDetail) {
if (StringUtils.isBlank(commodityDetail.getId())) {
if (null == commodityDetail.getCommodity() || StringUtils.isBlank(commodityDetail.getCommodity().getId())) {
throw new NullFiledException("商品ID为空");
} else {
Commodity commodity = new Commodity();
commodity.setId(commodityDetail.getCommodity().getId());

CommodityDetail newCommodityDetail = new CommodityDetail();
newCommodityDetail.setCommodity(commodity);
newCommodityDetail.setDetail(commodityDetail.getDetail());

commodityDetailRepository.save(newCommodityDetail);
}
} else {
CommodityDetail saved = commodityDetailRepository.findById(commodityDetail.getId()).orElseThrow(() -> new IdNotFoundException("详情ID不存在"));
saved.setDetail(commodityDetail.getDetail());
commodityDetailRepository.save(saved);
}
}
}

0 comments on commit 31ed25f

Please sign in to comment.