Esempio n. 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;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add the Item to the database
        /// </summary>
        /// <param name="newItem"></param>
        /// <param name="Images"></param>
        /// <returns></returns>
        public async Task AddItem(Item newItem, ICollection <IFormFile> images, string imgUploadPath)
        {
            // Might not be working the way I want it to but Transactions should ensure that when the file does not get created then it wont add the item to the database
            try {
                using (var ts = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) {
                    // Add images to the item
                    if (images != null && images.Any())
                    {
                        // instantiate a new image list for the item
                        newItem.Images = new List <ItemImage>();
                        // List for Images to add
                        List <ItemImage> imagesToAdd      = new List <ItemImage>();
                        List <string>    uploadedImgNames = await ImageManager.UploadImages(images, imgUploadPath);

                        foreach (string imgFileName in uploadedImgNames)
                        {
                            imagesToAdd.Add(new ItemImage {
                                ImageFileName = imgFileName
                            });
                        }
                        AddImagesToItem(newItem, imagesToAdd);
                    }
                    // Add item to Database
                    _context.Add(newItem);
                    await _context.SaveChangesAsync();

                    // Complete the transaction
                    ts.Complete();
                }
            }
            catch (Exception e) {
                throw e;
            }
        }
Esempio n. 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;
            }
        }