예제 #1
0
        private void ValidateDoesNotAlreadyExists(Entity.Spending spendingToValidate)
        {
            if (spendingToValidate.Spender?.Id == null)
            {
                throw new ValidationException("Spending not valid: No Spender");
            }

            IList <Entity.Spending> spenderSpendings = _spendingRepository.GetFromSpender(spendingToValidate.Spender.Id.Value);

            if (spenderSpendings.Any(sp => sp.Amount.IsEquals(spendingToValidate.Amount) &&
                                     sp.Date.IsEquals(spendingToValidate.Date)))
            {
                throw new ValidationException("Spending not valid: A spending that has the same amount and date already exists");
            }
        }
예제 #2
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));
            }
        }