public IActionResult Create([FromBody]OrderDto orderDto) { string userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; Guid gORderNumber = Guid.NewGuid(); string OrderNumber = gORderNumber.ToString(); //var userId = int.Parse(context.Principal.Identity.Name); var user = _userService.GetById(int.Parse(userId)); try { Order order = orderDto.ToModel(); // Audit fields order.createdBy = user.Username; order.Created = DateTime.Now; order.OrderNumber = OrderNumber; Order returnedOrder = _orderService.Create(order, user); OrderDto r = OrderDto.FromModel(returnedOrder); return Ok(r); } catch (AppException ex) { //var Message = ex.Message; return ErrorResponse(ex.Message); //return BadRequest(new { message = ex.Message }); } }
public void AddOrder(OrderDto dto) { var goods = GoodsRepository.Instance.Find(x => x.Id == dto.GoodsId).FirstOrDefault(); if (goods == null) { throw new ServiceException("不存在此商品"); } if (Convert.ToInt32(dto.Quantity) > goods.Quantity) { throw new ServiceException("此商品库存不足"); } var userId = UserService.GetCurrentUser().Id; var user = UserService.Instance.GetUser(userId); if (user == null) { throw new ServiceException("当前用户未登录,不能完成下单操作"); } if (user.Integral < goods.Integral) { throw new ServiceException("对不起,您的积分暂时不足以兑换此商品,请选择其它商品进行兑换"); } var entity = dto.ToModel <Order>(); entity.Integral = goods.Integral; entity.OrderNo = OrderHelper.GetOrderNo(); entity.State = OrderStateEnum.未发货; entity.UserId = userId; //减库存 goods.Quantity = goods.Quantity - Convert.ToInt32(dto.Quantity); orderRepository.Transaction(() => { orderRepository.Insert(entity); GoodsRepository.Instance.Save(goods); }); }