Esempio n. 1
0
        public async Task <IActionResult> GetDebt([FromRoute] int debtId)
        {
            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}"
                })));
            }

            var debt = await _debtService.GetDebtAsync(debtId);

            if (debt == null)
            {
                return(NotFound(new ErrorResponse(new ErrorModel {
                    Message = $"There is no debt with id: {debtId}"
                })));
            }

            if (debt.UserId != userId)
            {
                return(Forbid());
            }

            return(Ok(new Response <DebtResponse>(_mapper.Map <DebtResponse>(debt))));
        }