Esempio n. 1
0
        public async Task <IActionResult> AddUserDocument([FromForm] UserDocumentRequest userDocumentRequest)
        {
            try
            {
                string extension = Path.GetExtension(userDocumentRequest.Document.FileName);

                if (extension != Constants.ALLOWED_EXTENSION)
                {
                    return(BadRequest(new { ErrorMessage = ErrorMessages.ONLY_PDF_EXTENSION_ALLOWED }));
                }

                userDocumentRequest.DocumentName = $"{Guid.NewGuid()}{extension}";

                string directory = Path.Combine(_hostingEnvironment.WebRootPath, Constants.DOCUMENT_PATH);
                string path      = Path.Combine(directory, userDocumentRequest.DocumentName);

                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                using (Stream stream = new FileStream(path, FileMode.Create))
                {
                    userDocumentRequest.Document.CopyTo(stream);
                }

                userDocumentRequest.DocumentPath = Path.Combine(Constants.DOCUMENT_PATH, userDocumentRequest.DocumentName);

                int result = await _userDocumentRepository.Add(userDocumentRequest);

                if (result == 0)
                {
                    return(NotFound());
                }
                return(StatusCode(StatusCodes.Status201Created));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Esempio n. 2
0
 public IActionResult AddUserDocument([FromBody] UserDocument model)
 {
     Doc_repo.Add(model);
     return(new OkObjectResult(new { UserDocumentID = model.UserDocumentId }));
 }