private Expense RegisterSimpleExpense(RegisterExpense simpleExpense, Payee payee) { var entity = (SimpleExpense)simpleExpense; entity.Payee = payee; #region Validations if (simpleExpense.Payers?.Any() != true) { ModelState.AddModelError("Payers", "When registering a Simple Expense, you must specify at least one Payer."); return(null); } #region Validate Payers // TODO: validate duplications before calling the database var payers = _roommates.GetByIds(simpleExpense.Payers.Select(x => x.Id).Distinct()); if (payers.Count() != simpleExpense.Payers.Count()) { ModelState.AddModelError("Payers", "At least one Payer is invalid, does not represent a registered Roommate, or is duplicated."); return(null); } else if (payers.Count() == 1 && payers.First().Id == payee.Id) { ModelState.AddModelError("Payers", "At this moment, self expenses are not supported. Please consider other alternatives."); return(null); } var sum = simpleExpense.Payers.Sum(x => x.Amount * (decimal)x.Multiplier); if (sum > 0) { ModelState.AddModelError("Payers", "An Expense cannot be proportional and even at the same time. Amount and Multiplier cannot be filled at the same time. Please, select only one."); return(null); } entity.Payers = simpleExpense.Payers.Select(x => new Payer { Id = x.Id, Amount = simpleExpense.Distribution.GetAmount(simpleExpense, x), Name = payers.Single(p => p.Id == x.Id).Name }); var total = entity.Payers.Sum(x => x.Amount); if (total != simpleExpense.Total) { ModelState.AddModelError("Payers", "The total amount for this expense and the total amount by payers' distribution differ."); return(null); } #endregion #endregion var result = _expenses.Add(entity); if (result != null) { UpdateBalances(entity.Payers, entity.Payee, entity.Total); } return(result); }
public IActionResult Post([FromBody] Expense expense) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _expensesRepository.Add(expense); return(StatusCode(StatusCodes.Status201Created)); }
public IActionResult CreateExpense(Expense expense) { if (!ModelState.IsValid) { return(BadRequest()); } _repo.Add(expense); _repo.SaveAll(); // var expenseToReturn = _mapper.Map<ExpensesForListDto>(expense); return(Created(new Uri(Request.Path + "/" + expense.Id), expense)); }
public IActionResult AddRandom() { var categories = new[] { "food", "fuel", "clothes", "bills" }; _expensesRepository.Add(new Expense() { Amount = DateTime.Now.Ticks % 100, Category = categories[new Random().Next(0, categories.Length)], Date = DateTime.UtcNow }); return(RedirectToAction(nameof(Index))); }
protected AddEditViewModel() { _expensesRepository = new ExpensesRepository(); RegisterCommand = new RelayCommand <Expense>(async(exp) => { if (!exp.IsValid) { return; } if (exp.Id == Guid.Empty) { exp.Id = Guid.NewGuid(); _expensesRepository.Add(exp); } else { _expensesRepository.Update(exp); } await Application.Current.MainPage.Navigation.PopAsync(animated: true); }, (exp) => exp != null && exp.IsValid); }