Пример #1
0
        public async Task <Purchase> CreatePurchase(CreatePurchaseModel model, int currentUserId)
        {
            var purchase = new Purchase
            {
                Type       = PurchaseType.Custom,
                CreatorId  = currentUserId,
                IsPayedOff = false,
                Name       = model.Name,
                TotalSum   = model.TotalSum,
                CreatedAt  = DateTime.UtcNow
            };

            _db.Purchases.Add(purchase);
            await _db.SaveChangesAsync();

            var userPurchases = model.Users.Select(u => new UserPurchase
            {
                PurchaseId = purchase.Id,
                UserId     = u.UserId,
                Sum        = u.Sum,
                Status     = PurchaseStatus.New
            });

            _db.UserPurchases.AddRange(userPurchases);
            await _db.SaveChangesAsync();

            purchase.UserPurchases = await _db.UserPurchases
                                     .Include(up => up.User)
                                     .Where(up => up.PurchaseId == purchase.Id)
                                     .ToListAsync();

            return(purchase);
        }
Пример #2
0
        public async Task <IActionResult> CreatePurchase([FromBody] CreatePurchaseModel model)
        {
            if (model == null)
            {
                throw new ApiException(400, "Invalid request body", ErrorCode.InvalidRequestFormat);
            }

            var purchase = await _purchasesService.CreatePurchase(model, _currentUser.Id);

            return(Created($"/api/purchases/{purchase.Id}", _mapper.Map <MyPurchase>(purchase)));
        }