public async Task <IActionResult> DeleteQuoteConfirmed(int id)
        {
            //grab everthing we need from the database
            AttachedQuote quote = await _context.AttachedQuote.SingleOrDefaultAsync(q => q.Id == id);

            CapFundingRequest request = await _context.CapFundingRequests.SingleOrDefaultAsync(r => r.Id == quote.CapFundingRequestId);

            List <QuoteAttachments> attachments = await _context.QuoteAttachments.Where(a => a.AttachedQuoteId == id).ToListAsync();

            foreach (var attachment in attachments)
            {
                if (attachment.FileLocation != null || attachment.FileLocation != "")
                {
                    System.IO.File.Delete(attachment.FileLocation);
                }
                _context.QuoteAttachments.Remove(attachment);
            }

            _context.AttachedQuote.Remove(quote);
            await _context.SaveChangesAsync();

            object getNewViewModel = await CreateViewModel(request);

            return(View("FundingRequestReview", getNewViewModel));
        }
        public async Task <IActionResult> DeleteQuote(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            AttachedQuote attachment = await _context.AttachedQuote
                                       .SingleOrDefaultAsync(q => q.Id == id);

            if (attachment == null)
            {
                return(NotFound());
            }

            return(View(attachment));
        }
        public async Task <IActionResult> EditQuote(int id)
        {
            AttachedQuote quote = await _context.AttachedQuote.SingleOrDefaultAsync(q => q.Id == id);

            CapFundingRequest request = await _context.CapFundingRequests.SingleOrDefaultAsync(r => r.Id == quote.CapFundingRequestId);

            if (quote == null)
            {
                return(NotFound());
            }
            else
            {
                FundingRequestViewModel viewModel = new FundingRequestViewModel
                {
                    CapFundingRequest = request,
                    AttachedQuote     = quote,
                    QuoteAttachments  = await _context.QuoteAttachments.Where(a => a.AttachedQuoteId == id).ToListAsync()
                };
                return(View("QuoteForm", viewModel));
            }
        }
        public async Task <IActionResult> SaveQuote(FundingRequestViewModel request, List <IFormFile> files)
        {
            int           requestId        = request.CapFundingRequest.Id;
            AttachedQuote objAttachedQuote = request.AttachedQuote;

            //Check if there is a file and if its in the right format
            if (files.Count > 0)
            {
                foreach (IFormFile formFile in files)
                {
                    string ext = Path.GetExtension(formFile.FileName);
                    if (formFile.Length < 0 || ext != ".pdf")
                    {
                        TempData["ErrorMessage"] = "Files not in the correct format.";
                        return(RedirectToAction(nameof(Details), new { request.CapFundingRequest.Id }));
                    }
                }
            }

            //write to the database now that we know
            if (objAttachedQuote.Id == 0)
            {
                objAttachedQuote.CapFundingRequestId = request.CapFundingRequest.Id;
                await _context.AttachedQuote.AddAsync(objAttachedQuote);

                if (files.Count > 0)
                {
                    await _context.SaveChangesAsync();

                    AttachedQuote LastQuote = await _context.AttachedQuote.LastAsync();

                    int quoteId = LastQuote.Id;
                    //build path to folder to store file
                    string dir = Path.Combine("E:\\ApplicationDocuments\\FundingRequests\\Quotes\\",
                                              request.CapFundingRequest.ProjectName);
                    Directory.CreateDirectory(dir); //this only happens when the directory doesnt exist
                    foreach (IFormFile formFile in files)
                    {
                        string fileName = Path.GetFileName(formFile.FileName);
                        string filePath = Path.Combine(dir, fileName);

                        using (FileStream stream = new FileStream(filePath, FileMode.Create))
                        {
                            await formFile.CopyToAsync(stream);

                            QuoteAttachments quoteAttachment = new QuoteAttachments
                            {
                                FileName        = formFile.FileName,
                                FileLocation    = Path.Combine(dir, formFile.FileName),
                                AttachedQuoteId = quoteId,
                            };
                            await _context.QuoteAttachments.AddAsync(quoteAttachment);
                        }
                    }
                }
            }
            else
            {
                AttachedQuote quoteInDb = await _context.AttachedQuote.SingleAsync(c => c.Id == objAttachedQuote.Id);

                quoteInDb.VendorName  = request.AttachedQuote.VendorName;
                quoteInDb.ContactNum  = request.AttachedQuote.ContactNum;
                quoteInDb.ContactName = request.AttachedQuote.ContactName;
                quoteInDb.QuoteAmt    = request.AttachedQuote.QuoteAmt;
                quoteInDb.QuoteDate   = request.AttachedQuote.QuoteDate;

                if (files.Count > 0)
                {
                    List <QuoteAttachments> attachmentInDb = await _context.QuoteAttachments
                                                             .Where(a => a.AttachedQuoteId == quoteInDb.Id).ToListAsync();

                    //first we will loop through the attachments and delete them
                    foreach (var attachment in attachmentInDb)
                    {
                        if (attachment.FileLocation != null || attachment.FileLocation != "")
                        {
                            System.IO.File.Delete(attachment.FileLocation);
                        }
                        _context.QuoteAttachments.Remove(attachment);
                    }
                    //now that we have cleared it all lets upload the new attachments
                    //build path to folder to store file
                    string dir = Path.Combine("E:\\ApplicationDocuments\\FundingRequests\\Quotes\\",
                                              request.CapFundingRequest.ProjectName);
                    Directory.CreateDirectory(dir); //this only happens when the directory doesnt exist
                    foreach (IFormFile formFile in files)
                    {
                        string fileName = Path.Combine(formFile.FileName);
                        string filePath = Path.Combine(dir, fileName);

                        using (FileStream stream = new FileStream(filePath, FileMode.Create))
                        {
                            await formFile.CopyToAsync(stream);

                            QuoteAttachments quoteAttachment = new QuoteAttachments
                            {
                                FileName        = formFile.FileName,
                                FileLocation    = Path.Combine(dir, formFile.FileName),
                                AttachedQuoteId = quoteInDb.Id,
                            };
                            await _context.QuoteAttachments.AddAsync(quoteAttachment);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Details), new { request.CapFundingRequest.Id }));
        }