Beispiel #1
0
        /// <summary>
        /// Add a new Partner to the database, upload image if given
        /// </summary>
        /// <param name="newPartner"></param>
        /// <param name="image"></param>
        /// <param name="imgUploadPath"></param>
        /// <returns></returns>
        public async Task AddPartner(Partner newPartner, IFormFile image, string imgUploadPath)
        {
            try {
                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                    // Add image to the item
                    if (image != null)
                    {
                        string uploadedImgName = await ImageManager.UploadImage(image, imgUploadPath);

                        if (uploadedImgName != null)
                        {
                            newPartner.Image = new PartnerImage {
                                ImageFileName = uploadedImgName
                            }
                        }
                        ;
                    }
                    // Add item to Database
                    _context.Add(newPartner);
                    await _context.SaveChangesAsync();

                    // Complete the transaction
                    ts.Complete();
                }
            }
            catch (Exception e) {
                throw e;
            }
        }
Beispiel #2
0
        // 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;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Add Tehtud töö to the database and upload images
        /// </summary>
        /// <param name="newTehtudToo"></param>
        /// <param name="images"></param>
        /// <param name="imgUploadPath"></param>
        /// <returns></returns>
        public async Task AddTehtudToo(TehtudToo newTehtudToo, ICollection <IFormFile> images, string imgUploadPath)
        {
            try {
                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                    // Add images to Tehtud töö
                    if (images != null && images.Any())
                    {
                        // instantiate a new image list for Tehtud töö
                        newTehtudToo.Images = new List <TehtudTooImage>();
                        // List for Images to add
                        List <TehtudTooImage> imagesToAdd      = new List <TehtudTooImage>();
                        List <string>         uploadedImgNames = await ImageManager.UploadImages(images, imgUploadPath);

                        foreach (string imgFileName in uploadedImgNames)
                        {
                            imagesToAdd.Add(new TehtudTooImage {
                                ImageFileName = imgFileName
                            });
                        }
                        // Add images to the new Tehtud töö
                        foreach (TehtudTooImage img in imagesToAdd)
                        {
                            newTehtudToo.Images.Add(img);
                        }
                        ;
                    }
                    // Add Tehtud töö to Database
                    _context.Add(newTehtudToo);
                    await _context.SaveChangesAsync();

                    // Complete the transaction
                    ts.Complete();
                }
            }
            catch (Exception e) {
                throw e;
            }
        }