Ejemplo n.º 1
0
        public async Task <ActionResult <IncomeDto> > CreateIncomeAsync([FromBody] CreateIncomeDto incomeForCreateDto)
        {
            if (incomeForCreateDto.BudgetId == null)
            {
                return(BadRequest("BudgetId is required."));
            }

            var budget = await budgetRepository.GetByIdAsync(incomeForCreateDto.BudgetId.Value);

            if (budget == null)
            {
                return(NotFound($"No budget with id {incomeForCreateDto.BudgetId.Value} found"));
            }

            if (!IsUserAuthorizedForResource(budget))
            {
                return(Unauthorized($"You are not authorized to access budget with id {incomeForCreateDto.BudgetId.Value}"));
            }

            var newIncome = mapper.Map <Income>(incomeForCreateDto);

            incomeRepository.Add(newIncome);

            var saveResult = await incomeRepository.SaveAllAsync();

            if (!saveResult)
            {
                return(BadRequest("Unable to create expense."));
            }

            var incomeForReturn = mapper.Map <IncomeDto>(newIncome);

            return(CreatedAtRoute("GetIncomeAsync", new { id = incomeForReturn.Id }, incomeForReturn));
        }
        public ActionResult Create([Bind(Include = "IncomeID,IncomeName,IncomeAmount,UserID")] Income income)
        {
            if (ModelState.IsValid)
            {
                incomeRepository.Add(income);
                incomeRepository.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.UserID = new SelectList(userRepository.GetUsers(), "UserID", "Name", income.UserID);
            return(View(income));
        }
Ejemplo n.º 3
0
 public void Add(Income income)
 {
     _incomeRepository.Add(income);
 }