private bool SaveScan(IFormFile file, int index)
        {
            //do save
            //if file exists?
            var path = Path.Combine(EnviromentHelper.RootPath, EnviromentHelper.ScanFolder);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            var fileName = Path.Combine(path, EnviromentHelper.ScanFilePathMaker(file.FileName, index));

            if ((new FileInfo(fileName)).Exists)
            {
                return(false);
            }
            using (var stream = new FileStream(fileName, FileMode.Create))
            {
                file.CopyTo(stream);
            }
            return(true);
        }
        public IActionResult UploadScan([FromQuery] int contractId)
        {
            var files = Request.Form.Files;

            if (files.Count < 1 || files.Count > 1)
            {
                return(BadRequest("Invalid file count."));
            }
            var file = files.First();

            if (file.Length < (int)MinFileLength)
            {
                return(BadRequest("File is too small"));
            }
            if (string.Compare(file.ContentType, AcceptedUploadType, true) != 0)
            {
                return(BadRequest("Invalid file type."));
            }
            using (var context = new DealerContractContext())
            {
                var contract = context.Contracts.FirstOrDefault(c => c.ContractId == contractId);
                if (contract == null)
                {
                    return(BadRequest("Contract not found"));
                }
                if (!string.IsNullOrEmpty(contract.ScannedContractUrl))
                {
                    return(BadRequest("Contract has scan uploaded already"));
                }

                if (!SaveScan(file, contract.ContractId))
                {
                    throw new InvalidOperationException();
                }
                contract.ScannedContractUrl = EnviromentHelper.ScanFilePathMaker(file.FileName, contract.ContractId);
                context.SaveChanges();
            }
            return(Ok());
        }