public async Task <ActionResult <SpendingViewModel> > Post([FromBody] CreateSpendingViewModel createViewModel) { Domain.Entity.Currency currency = _currencyRepository.Get(createViewModel.CurrencyId); if (currency == null) { return(NotFound("Currency not found")); } Domain.Entity.Spender spender = _spenderRepository.Get(createViewModel.SpenderId); if (spender == null) { return(NotFound("Spender not found")); } Domain.Entity.Spending spendingToCreate = Domain.Entity.Spending.BuildSpending( amount: createViewModel.Amount, comment: createViewModel.Comment, currency: currency, spender: spender, date: createViewModel.Date?.Date, nature: createViewModel.Nature); _spendingService.ValidateNewSpending(spendingToCreate); Domain.Entity.Spending insertedSpending = await _spendingRepository.Insert(spendingToCreate); return(CreatedAtAction(nameof(Get), new { insertedSpending.Id }, insertedSpending.ToViewModel())); }
public async Task <Domain.Entity.Spending> Insert(Domain.Entity.Spending spendingToAdd) { Spending dBSpending = spendingToAdd.ToDb(); EntityEntry <Spending> result = await _applicationDbContext.Spendings.AddAsync(dBSpending); await _applicationDbContext.SaveChangesAsync(); return(result.Entity.ToEntity()); }
public ActionResult <SpendingViewModel> Get(long id) { Domain.Entity.Spending spending = _spendingRepository.Get(id); if (spending == null) { return(NotFound("Spending not found")); } return(Ok(spending.ToViewModel())); }
public static Data.Spending ToDb(this Domain.Entity.Spending spending) { return(new Data.Spending { SpenderId = spending.Spender.Id.Value, Date = spending.Date.Date.Value, Nature = spending.Nature, Amount = spending.Amount.Value.Value, CurrencyId = spending.Currency.Id.Value, Comment = spending.Comment, }); }
public static SpendingViewModel ToViewModel(this Domain.Entity.Spending spending) { return(new SpendingViewModel { Id = spending.Id, SpenderName = spending.Spender?.FullName(), Date = spending.Date.Date, Nature = spending.Nature.ToString(), Amount = spending.Amount.Value, CurrencyName = spending.Currency?.Name, Comment = spending.Comment, }); }