コード例 #1
0
        public async Task <UserPurchase> UpdateOrder
            (int purchaseId, SubmitOrderItemsModel model, int currentUserId)
        {
            var purchase = await _db.Purchases
                           .Include(p => p.StoreOrder)
                           .Include(p => p.UserPurchases)
                           .ThenInclude(up => up.User)
                           .Where(up => up.Id == purchaseId)
                           .FirstAsync();

            if (purchase.StoreOrder.IsSubmitted)
            {
                throw new ApiException(401, "Purchase closed for change by creator", ErrorCode.ValidationError);
            }

            var userPurchase = purchase.UserPurchases
                               .Where(t => t.UserId == currentUserId)
                               .FirstOrDefault();

            var prevItems = await _db.UserPurchaseItems
                            .Include(t => t.UserPurchase)
                            .Where(t => t.UserPurchase.PurchaseId == purchaseId)
                            .Where(t => t.UserPurchase.UserId == currentUserId)
                            .ToListAsync();

            var items = model.Items.Select(u => new UserPurchaseItem
            {
                UserPurchase = userPurchase,

                ItemId = u.ItemId,
                Number = u.Number,
                Price  = u.Price
            });

            var prevUserPurchaseSum = userPurchase.Sum;

            userPurchase.Sum = items.Sum(item => item.Price);

            if (purchase.CreatorId != currentUserId)
            {
                purchase.TotalSum = (purchase.TotalSum ?? 0) - (prevUserPurchaseSum ?? 0) + userPurchase.Sum;
            }

            _db.UserPurchaseItems.RemoveRange(prevItems);
            _db.UserPurchaseItems.AddRange(items);
            await _db.SaveChangesAsync();

            return(userPurchase);
        }
コード例 #2
0
        public async Task <IActionResult> UpdateOrder(int purchaseId, [FromBody] SubmitOrderItemsModel model)
        {
            if (model == null)
            {
                throw new ApiException(400, "Invalid request body", ErrorCode.InvalidRequestFormat);
            }

            var userPurchase = await _storeOrdersService
                               .UpdateOrder(purchaseId, model, _currentUser.Id);

            if (userPurchase.Purchase.CreatorId == _currentUser.Id)
            {
                return(Ok(_mapper.Map <MyPurchase>(userPurchase.Purchase)));
            }
            else
            {
                return(Ok(_mapper.Map <PurchaseWithMe>(userPurchase)));
            }
        }