public IActionResult AddBill([FromBody] AddBillModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var bill = new Bill { Creator = _unitOfWork.UsersRepository.Get(model.CreatorId), Date = model.Date, Description = model.Description, Notes = model.Notes.Select(n => new Note { Id = n.Id, Text = n.Text }).ToList(), Subcategory = _unitOfWork.SubcategoriesRepository.Get(model.SubcategoryId), TotalAmount = model.TotalAmount, UserBills = model.Payers.Select(p => new UserBill { User = _unitOfWork.UsersRepository.Get(p.Id), Amount = p.Amount }).ToList() }; _unitOfWork.BillsRepository.Add(bill); var history = new History { Bill = bill, Creator = bill.Creator, Date = bill.Date, Description = bill.Description, HistoryType = ActionType.Add }; var notifications = new List <Notification>(); foreach (var reader in bill.UserBills) { if (reader.User.Id != bill.Creator.Id) { notifications.Add(new Notification { Readed = false, Reader = reader.User, History = history }); } } _unitOfWork.HistoriesRepository.Add(history); _unitOfWork.NotificationsRepository.AddRange(notifications); SendNotifications(bill, notifications); _unitOfWork.Complete(); return(new OkObjectResult(new { Message = "Rachunek został dodany" })); }
public async Task AddBillAsync(AddBillModel model) { if (model.Amount < 0) { throw new InvalidOperationException("金额不能小于0"); } var bill = model.CopyProperties <Bill>(); _unitOfWork.RegisterAdd(bill); await _unitOfWork.CommitAsync(); }