public async Task <IActionResult> Create(int userId, IncomeForCreationDto incomeForCreationDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // incomeForCreationDto.Date = DateTime.Now; incomeForCreationDto.UserId = userId; var incomeToCreate = _mapper.Map <Income>(incomeForCreationDto); _commonRepo.Add(incomeToCreate); if (await _commonRepo.SaveAll()) { var incomeToReturn = _mapper.Map <IncomeToReturnDto>(incomeToCreate); return(CreatedAtRoute("GetIncome", new { id = incomeToCreate.Id }, incomeToReturn)); } throw new Exception("Creating the income failed on save"); }
public async Task <IActionResult> AddIncome(int userId, IncomeForCreationDto IncomeForCreation) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); var income = _mapper.Map <Income>(IncomeForCreation); userFromRepo.Incomes.Add(income); if (await _repo.SaveAll()) { var incomeToReturn = _mapper.Map <IncomeForReturnDto>(income); return(CreatedAtRoute("GetIncome", new { userId = userId, id = income.Id }, incomeToReturn)); } return(BadRequest("Could not add the income.")); }
public async Task <IActionResult> Post([FromBody] IncomeForCreationDto incomeForCreationDto) { if (await this._customerRepo.GetSingle(incomeForCreationDto.CustomerId) == null) { return(BadRequest()); } var income = new Income { CreditBills = incomeForCreationDto.CreditBills, GiveMeLoan = incomeForCreationDto.GiveMeLoan, HouseholdExpense = incomeForCreationDto.HouseholdExpense, MonthlyMortgageOrRent = incomeForCreationDto.MonthlyMortgageOrRent, CustomerId = incomeForCreationDto.CustomerId, MonthlySalary = incomeForCreationDto.MonthlySalary, OtherIncome = incomeForCreationDto.OtherIncome }; var result = await _incomeRepo.Create(income); var incomeDto = _mapper.Map <IncomeDto>(income); return(Ok(incomeDto)); }