示例#1
0
        public async Task <IActionResult> DeleteDebt([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 debtInDb = await _debtService.GetDebtAsync(debtId);

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

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

            var deleteDebt = await _debtService.DeleteDebtAsync(debtInDb);

            if (!deleteDebt)
            {
                return(BadRequest(new ErrorResponse(new ErrorModel {
                    Message = $"Could not delete debt with id:{debtId}"
                })));
            }

            return(NoContent());
        }