public IActionResult GetCategoryById(Guid id)
        {
            if (!_budgetCategoryRepository.CategoryExists(id))
            {
                return(NotFound());
            }

            var categoryFromRepo = _budgetCategoryRepository.GetCategoryById(id);

            var mappedCategory = Mapper.Map <BudgetCategoryDto>(categoryFromRepo);

            return(Ok(mappedCategory));
        }
Example #2
0
        public IActionResult AddExpense(Guid categoryId, [FromBody] ExpenseForAddDto expense)
        {
            if (expense == null)
            {
                return(BadRequest());
            }

            if (!_budgetCategoryRepository.CategoryExists(categoryId))
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var budgetCategory = _budgetCategoryRepository.GetCategoryById(categoryId);

            var expenseToCreate = Mapper.Map <ExpenseEntity>(expense);

            expenseToCreate.BudgetCategory = budgetCategory;

            _expenseRepository.AddExpense(expenseToCreate);

            if (!_expenseRepository.SaveChanges())
            {
                return(StatusCode(500, "The server was unable to handle your request."));
            }

            var createdExpenseForReturn = Mapper.Map <ExpenseDto>(expenseToCreate);

            return(CreatedAtRoute("GetExpenseById", new
                                  { id = createdExpenseForReturn.ExpenseId }, createdExpenseForReturn));
        }