Ejemplo n.º 1
0
        public async Task <IActionResult> Delete(int UserId)
        {
            //TESTAR DEPOIS
            try
            {
                var user = await _userManager.FindByIdAsync(UserId.ToString());

                if (user == null)
                {
                    return(NotFound());
                }

                string token = Request.Headers["Authorization"];
                token = token.Replace("Bearer ", "");

                if (!user.JsonWebToken.Equals(token))
                {
                    return(Unauthorized());
                }
                var decks = await _deckRepository.GetByUserId(user.Id);

                foreach (var deck in decks)
                {
                    var statistic = await _statisticRepository.GetStatisticByDeckIdAsync(deck.Id);

                    await _cardRepository.DeleteAllCardsAsync(deck.Id);

                    await _statisticRepository.DeleteStatisticAsync(statistic.Id, deck.Id);
                }

                await _deckRepository.DeleteAllDecksAsync(user.Id);

                await _userManager.DeleteAsync(user);

                return(Ok());
            }
            catch (System.Exception ex)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"DataBase is fail {ex.Message}"));
            }
        }