示例#1
0
        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()));
        }
示例#2
0
        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());
        }
示例#3
0
        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()));
        }
示例#4
0
 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,
     });
 }
示例#5
0
 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,
     });
 }