public async Task <IActionResult> CreateDebt([FromBody] DebtRequest request) { var userId = User.Claims.FirstOrDefault(c => c.Type == "id").Value; // check if user exists var userExists = await _identityService.CheckIfUserExists(userId); if (!userExists) { return(NotFound(new ErrorResponse(new ErrorModel { Message = $"There is no user with id: {userId}" }))); } // check if new debt budgetId is correct var budget = await _budgetService.GetBudgetAsync(request.BudgetId); if (budget == null) { return(NotFound(new ErrorResponse(new ErrorModel { Message = $"There is no budget with id: {request.BudgetId}" }))); } if (budget.UserId != userId) { return(Forbid()); } // create debt var newDebt = await _debtService.NewDebtAsync(request, userId); if (newDebt == null) { return(BadRequest(new ErrorResponse(new ErrorModel { Message = "Could not create new debt" }))); } var locationUri = _uriService.GetDebtUri(newDebt.Id); return(Created(locationUri, new Response <DebtResponse>(_mapper.Map <DebtResponse>(newDebt)))); }