public async Task <string> DeleteEmojisByType(EmojiType emojiType) { var targetEmojis = this.db.Emojis.Where(x => x.EmojiType == emojiType).ToList(); int count = 0; foreach (var emoji in targetEmojis) { var emojiSkins = this.db.EmojiSkins.Where(x => x.EmojiId == emoji.Id).ToList(); foreach (var emojiSkin in emojiSkins) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.EmojiSkin, emojiSkin.Id), GlobalConstants.EmojisFolder); } ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.EmojiName, emoji.Id), GlobalConstants.EmojisFolder); this.db.EmojiSkins.RemoveRange(emojiSkins); this.db.Emojis.Remove(emoji); this.db.SaveChanges(); await this.db.SaveChangesAsync(); count++; } return(string.Format( SuccessMessages.SuccessfullyDeletedEmojisByType, count, emojiType.ToString().ToUpper())); }
private async Task DeleteOldMessage(string group) { var targetGroup = await this.db.Groups.FirstOrDefaultAsync(x => x.Name.ToLower() == group.ToLower()); if (targetGroup != null) { var messages = this.db.ChatMessages .Where(x => x.GroupId == targetGroup.Id) .OrderBy(x => x.SendedOn) .ToList(); if (messages.Count > GlobalConstants.SavedChatMessagesCount) { var oldMessages = messages.Take(messages.Count - GlobalConstants.SavedChatMessagesCount); foreach (var oldMessageId in oldMessages.Select(x => x.Id).ToList()) { var oldImages = this.db.ChatImages.Where(x => x.ChatMessageId == oldMessageId).ToList(); foreach (var oldImage in oldImages) { ApplicationCloudinary.DeleteImage( this.cloudinary, oldImage.Name, GlobalConstants.PrivateChatImagesFolder); } this.db.ChatImages.RemoveRange(oldImages); } this.db.ChatMessages.RemoveRange(oldMessages); await this.db.SaveChangesAsync(); } } }
public async Task <bool> DeleteUserImagesByUsername(string username) { var user = this.db.Users.FirstOrDefault(x => x.UserName == username); if (user != null && (user.ImageUrl != null || user.CoverImageUrl != null)) { user.ImageUrl = null; ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserProfilePictureName, username), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); user.CoverImageUrl = null; ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserCoverImageName, username), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); this.db.Users.Update(user); await this.db.SaveChangesAsync(); return(true); } return(false); }
public async Task <Tuple <bool, string> > DeleteHolidayTheme(string id) { var targetTheme = await this.db.HolidayThemes.FirstOrDefaultAsync(x => x.Id == id); if (targetTheme != null) { var themeName = targetTheme.Name; var allThemeIcons = this.db.HolidayIcons.Where(x => x.HolidayThemeId == targetTheme.Id).ToList(); foreach (var icon in allThemeIcons) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.HolidayIconName, icon.Id), GlobalConstants.HolidayThemesFolder); } this.db.HolidayIcons.RemoveRange(allThemeIcons); this.db.HolidayThemes.Remove(targetTheme); await this.db.SaveChangesAsync(); return(Tuple.Create( true, string.Format(SuccessMessages.SuccessfullyDeleteHolidayTheme, themeName.ToUpper()))); } return(Tuple.Create(false, ErrorMessages.HolidayThemeDoesNotExist)); }
public async Task <Tuple <bool, string> > DeleteChatTheme(DeleteChatThemeInputModel model) { var targetTheme = await this.db.ChatThemes.FirstOrDefaultAsync(x => x.Id == model.Id); if (targetTheme == null) { return(Tuple.Create( false, string.Format(ErrorMessages.ChatThemeDoesNotAlreadyExist, model.Name.ToUpper()))); } ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.ChatThemeName, targetTheme.Id), GlobalConstants.ChatThemesFolderName); var targetGroups = this.db.Groups.Where(x => x.ChatThemeId == targetTheme.Id); foreach (var group in targetGroups) { group.ChatThemeId = null; } this.db.Groups.UpdateRange(targetGroups); await this.db.SaveChangesAsync(); this.db.ChatThemes.Remove(targetTheme); await this.db.SaveChangesAsync(); return(Tuple.Create( true, string.Format(SuccessMessages.SuccessfullyDeleteChatTheme, model.Name.ToUpper()))); }
public async Task CreateAsyncReturnsExistingViewModelIfRestaurantExists() { await this.AddTestingDestinationToDb(); await this.AddTestingCountryToDb(); DestinationDetailsViewModel destinationDetailsViewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var destinationCreateInputModel = new DestinationCreateInputModel() { Name = TestDestinationName, CountryId = TestCountryId, Image = file, }; destinationDetailsViewModel = await this.DestinationsServiceMock.CreateAsync(destinationCreateInputModel); } ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), destinationDetailsViewModel.Name); var destinationsDbSet = this.DbContext.Destinations.OrderBy(r => r.CreatedOn); Assert.Equal(destinationsDbSet.Last().Id, destinationDetailsViewModel.Id); Assert.Equal(destinationsDbSet.Last().Name, destinationDetailsViewModel.Name); Assert.Equal(destinationsDbSet.Last().Country.Name, destinationDetailsViewModel.CountryName); }
public async Task <int> DeleteAllUsersImages() { int count = 0; var users = this.db.Users.Where(x => x.ImageUrl != null || x.CoverImageUrl != null).ToList(); foreach (var user in users) { if (user.ImageUrl != null) { user.ImageUrl = null; ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserProfilePictureName, user.UserName), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); count++; } if (user.CoverImageUrl != null) { user.CoverImageUrl = null; ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserCoverImageName, user.UserName), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); count++; } } this.db.Users.UpdateRange(users); await this.db.SaveChangesAsync(); return(count); }
public async Task DeleteCandidate(string id) { var targetCandidate = await this.db.JobCandidates.FirstOrDefaultAsync(x => x.Id == id); ApplicationCloudinary.DeleteImage(this.cloudinary, targetCandidate.CvName); this.db.JobCandidates.Remove(targetCandidate); await this.db.SaveChangesAsync(); }
public void DeleteCloudinary(string id) { var settings = this.configuration["CloudSettings"].Split("$"); var cloudName = settings[0]; var apiKey = settings[1]; var apiSec = settings[2]; var account = new Account(cloudName, apiKey, apiSec); var cloud = new Cloudinary(account); ApplicationCloudinary.DeleteImage(cloud, id); }
public async Task <Tuple <string, string> > DeletePost(string id, ApplicationUser user) { var post = this.db.Posts.FirstOrDefault(x => x.Id == id); var userPost = this.db.Users.FirstOrDefault(x => x.Id == post.ApplicationUserId); if (post != null && userPost != null) { if (post.ImageUrl != null) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostCoverImageName, post.Id), GlobalConstants.PostBaseImageFolder); } var allPostImages = this.db.PostImages .Where(x => x.PostId == post.Id) .ToList(); foreach (var postImage in allPostImages) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostImageName, postImage.Id), GlobalConstants.PostBaseImagesFolder); } if (user.Id == post.ApplicationUserId) { this.cyclicActivity.AddUserAction(user, UserActionsType.DeleteOwnPost, user); } else { this.cyclicActivity.AddUserAction(user, UserActionsType.DeletedPost, userPost); this.cyclicActivity.AddUserAction(userPost, UserActionsType.DeletePost, user); } var postActivities = this.db.UserActions.Where(x => x.PostId == post.Id); var comments = this.db.Comments.Where(x => x.PostId == post.Id).ToList(); this.db.Comments.RemoveRange(comments); this.db.UserActions.RemoveRange(postActivities); this.db.PostImages.RemoveRange(allPostImages); this.db.Posts.Remove(post); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyDeletePost)); } return(Tuple.Create("Error", SuccessMessages.SuccessfullyDeletePost)); }
public async Task <Tuple <bool, string> > DeleteEmoji(DeleteEmojiInputModel model) { var emoji = await this.db.Emojis.FirstOrDefaultAsync(x => x.Id == model.Id); var emojiType = emoji.EmojiType; if (emoji != null) { string emojiName = emoji.Name; var emojiSkins = this.db.EmojiSkins.Where(x => x.EmojiId == emoji.Id).ToList(); foreach (var emojiSkin in emojiSkins) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.EmojiSkin, emojiSkin.Id), GlobalConstants.EmojisFolder); } ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.EmojiName, emoji.Id), GlobalConstants.EmojisFolder); this.db.EmojiSkins.RemoveRange(emojiSkins); this.db.Emojis.Remove(emoji); this.db.SaveChanges(); var targetToUpdate = this.db.Emojis .Where(x => x.EmojiType == emojiType) .OrderBy(x => x.Position) .ToList(); for (int i = 0; i < targetToUpdate.Count(); i++) { targetToUpdate[i].Position = i + 1; } this.db.Emojis.UpdateRange(targetToUpdate); await this.db.SaveChangesAsync(); return(Tuple.Create( true, string.Format(SuccessMessages.SuccessfullyDeleteEmoji, emojiName.ToUpper()))); } else { return(Tuple.Create(false, ErrorMessages.EmojiDoesNotExist)); } }
public async Task DeleteJobPosition(string id) { var position = await this.db.JobPositions.FirstOrDefaultAsync(x => x.Id == id); var positionCandidates = this.db.JobCandidates.Where(x => x.JobPositionId == id).ToList(); foreach (var candidate in positionCandidates) { ApplicationCloudinary.DeleteImage(this.cloudinary, candidate.CvName); } this.db.JobCandidates.RemoveRange(positionCandidates); this.db.JobPositions.Remove(position); await this.db.SaveChangesAsync(); }
public async Task ApplyForJob(JobCandidateInputModel model) { var isExist = await this.db.JobCandidates .FirstOrDefaultAsync(x => x.Phonenumber == model.Phonenumber && x.Email == model.Email && x.JobPositionId == model.JobPositionId); if (isExist == null) { isExist = new JobCandidate { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Phonenumber = model.Phonenumber, JobPositionId = model.JobPositionId, }; var cvUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.CV, $"{isExist.Id}-{model.CV.FileName}"); isExist.CvUrl = cvUrl; isExist.CvName = $"{isExist.Id}-{model.CV.FileName}"; this.db.JobCandidates.Add(isExist); } else { isExist.FirstName = model.FirstName; isExist.LastName = model.LastName; ApplicationCloudinary.DeleteImage(this.cloudinary, isExist.CvName); var cvUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, model.CV, $"{isExist.Id}-{model.CV.FileName}"); isExist.CvUrl = cvUrl; isExist.CvName = $"{isExist.Id}-{model.CV.FileName}"; this.db.JobCandidates.Update(isExist); } await this.db.SaveChangesAsync(); }
public async Task CreateAsyncAddsRestaurantToDbContext() { await this.AddTestingDestinationToDb(); RestaurantDetailsViewModel restaurantDetailsViewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var restaurantCreateInputModel = new RestaurantCreateInputModel { Name = TestRestaurantName, DestinationId = TestDestinationId, Type = TestRestaurantType, Image = file, Address = TestRestaurantAddress, Seats = TestRestaurantSeats, }; restaurantDetailsViewModel = await this.RestaurantsServiceMock.CreateAsync(restaurantCreateInputModel); } ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), restaurantDetailsViewModel.Name); var restaurantsDbSet = this.DbContext.Restaurants.OrderBy(r => r.CreatedOn); Assert.Collection(restaurantsDbSet, elem1 => { Assert.Equal(restaurantsDbSet.Last().Id, restaurantDetailsViewModel.Id); Assert.Equal(restaurantsDbSet.Last().Name, restaurantDetailsViewModel.Name); Assert.Equal(restaurantsDbSet.Last().DestinationId, restaurantDetailsViewModel.DestinationId); Assert.Equal(restaurantsDbSet.Last().Destination.Name, restaurantDetailsViewModel.DestinationName); Assert.Equal(restaurantsDbSet.Last().Address, restaurantDetailsViewModel.Address); Assert.Equal(restaurantsDbSet.Last().Type.ToString(), restaurantDetailsViewModel.Type); Assert.Equal(restaurantsDbSet.Last().ImageUrl, restaurantDetailsViewModel.ImageUrl); }); }
public async Task CreateAsyncAddsActivityToDbContext() { await this.AddTestingCountryToDb(); await this.AddTestingDestinationToDb(); ActivityDetailsViewModel activityDetailsViewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var activityCreateInputModel = new ActivityCreateInputModel() { Name = TestActivityName, DestinationId = TestDestinationId, Type = TestActivityType, Image = file, Date = this.testDate, }; activityDetailsViewModel = await this.ActivitiesServiceMock.CreateAsync(activityCreateInputModel); } ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), activityDetailsViewModel.Name); var activitiesDbSet = this.DbContext.Activities.OrderBy(r => r.CreatedOn); Assert.Collection(activitiesDbSet, elem1 => { Assert.Equal(activitiesDbSet.Last().Id, activityDetailsViewModel.Id); Assert.Equal(activitiesDbSet.Last().Name, activityDetailsViewModel.Name); Assert.Equal(activitiesDbSet.Last().DestinationId, activityDetailsViewModel.DestinationId); Assert.Equal(activitiesDbSet.Last().Type.ToString(), activityDetailsViewModel.Type); Assert.Equal(activitiesDbSet.Last().ImageUrl, activityDetailsViewModel.ImageUrl); }); }
public async Task EditAsyncEditsRestaurantsImage() { await this.AddTestingDestinationToDb(); this.DbContext.Restaurants.Add(new Restaurant { Id = TestRestaurantId, Name = TestRestaurantName, DestinationId = TestDestinationId, Type = RestaurantType.Bar, Address = TestRestaurantAddress, Seats = TestRestaurantSeats, ImageUrl = TestImageUrl, }); await this.DbContext.SaveChangesAsync(); using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var restaurantEditViewModel = new RestaurantEditViewModel() { Id = TestRestaurantId, Name = TestRestaurantName, DestinationId = TestDestinationId, Type = TestRestaurantType, Address = TestRestaurantAddress, Seats = TestRestaurantSeats, NewImage = file, }; await this.RestaurantsServiceMock.EditAsync(restaurantEditViewModel); ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), restaurantEditViewModel.Name); } Assert.NotEqual(TestImageUrl, this.DbContext.Restaurants.Find(TestRestaurantId).ImageUrl); }
public async Task <Tuple <bool, string> > DeleteChatStickerType(DeleteChatStickerTypeInputModel model) { var targetStickerType = await this.db.StickerTypes.FirstOrDefaultAsync(x => x.Id == model.Id); if (targetStickerType != null) { var favouriteStickerTypes = this.db.FavouriteStickers .Where(x => x.StickerTypeId == targetStickerType.Id) .ToList(); string name = targetStickerType.Name; int count = 0; var allStickers = this.db.Stickers.Where(x => x.StickerTypeId == targetStickerType.Id).ToList(); foreach (var sticker in allStickers) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.StickerName, sticker.Id), GlobalConstants.StickersFolder); count++; } ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.StickerTypeName, targetStickerType.Id), GlobalConstants.StickerTypeFolder); this.db.FavouriteStickers.RemoveRange(favouriteStickerTypes); this.db.Stickers.RemoveRange(allStickers); this.db.StickerTypes.Remove(targetStickerType); await this.db.SaveChangesAsync(); return(Tuple.Create( true, string.Format(SuccessMessages.SuccessfullyDeleteChatStickerType, name.ToUpper(), count))); } return(Tuple.Create(false, ErrorMessages.StickerTypeDoesNotExist)); }
public async Task <Tuple <string, int> > RemoveSection(string id) { var targetSection = await this.db.Sections.FirstOrDefaultAsync(x => x.Id == id); var targetParts = this.db.SectionParts.Where(x => x.SectionId == id).ToList(); var targetTexts = this.db.PartTexts.Where(x => x.SectionId == id).ToList(); var targetImages = this.db.PartImages.Where(x => x.SectionId == id).ToList(); var removedParts = 0; var removedSectionName = targetSection.Name; this.db.PartTexts.RemoveRange(targetTexts); foreach (var targetPart in targetParts) { var partImages = this.db.PartImages.Where(x => x.SectionPartId == targetPart.Id).ToList(); var partTexts = this.db.PartTexts.Where(x => x.SectionPartId == targetPart.Id).ToList(); foreach (var partImage in partImages) { ApplicationCloudinary.DeleteImage(this.cloudinary, partImage.Name); } this.db.PartImages.RemoveRange(partImages); this.db.PartTexts.RemoveRange(partTexts); this.db.SectionParts.Remove(targetPart); removedParts++; } foreach (var targetImage in targetImages) { ApplicationCloudinary.DeleteImage(this.cloudinary, targetImage.Name); } this.db.PartImages.RemoveRange(targetImages); this.db.Sections.Remove(targetSection); await this.db.SaveChangesAsync(); return(Tuple.Create(removedSectionName, removedParts)); }
public async Task <Tuple <bool, string> > DeleteChatSticker(DeleteChatStickerInputModel model) { var targetSticker = await this.db.Stickers.FirstOrDefaultAsync(x => x.Id == model.Id); if (targetSticker != null) { string name = targetSticker.Name; ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.StickerName, model.Id), GlobalConstants.StickersFolder); this.db.Stickers.Remove(targetSticker); await this.db.SaveChangesAsync(); return(Tuple.Create( true, string.Format(SuccessMessages.SuccessfullyDeleteChatSticker, name.ToUpper()))); } return(Tuple.Create(false, ErrorMessages.StickerDoesNotExist)); }
public async Task EditAsyncEditsRestaurantsImage() { await this.AddTestingDestinationToDb(); this.DbContext.Activities.Add(new Activity { Id = TestActivityId, Name = TestActivityName, DestinationId = TestDestinationId, Type = ActivityType.Adventure, ImageUrl = TestImageUrl, }); await this.DbContext.SaveChangesAsync(); using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var activityToEditViewModel = new ActivityEditViewModel() { Id = TestActivityId, Name = TestActivityName, DestinationId = TestDestinationId, Type = TestActivityType, NewImage = file, }; await this.ActivitiesServiceMock.EditAsync(activityToEditViewModel); ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), activityToEditViewModel.Name); } Assert.NotEqual(TestImageUrl, this.DbContext.Activities.Find(TestActivityId).ImageUrl); }
public async Task EditAsyncEditsDestinationsImage() { await this.AddTestingCountryToDb(); this.DbContext.Destinations.Add(new Destination { Id = TestDestinationId, Name = TestDestinationName, CountryId = TestCountryId, ImageUrl = TestImageUrl, }); await this.DbContext.SaveChangesAsync(); using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var destinationEditViewModel = new DestinationEditViewModel() { Id = TestDestinationId, Name = TestDestinationName, CountryId = TestCountryId, NewImage = file, }; await this.DestinationsServiceMock.EditAsync(destinationEditViewModel); ApplicationCloudinary.DeleteImage(ServiceProvider.GetRequiredService <Cloudinary>(), destinationEditViewModel.Name); } Assert.NotEqual(TestImageUrl, this.DbContext.Destinations.Find(TestDestinationId).ImageUrl); }
public async Task <IActionResult> OnPostAsync() { var user = await this.userManager.GetUserAsync(this.User); if (user == null) { return(this.NotFound($"Unable to load user with ID '{this.userManager.GetUserId(this.User)}'.")); } this.RequirePassword = await this.userManager.HasPasswordAsync(user); if (this.RequirePassword) { if (!await this.userManager.CheckPasswordAsync(user, this.Input.Password)) { this.ModelState.AddModelError(string.Empty, "Incorrect password."); return(this.Page()); } } var comments = this.db.Comments.Where(x => x.ApplicationUserId == user.Id); foreach (var comment in comments) { await this.commentService.DeleteCommentById(comment.Id); } this.db.SaveChanges(); var posts = this.db.Posts.Where(x => x.ApplicationUserId == user.Id).ToList(); foreach (var post in posts) { var action = this.db.UserActions.Where(x => x.PostId == post.Id).ToList(); var postComments = this.db.Comments.Where(x => x.PostId == post.Id).ToList(); foreach (var comment in postComments) { await this.commentService.DeleteCommentById(comment.Id); } this.db.UserActions.RemoveRange(action); } var followFollowed = this.db.FollowUnfollows .Where(x => x.PersonId == user.Id || x.FollowerId == user.Id) .ToList(); var likes = this.db.PostsLikes .Where(x => x.UserId == user.Id) .ToList(); var recommendedFriends = this.db.RecommendedFriends .Where(x => x.ApplicationUserId == user.Id || x.RecommendedUsername == user.UserName) .ToList(); var chatGroups = this.db.Groups .Where(x => x.Name.ToLower().Contains(user.UserName.ToLower())) .ToList(); var chatMessages = this.db.ChatMessages .Where(x => x.ReceiverUsername == user.UserName || x.ApplicationUserId == user.Id) .ToList(); foreach (var chatMessage in chatMessages) { var targetImages = this.db.ChatImages.Where(x => x.ChatMessageId == chatMessage.Id).ToList(); foreach (var targetImage in targetImages) { ApplicationCloudinary.DeleteImage( this.cloudinary, targetImage.Name, GlobalConstants.PrivateChatImagesFolder); } this.db.ChatImages.RemoveRange(targetImages); this.db.SaveChanges(); } var ratings = this.db.UserRatings .Where(x => x.RaterUsername == user.UserName || x.Username == user.UserName) .ToList(); ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserProfilePictureName, user.UserName), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryUserCoverImageName, user.UserName), string.Format(GlobalConstants.UserProfilePicturesFolder, user.UserName)); foreach (var post in posts) { var allPostImages = this.db.PostImages .Where(x => x.PostId == post.Id) .ToList(); foreach (var postImage in allPostImages) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostImageName, postImage.Id), GlobalConstants.PostBaseImagesFolder); } ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostCoverImageName, post.Id), GlobalConstants.PostBaseImageFolder); } this.db.PostsLikes.RemoveRange(likes); this.db.FollowUnfollows.RemoveRange(followFollowed); this.db.Posts.RemoveRange(posts); this.db.RecommendedFriends.RemoveRange(recommendedFriends); this.db.Groups.RemoveRange(chatGroups); this.db.ChatMessages.RemoveRange(chatMessages); this.db.UserRatings.RemoveRange(ratings); await this.db.SaveChangesAsync(); var result = await this.userManager.DeleteAsync(user); var userId = await this.userManager.GetUserIdAsync(user); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'."); } await this.signInManager.SignOutAsync(); this.logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId); return(this.Redirect("~/")); }
public async Task <Tuple <string, string> > EditProduct(EditProductInputModel inputModel) { var product = await this.db.Products.FirstOrDefaultAsync(x => x.Id == inputModel.Id); if (product != null) { var category = await this.db.ProductCategories .FirstOrDefaultAsync(x => x.Title.ToLower() == inputModel.ProductCategory.ToLower()); if (category != null) { product.ProductCategoryId = category.Id; product.Name = inputModel.Name; product.Description = inputModel.SanitaizedDescription; product.AvailableQuantity = inputModel.AvailableQuantity; product.SpecificationsDescription = inputModel.SanitaizedSpecifications; product.Price = inputModel.Price; product.UpdatedOn = DateTime.UtcNow; if (inputModel.ProductImages.Count != 0) { var images = this.db.ProductImages.Where(x => x.ProductId == inputModel.Id).ToList(); foreach (var image in images) { ApplicationCloudinary.DeleteImage(this.cloudinary, image.Name); } this.db.ProductImages.RemoveRange(images); await this.db.SaveChangesAsync(); for (int i = 0; i < inputModel.ProductImages.Count(); i++) { var imageName = string.Format(GlobalConstants.ProductImageName, product.Id, i); var imageUrl = await ApplicationCloudinary.UploadImage( this.cloudinary, inputModel.ProductImages.ElementAt(i), imageName); if (imageUrl != null) { var image = new ProductImage { ImageUrl = imageUrl, Name = imageName, ProductId = product.Id, }; product.ProductImages.Add(image); } } } this.db.Products.Update(product); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyEditedProduct)); } return(Tuple.Create("Error", ErrorMessages.InvalidInputModel)); } return(Tuple.Create("Error", ErrorMessages.InvalidInputModel)); }
public async Task <Tuple <string, string> > DeletePost(string id, ApplicationUser user) { var post = this.db.Posts .Include(x => x.ApplicationUser) .AsSplitQuery() .FirstOrDefault(x => x.Id == id); if (post != null) { if (post.ImageUrl != null) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostCoverImageName, post.Id), GlobalConstants.PostBaseImageFolder); } var allPostImages = this.db.PostImages .Where(x => x.PostId == post.Id) .ToList(); foreach (var postImage in allPostImages) { ApplicationCloudinary.DeleteImage( this.cloudinary, string.Format(GlobalConstants.CloudinaryPostImageName, postImage.Id), GlobalConstants.PostBaseImagesFolder); } if (user.Id == post.ApplicationUserId) { this.nonCyclicActivity.AddUserAction(new DeleteOwnPostUserAction { ApplicationUser = user, ApplicationUserId = user.Id, ShortContent = post.ShortContent, Title = post.Title, }); } else { this.nonCyclicActivity.AddUserAction(new DeletedPostUserAction { DeleterApplicationUser = user, DeleterApplicationUserId = user.Id, ApplicationUser = post.ApplicationUser, ApplicationUserId = post.ApplicationUser.Id, ShortContent = post.ShortContent, Title = post.Title, }); this.nonCyclicActivity.AddUserAction(new DeletePostUserAction { ApplicationUser = user, ApplicationUserId = user.Id, ShortContent = post.ShortContent, Title = post.Title, }); } var postActivities = this.db.UserActions .Where(x => x.BaseUserAction.BaseBlogAction.PostId == post.Id) .ToList(); var comments = this.db.Comments.Where(x => x.PostId == post.Id).ToList(); this.db.Comments.RemoveRange(comments); this.db.UserActions.RemoveRange(postActivities); this.db.PostImages.RemoveRange(allPostImages); this.db.Posts.Remove(post); await this.db.SaveChangesAsync(); return(Tuple.Create("Success", SuccessMessages.SuccessfullyDeletePost)); } return(Tuple.Create("Error", SuccessMessages.SuccessfullyDeletePost)); }