예제 #1
0
        /// <summary>
        /// remove user data
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public override async Task <RServiceResult <bool> > RemoveUserData(Guid userId)
        {
            RMuseumDbContext context = _context as RMuseumDbContext;

            string systemEmail  = $"{Configuration.GetSection("Ganjoor")["SystemEmail"]}";
            var    systemUserId = (Guid)(await FindUserByEmail(systemEmail)).Result.Id;

            if (systemUserId == userId)
            {
                return(new RServiceResult <bool>(false, "تلاش برای حذف کاربر سیستمی"));
            }

            string deletedUserEmail = $"{Configuration.GetSection("Ganjoor")["DeleteUserEmail"]}";
            var    deletedUserId    = (Guid)(await FindUserByEmail(deletedUserEmail)).Result.Id;

            if (deletedUserId == userId)
            {
                return(new RServiceResult <bool>(false, "تلاش برای حذف کاربر سیستمی کاربر حذف شده"));
            }

            var reviewedRecitations = await context.Recitations.Where(r => r.ReviewerId == userId).ToListAsync();

            foreach (var reviewedRecitation in reviewedRecitations)
            {
                reviewedRecitation.ReviewerId = deletedUserId;
            }
            context.UpdateRange(reviewedRecitations);
            await context.SaveChangesAsync(); //some tracking data related bugs makes it necessary to call this especially for same table data processing

            var suggestedCorrections = await context.GanjoorPoemCorrections.Where(c => c.UserId == userId).ToListAsync();

            foreach (var suggestedCorrection in suggestedCorrections)
            {
                suggestedCorrection.UserId = deletedUserId;
            }
            context.UpdateRange(suggestedCorrections);
            await context.SaveChangesAsync();

            var reviewedCorrections = await context.GanjoorPoemCorrections.Where(c => c.ReviewerUserId == userId).ToListAsync();

            foreach (var reviewedCorrection in reviewedCorrections)
            {
                reviewedCorrection.ReviewerUserId = deletedUserId;
            }
            context.UpdateRange(reviewedCorrections);
            await context.SaveChangesAsync();

            var reportedComments = await context.GanjoorReportedComments.Where(r => r.ReportedById == userId).ToListAsync();

            foreach (var reportedComment in reportedComments)
            {
                reportedComment.ReportedById = deletedUserId;
            }
            context.UpdateRange(reportedComments);
            await context.SaveChangesAsync();

            var ganjoorLinks = await context.GanjoorLinks.Where(l => l.SuggestedById == userId).ToListAsync();

            foreach (var ganjoorLink in ganjoorLinks)
            {
                ganjoorLink.SuggestedById = deletedUserId;
            }
            context.UpdateRange(ganjoorLinks);
            await context.SaveChangesAsync();

            var reviewedGanjoorLinks = await context.GanjoorLinks.Where(l => l.ReviewerId == userId).ToListAsync();

            foreach (var reviewedGanjoorLink in reviewedGanjoorLinks)
            {
                reviewedGanjoorLink.ReviewerId = deletedUserId;
            }
            context.UpdateRange(reviewedGanjoorLinks);
            await context.SaveChangesAsync();

            var pinLinks = await context.PinterestLinks.Where(l => l.SuggestedById == userId).ToListAsync();

            foreach (var pinLink in pinLinks)
            {
                pinLink.SuggestedById = deletedUserId;
            }
            context.UpdateRange(pinLinks);
            await context.SaveChangesAsync();

            var reviewedPinLinks = await context.PinterestLinks.Where(l => l.ReviewerId == userId).ToListAsync();

            foreach (var reviewedPinLink in reviewedPinLinks)
            {
                reviewedPinLink.ReviewerId = deletedUserId;
            }
            context.UpdateRange(reviewedPinLinks);
            await context.SaveChangesAsync();

            var poemMusicTracks = await context.GanjoorPoemMusicTracks.Where(m => m.SuggestedById == userId).ToListAsync();

            foreach (var poemMusicTrack in poemMusicTracks)
            {
                poemMusicTrack.SuggestedById = deletedUserId;
            }
            context.UpdateRange(poemMusicTracks);
            await context.SaveChangesAsync();

            var snapshots = await context.GanjoorPageSnapshots.Where(s => s.MadeObsoleteByUserId == userId).ToListAsync();

            foreach (var snapshot in snapshots)
            {
                snapshot.MadeObsoleteByUserId = deletedUserId;
            }
            context.UpdateRange(snapshots);
            await context.SaveChangesAsync();

            var translations = await context.GanjoorPoemTranslations.Where(t => t.UserId == userId).ToListAsync();

            foreach (var translation in translations)
            {
                translation.UserId = deletedUserId;
            }
            context.UpdateRange(translations);
            await context.SaveChangesAsync();

            var suggestedPoetNotes = await context.GanjoorPoetSuggestedSpecLines.Where(s => s.SuggestedById == userId).ToListAsync();

            foreach (var suggestedPoetNote in suggestedPoetNotes)
            {
                suggestedPoetNote.SuggestedById = deletedUserId;
            }
            context.UpdateRange(suggestedPoetNotes);
            await context.SaveChangesAsync();

            var suggestedPoetPhotos = await context.GanjoorPoetSuggestedPictures.Where(s => s.SuggestedById == userId).ToListAsync();

            foreach (var suggestedPoetPhoto in suggestedPoetPhotos)
            {
                suggestedPoetPhoto.SuggestedById = deletedUserId;
            }
            context.UpdateRange(suggestedPoetPhotos);
            await context.SaveChangesAsync();


            var visits = await context.GanjoorUserPoemVisits.Where(v => v.UserId == userId).ToListAsync();

            context.RemoveRange(visits);
            await context.SaveChangesAsync();

            var bookmarks = await context.UserBookmarks.Where(b => b.RAppUserId == userId).ToListAsync();

            context.RemoveRange(bookmarks);
            await context.SaveChangesAsync();

            var uploadSessions = await context.UploadSessions.Where(s => s.UseId == userId).ToListAsync();

            context.RemoveRange(uploadSessions);
            await context.SaveChangesAsync();

            var recitations = await context.Recitations.Where(r => r.OwnerId == userId).ToListAsync();

            context.RemoveRange(recitations);
            await context.SaveChangesAsync();

            var ganjoorBookmarks = await context.GanjoorUserBookmarks.Where(b => b.UserId == userId).ToListAsync();

            context.RemoveRange(ganjoorBookmarks);
            await context.SaveChangesAsync();


            var reportedRecitaions = await context.RecitationErrorReports.Where(r => r.ReporterId == userId).ToListAsync();

            context.RemoveRange(reportedRecitaions);
            await context.SaveChangesAsync();

            var comments = await context.GanjoorComments.Where(c => c.UserId == userId).ToListAsync();

            foreach (var comment in comments)
            {
                //await _ganjoorService.DeleteMyComment(userId, comment.Id);/*had error in service initializtion, so done it in the dirty way*/
                await _DeleteComment(context, comment.Id);
            }

            var recitationsVotes = await context.RecitationUserUpVotes.Where(c => c.UserId == userId).ToListAsync();

            foreach (var vote in recitationsVotes)
            {
                int poemId = await context.Recitations.AsNoTracking().Where(r => r.Id == vote.RecitationId).Select(r => r.GanjoorPostId).SingleAsync();

                context.Remove(vote);
                await context.SaveChangesAsync();
                await _ReOrderPoemRecitationsAsync(context, poemId);
            }


            return(await base.RemoveUserData(userId));//notifications are deleted here, some of these operations might produce new notifications
        }