public async Task EditTehtudToo(TehtudToo editedTehtudToo, List <int> imagesToRemoveIds, ICollection <IFormFile> imagesToAdd, string imgUploadPath) { // Check if Tehtud töö with Edited Tehtud töö ID exists in the database if (!await TehtudTooExists(editedTehtudToo.Id)) { return; } // Transaction for IO not working as of now // try and catch for DbUpdateConcurrencyException try { using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // Check if any images to Add and do so if (imagesToAdd != null && imagesToAdd.Any()) { // List for new Images to add List <TehtudTooImage> newImages = new List <TehtudTooImage>(); // upload the images to the given path List <string> uploadedImgNames = await ImageManager.UploadImages(imagesToAdd, imgUploadPath); foreach (string imgFileName in uploadedImgNames) { newImages.Add(new TehtudTooImage { ImageFileName = imgFileName }); } // Add images to the new Tehtud töö foreach (TehtudTooImage img in newImages) { editedTehtudToo.Images.Add(img); } ; } // Check if any images to remove and do so if (imagesToRemoveIds != null && imagesToRemoveIds.Any()) { foreach (int imageId in imagesToRemoveIds) { if (await IsImageOwnedByGivenTehtudToo(editedTehtudToo.Id, imageId)) { // Remove the image from this list and also add it to the DB queue to query when SaveChanges is called to remove it from DB as well // Have to do both because, if just removing from list. Updating thinks we didn't touch the image at all, // if we would remove the image only from the Database and not from the list right away. The list would add it back:( editedTehtudToo.Images.Remove(editedTehtudToo.Images.First(x => x.Id == imageId)); await RemoveImageWithoutSaveChanges(imageId); } } } _context.Update(editedTehtudToo); await _context.SaveChangesAsync(); ts.Complete(); } } catch (Exception e) { throw e; } }
// Not using .FirstAsync() because it will just bring another variety in the code, possibly making it more complicated. // Should probably use it because it first checks if that item with the given ID is being traced by the context already // e.g. The item is queried to be deleted, which the db context traces. Then if you choose to Delete it right away, it's // already being traced and will not have to be queried again. //public async Task<Item> GetItemById(int? id) { // if (id == null) { // return null; // } // else { // return await _context.Items.FirstAsync(); // } //} /// <summary> /// Edit the Item /// </summary> /// <param name="editedItem"></param> /// <param name="imagesToRemoveIds"></param> /// <param name="imagesToAdd"></param> /// <param name="imgUploadPath"></param> /// <returns></returns> public async Task EditItem(Item editedItem, List <int> imagesToRemoveIds, ICollection <IFormFile> imagesToAdd, string imgUploadPath) { // Check if an item with Edited Item ID exists in the database if (!await ItemExists(editedItem.Id)) { return; } // Transactions should also check for the file IO, if that throws an error then it wont update the database try { using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // Check if any images to Add and do so if (imagesToAdd != null && imagesToAdd.Any()) { // List for new Images to add List <ItemImage> newImages = new List <ItemImage>(); // upload the images to the given path List <string> uploadedImgNames = await ImageManager.UploadImages(imagesToAdd, imgUploadPath); foreach (string imgFileName in uploadedImgNames) { newImages.Add(new ItemImage { ImageFileName = imgFileName }); } AddImagesToItem(editedItem, newImages); } // Check if any images to remove and do so if (imagesToRemoveIds != null && imagesToRemoveIds.Any()) { foreach (int imageId in imagesToRemoveIds) { if (await IsImageOwnedByGivenItem(editedItem.Id, imageId)) { // Remove the image from this list and also add it to the DB queue to query when SaveChanges is called to remove it from DB as well // Have to do both because, if just removing from list. Updating thinks we didn't touch the image at all, // if we would remove the image only from the Database and not from the list right away. The list would add it back:( editedItem.Images.Remove(editedItem.Images.First(x => x.Id == imageId)); await RemoveImageWithoutSaveChanges(imageId); } } } _context.Update(editedItem); await _context.SaveChangesAsync(); ts.Complete(); } } catch (Exception e) { throw e; } }
/// <summary> /// Edit Partner, upload/remove the image /// </summary> /// <param name="editedPartner"></param> /// <param name="image"></param> /// <param name="imgUploadPath"></param> /// <returns></returns> public async Task EditPartner(Partner editedPartner, IFormFile uploadedImage, bool removeImage, string imgUploadPath) { // Stop executing method if Partner with given ID doesn't exist in database if (!await PartnerExists(editedPartner.Id)) { return; } // Transaction for IO not working as of now // try and catch for DbUpdateConcurrencyException try { using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // Get the partners Image, if has none then return null PartnerImage partnerImage = await _context.PartnerImages.Where(img => img.Id == editedPartner.Id).FirstOrDefaultAsync(); // Check if user posted a new image to form if (uploadedImage != null) { // Remove current image from database if there's one string uploadedImgName = await ImageManager.UploadImage(uploadedImage, imgUploadPath); editedPartner.Image = new PartnerImage { ImageFileName = uploadedImgName }; // If Partner already has an image then update its file name to the new one, otherwise the new image will be added by updating Partners entity if (partnerImage != null) { partnerImage.ImageFileName = uploadedImgName; } } // Check if the image has to be removed and do so else if (removeImage) { if (partnerImage != null) { _context.PartnerImages.Remove(partnerImage); } } _context.Update(editedPartner); await _context.SaveChangesAsync(); ts.Complete(); } } catch (Exception e) { throw e; } }