Ejemplo n.º 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()));
        }
Ejemplo n.º 2
0
 public static Data.Spender ToDb(this Domain.Entity.Spender spender)
 {
     return(new Data.Spender
     {
         FirstName = spender.Person.FirstName,
         LastName = spender.Person.LastName,
         CurrencyId = spender.Currency.Id.Value,
     });
 }
Ejemplo n.º 3
0
        public ActionResult <IEnumerable <SpendingViewModel> > GetFromSpender(long spenderId, [FromQuery] string orderBy)
        {
            Domain.Entity.Spender spender = _spenderRepository.Get(spenderId);
            if (spender == null)
            {
                return(NotFound("Spender not found"));
            }

            IList <Domain.Entity.Spending> spendings = _spendingRepository.GetFromSpender(spenderId);
            IList <SpendingViewModel>      rawResult = spendings.Select(sp => sp.ToViewModel()).ToList();

            switch (orderBy)
            {
            case (nameof(SpendingViewModel.Amount)):
                return(Ok(rawResult.OrderBy(sp => sp.Amount)));

            case (nameof(SpendingViewModel.Date)):
                return(Ok(rawResult.OrderBy(sp => sp.Date)));

            default:
                return(Ok(rawResult));
            }
        }