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 async Task <IActionResult> CreateIncome([FromBody] CreateIncomeDto createIncomeDto) { var user = await _userService.GetById(int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier))); if (createIncomeDto.PlannerId != user.PlannerId) { return(BadRequest()); } await _incomeService.Create(createIncomeDto); return(Ok()); }
public IActionResult Create([FromBody] CreateIncomeDto creatrIncomeDto) { try { var income = _mapper.Map <Income>(creatrIncomeDto); income.UserId = AuthoticateUserId(); this._context.Add(income); this._context.SaveChanges(); this._context.Entry(income).Reference(c => c.User).Load(); this._context.Entry(income).Reference(c => c.IncomeType).Load(); return(Ok(_mapper.Map <IncomeDto>(income))); } catch (Exception ex) { _logging.WriteExption(ex); return(BadRequest()); } }