public async Task <IActionResult> AddExpense([FromBody] AddExpenseRequest request)
        {
            var expense = request.ToDto();
            await _expenseManerger.AddExpenseAsync(expense);

            return(Created(Request.Path, expense.WrapResponse(Request.Path)));
        }
Beispiel #2
0
        public async Task <IActionResult> Add(AddExpenseRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(Failure());
            }

            return(await _expenseService.Add(request));
        }
 public static ExpenseDto ToDto(this AddExpenseRequest addExpenseRequest)
 {
     return(new ExpenseDto
     {
         ExpenseCategoryId = addExpenseRequest.ExpenseCategoryId
                             ?? throw new NullReferenceException($"{nameof(addExpenseRequest.ExpenseCategoryId)} Was Nulll"),
         Title = addExpenseRequest.Title,
         Amount = addExpenseRequest.Amount
                  ?? throw new NullReferenceException($"{nameof(addExpenseRequest.Amount)} Was Nulll"),
         From = addExpenseRequest.From
                ?? throw new NullReferenceException($"{nameof(addExpenseRequest.From)} Was Nulll"),
         To = addExpenseRequest.To
              ?? throw new NullReferenceException($"{nameof(addExpenseRequest.To)} Was Nulll")
     });
        public async Task <ActionResult <ExpenseModel> > Post(AddExpenseRequest request)
        {
            var result = await _addExpenseHandler.Handle(
                new AddExpenseParameters(
                    this.GetUserId(),
                    request.Name,
                    request.Amount,
                    request.AddedDate,
                    aggregateId : request.AggregateId,
                    expenseTypeId : request.ExpenseTypeId));

            return(result.Match <ActionResult>(
                       success => Ok(result.Result),
                       error => BadRequest(error)));
        }
        public async Task <IActionResult> Add(AddExpenseRequest request)
        {
            var userId = _jwtService.GetUserId();

            if (request.Amount < 0)
            {
                return(Failure());
            }
            var expense = new Expense()
            {
                CategoryId = request.CategoryId,
                UserId     = userId,
                Amount     = request.Amount,
                CurrencyId = request.CurrencyId
            };

            _context.Expenses.Add(expense);
            await _context.SaveChangesAsync();

            var addedExpense = _context.Expenses.Where(i => i.Id == expense.Id).Include(i => i.Currency).Include(i => i.Category).FirstOrDefault();

            return(Success(ConvertToVM(addedExpense)));
        }