// GET: PropertyDocuments/Create
        public IActionResult Create(Guid propertyId)
        {
            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            ViewData["DocumentTypeId"] = new SelectList(_context.DocumentTypes.Where(x => x.Owner == null || x.OwnerId == userId), "Id", "Description");
            var data = new PropertyDocumentCreateDto
            {
                PropertyId = propertyId,
            };

            return(View(data));
        }
        public async Task <bool> CreatePropertyDocumentForProperty(PropertyDocumentCreateDto propertyDoc)
        {
            var result          = false;
            var contentRootPath = _env.ContentRootPath;
            var uploads         = Path.Combine(_env.WebRootPath, "PropertyDocuments", propertyDoc.PropertyId.ToString());

            if (!Directory.Exists(uploads))
            {
                Directory.CreateDirectory(uploads);
            }
            if (propertyDoc.Document.Length > 0)
            {
                // Upload the file if less than 2 MB
                if (propertyDoc.Document.Length < 2097152)
                {
                    var filePath = Path.Combine(uploads, propertyDoc.Document.FileName);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        await propertyDoc.Document.CopyToAsync(fileStream);

                        await _context.AddAsync(new PropertyDocument
                        {
                            FileName       = propertyDoc.Document.FileName,
                            FilePath       = filePath,
                            CreatedDate    = DateTime.Now,
                            DocumentTypeId = propertyDoc.DocumentTypeId,
                            FileType       = Path.GetExtension(propertyDoc.Document.FileName),
                            PropertyId     = propertyDoc.PropertyId,
                            Expires        = propertyDoc.Expires,
                            ActiveFrom     = propertyDoc.ActiveFrom,
                            ExpirationDate = propertyDoc.ExpiryDate
                        });
                    }
                    await _context.SaveChangesAsync();

                    result = true;
                }
                else
                {
                    throw new ImageFormatLimitationException("The file is too large");
                }
            }
            return(result);
        }
        public async Task <IActionResult> Create([Bind("Document,DocumentTypeId,PropertyId,Expires,ActiveFrom,ExpiryDate")] PropertyDocumentCreateDto propertyDocument)
        {
            var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (ModelState.IsValid)
            {
                try
                {
                    var property = await _context.Properties.Include(x => x.Portfolio).SingleOrDefaultAsync(x => x.Id == propertyDocument.PropertyId);

                    var result = await _propertyDocumentService.CreatePropertyDocumentForProperty(propertyDocument);

                    return(RedirectToAction("Detail", "Property", new { portfolioId = property.Portfolio.Id, propertyId = property.Id }).WithSuccess("Success", "Document Added"));
                }
                catch (ImageFormatLimitationException ex)
                {
                    ModelState.AddModelError("Document", ex.Message);
                }
            }
            ViewData["DocumentTypeId"] = new SelectList(_context.DocumentTypes.Where(x => x.Owner == null || x.OwnerId == userId), "Id", "Description");
            return(View(propertyDocument));
        }