Ejemplo n.º 1
0
        public bool DeleteUserAccount()
        {
            bool delete = false;

            using (var db = DatabaseFactory.OpenDbConnection())
            {
                using (var trans = db.OpenTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    try
                    {
                        #region//remover os canais + tópicos
                        IEnumerable <int> idsChannelsOWner = db.Where <Channel>(ch => ch.OwnerId == NimbusUser.UserId && ch.Visible == true).Select(c => c.Id).ToList();
                        if (idsChannelsOWner.Count() > 0)
                        {
                            ChannelController apiChn = ClonedContextInstance <ChannelController>();
                            foreach (int item in idsChannelsOWner)
                            {
                                apiChn.DeleteChannel(item);
                            }
                        }
                        #endregion

                        #region//remover os votos dados aos canais e canais seguidos
                        db.SqlScalar <int>(@"UPDATE [ChannelUser]
                                                  SET [ChannelUser].[Accepted] = 0, [ChannelUser].[Follow] = 0,[ChannelUser].[Vote] = 0,
                                                      [ChannelUser].[Visible] = 0
                                                  WHERE [ChannelUser].[UserId] = @userId AND 
                                                  ([ChannelUser].[Follow] = 1  OR [ChannelUser].[Accepted] = 1 OR [ChannelUser].[Vote] = 1 OR
                                                   [ChannelUser].[Visible] = 1)", new { userId = NimbusUser.UserId });
                        #endregion

                        #region//remover da tabela moderador
                        db.SqlScalar <int>(@"UPDATE [Role]
                                                  SET [Role].[Accepted] = 0,[Role].[ChannelMagager] = 0, [Role].[IsOwner] = 0,
                                                      [Role].[MessageManager] = 0,[Role].[ModeratorManager] = 0,[Role].[Paid] = 0,
                                                      [Role].[TopicManager] = 0,[Role].[UserManager] = 0
                                                  WHERE [Role].[UserId] = @userId AND 

                                                  ([Role].[Accepted] = 1  OR [Role].[ChannelMagager] = 1 OR [Role].[IsOwner] = 1 OR
                                                   [Role].[MessageManager] = 1 OR [Role].[ModeratorManager] = 1 OR [Role].[Paid]= 1 OR
                                                   [Role].[TopicManager] = 1 OR [Role].[UserManager] = 1)", new { userId = NimbusUser.UserId });
                        #endregion

                        #region//remover comentários
                        IEnumerable <Comment> comments = db.Where <Comment>(c => c.Visible == true && c.UserId == NimbusUser.UserId).ToList();
                        if (comments.Count() > 0)
                        {
                            CommentController apiCmt = ClonedContextInstance <CommentController>();
                            foreach (var item in comments)
                            {
                                apiCmt.DeleteComment(item);
                            }
                        }
                        #endregion

                        #region //remover da tabela de topicos favoritos
                        db.SqlScalar <int>(@"UPDATE [UserTopicFavorite]
                                                  SET [UserTopicFavorite].[Visible] = 0
                                                  WHERE [UserTopicFavorite].[UserId] = @userId AND [UserTopicFavorite].[Visible] = 1", new { userId = NimbusUser.UserId });
                        #endregion

                        #region//removre os likes dados aos tópicos
                        db.SqlScalar <int>(@"UPDATE [UserLikeTopic]
                                                  SET [UserLikeTopic].[Visible] = 0
                                                  WHERE [UserLikeTopic].[UserId] = @userId AND [UserLikeTopic].[Visible]= 1", new { userId = NimbusUser.UserId });
                        #endregion

                        #region//remover usuário
                        var userCurrent = db.Where <User>(u => u.Id == NimbusUser.UserId).FirstOrDefault();
                        userCurrent.Password = null;

                        db.Update <User>(userCurrent, u => u.Id == NimbusUser.UserId);
                        #endregion

                        trans.Commit();
                        delete = true;
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }

            return(delete);
        }