public async Task <IActionResult> Edit(Guid id, [Bind("TLMDistributionId,RegistrationDate,TLMDistributionPeriodId,OrganizationIdFrom,OrganizationIdTo,ShippedDate,ReceivedDate,ReceivedBy,TrackingCode,RefTLMDistributionStatusId,Url,FileName,ParentTLMDistributionId")] TLMDistribution tLMDistribution)
        {
            if (id != tLMDistribution.TLMDistributionId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(tLMDistribution);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TLMDistributionExists(tLMDistribution.TLMDistributionId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                TempData["messageType"]  = "success";
                TempData["messageTitle"] = "RECORD UPDATED";
                TempData["message"]      = "Record successfully updated";

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction(nameof(Index), new { id = tLMDistribution.TLMDistributionPeriodId }));
            }
            ViewData["RefTLMDistributionStatusId"] = new SelectList(_context.TLMDistributionStatus, "RefTLMDistributionStatusId", "DistributionStatus", tLMDistribution.RefTLMDistributionStatusId);
            ViewData["OrganizationIdFrom"]         = new SelectList(_context.Organizations, "OrganizationId", "OrganizationCode", tLMDistribution.OrganizationIdFrom);
            ViewData["OrganizationIdTo"]           = new SelectList(_context.Organizations, "OrganizationId", "OrganizationCode", tLMDistribution.OrganizationIdTo);
            ViewData["ParentTLMDistributionId"]    = new SelectList(_context.TLMDistributions, "TLMDistributionId", "TrackingCode", tLMDistribution.ParentTLMDistributionId);
            ViewData["TLMDistributionPeriodId"]    = new SelectList(_context.TLMDistributionPeriods, "TLMDistributionPeriodId", "PeriodName", tLMDistribution.TLMDistributionPeriodId);
            ViewData["ParentId"] = tLMDistribution.TLMDistributionPeriodId;
            return(View(tLMDistribution));
        }
        public async Task <IActionResult> Create([Bind("TLMDistributionId,RegistrationDate,TLMDistributionPeriodId,OrganizationIdFrom,OrganizationIdTo,ShippedDate,ReceivedDate,ReceivedBy,TrackingCode,RefTLMDistributionStatusId,Url,FileName,ParentTLMDistributionId")] TLMDistribution tLMDistribution)
        {
            if (ModelState.IsValid)
            {
                tLMDistribution.TLMDistributionId = Guid.NewGuid();
                _context.Add(tLMDistribution);
                await _context.SaveChangesAsync();

                TempData["messageType"]  = "success";
                TempData["messageTitle"] = "RECORD CREATED";
                TempData["message"]      = "New record successfully created";

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction(nameof(Index), new { id = tLMDistribution.TLMDistributionPeriodId }));
            }
            ViewData["RefTLMDistributionStatusId"] = new SelectList(_context.TLMDistributionStatus, "RefTLMDistributionStatusId", "DistributionStatus", tLMDistribution.RefTLMDistributionStatusId);
            ViewData["OrganizationIdFrom"]         = new SelectList(_context.Organizations, "OrganizationId", "OrganizationCode", tLMDistribution.OrganizationIdFrom);
            ViewData["OrganizationIdTo"]           = new SelectList(_context.Organizations, "OrganizationId", "OrganizationCode", tLMDistribution.OrganizationIdTo);
            ViewData["ParentTLMDistributionId"]    = new SelectList(_context.TLMDistributions, "TLMDistributionId", "TrackingCode", tLMDistribution.ParentTLMDistributionId);
            ViewData["TLMDistributionPeriodId"]    = new SelectList(_context.TLMDistributionPeriods, "TLMDistributionPeriodId", "PeriodName", tLMDistribution.TLMDistributionPeriodId);
            ViewData["ParentId"] = tLMDistribution.TLMDistributionPeriodId;
            return(View(tLMDistribution));
        }
        public async Task <IActionResult> Upload(IFormFile file, Guid?id)
        {
            if (file == null)
            {
                return(NotFound());
            }

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

            TLMDistribution tLMDistribution = await _context.TLMDistributions.FindAsync(id);

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

            long size = file.Length;

            // Get folder path URL = ~/wwwroot/uploads
            var filePath = Path.Combine(_environment.WebRootPath, "uploads");

            if (size > 0)
            {
                // Limit file uploads to 50M
                if (size > 52428800)
                {
                    TempData["messageType"]  = "error";
                    TempData["messageTitle"] = "FILE NOT UPLOADED";
                    TempData["message"]      = "File size exceeded the maximum size permitted (50MB)";
                    return(RedirectToAction("Details", "TLMDistributions", new { id }));
                }

                // Limit file types to .jpg, .jpeg, .png, .doc, and .pdf only
                if (!CheckFileType(file.FileName))
                {
                    TempData["messageType"]  = "error";
                    TempData["messageTitle"] = "FILE NOT UPLOADED";
                    TempData["message"]      = "File type not valid. Upload PDF, Documents, or Images";
                    return(RedirectToAction("Details", "TLMDistributions", new { id }));
                }

                // Current file extension
                string extension = Path.GetExtension(file.FileName);

                // Set Filename format (i.e. Document-20190815141805-{id}{.pdf})
                var fileName = "Document" + DateTime.Now.ToString("-yyyyMMddHHmmss-") + id + extension;

                // Upload file to server
                using (var stream = new FileStream(Path.Combine(filePath, fileName), FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                // Build host URL and /uploads/ path
                var request    = HttpContext.Request;
                var uriBuilder = new UriBuilder
                {
                    Host   = request.Host.Host,
                    Scheme = request.Scheme,
                    Path   = "uploads/" + fileName
                };

                if (request.Host.Port.HasValue)
                {
                    uriBuilder.Port = request.Host.Port.Value;
                }

                var url = uriBuilder.ToString();

                // Update database with FileName and URL
                try
                {
                    tLMDistribution.FileName = fileName;
                    tLMDistribution.Url      = url;
                    _context.Update(tLMDistribution);

                    TempData["messageType"]  = "success";
                    TempData["messageTitle"] = "RECORDS UPDATED";
                    TempData["message"]      = "Records updated successfully";
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TLMDistributionExists(tLMDistribution.TLMDistributionId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Details), new { id = tLMDistribution.TLMDistributionId }));
            }

            TempData["messageType"]  = "error";
            TempData["messageTitle"] = "FILE NOT UPLOADED";
            TempData["message"]      = "A file could not be uploaded error";

            return(RedirectToAction("Details", "TLMDistributions", new { id }));
        }