Пример #1
0
        public async Task <ActionResult <Document> > PostDocument([FromForm] UploadDocumentInfoViewModel documentVm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string path     = _repository.GetDocumentUploadPath();
            string fileName = await _repository.UploadFile(documentVm.FileData, path);

            if (fileName != null)
            {
                Document document = new Document
                {
                    Name           = documentVm.Name,
                    Description    = documentVm.Description,
                    UploadDate     = DateTime.Now,
                    Path           = fileName,
                    LMSUserId      = _userManager.GetUserId(User),
                    DocumentTypeId = documentVm.DocumentTypeId,

                    CourseId      = documentVm.DocOwnerTypeId == (int)DocOwnerType.Course ? documentVm.DocOwnerId : null,
                    ModuleId      = documentVm.DocOwnerTypeId == (int)DocOwnerType.Module ? documentVm.DocOwnerId : null,
                    LMSActivityId = documentVm.DocOwnerTypeId == (int)DocOwnerType.Activity ? documentVm.DocOwnerId : null
                };
                await _repository.AddDocumentAsync(document);

                _repository.SaveAllAsync();

                return(CreatedAtAction("GetDocument", new { id = document.Id }, document));
            }
            return(BadRequest());
        }
Пример #2
0
        public async Task <JsonResult> Upload([FromBody] DocumentModel document)
        {
            var user = await _userManager.GetUserAsync(User);

            var data           = document.Data;
            var fileAttributes = document.FileName.Split('.');

            var doc = new Document()
            {
                Number            = document.Number,
                OwnerUserId       = user.Id,
                ResponsibleUserId = document.ResponsibleUserId,
                DocumentTypeId    = document.DocumentType,
                DeadLine          = document.DeadLine,
                CreationDate      = DateTimeOffset.Now,
                File = new File()
                {
                    CreationDate    = DateTimeOffset.Now,
                    Data            = data,
                    FileExtensionId = (FileExtensions)Enum.Parse(typeof(FileExtensions), fileAttributes[1]),
                    FileName        = fileAttributes[0],
                    Guid            = Guid.NewGuid()
                },
                StepId = Steps.New
            };

            await _documentRepository.AddDocumentAsync(doc);

            await _context.SaveChangesAsync();

            return(new JsonResult(new { success = true, data = doc.DocumentId }));
        }
Пример #3
0
 public async Task AddDocumentAsync(Document document)
 {
     if (_documentRepository.GetDocument(document.Number) != null)
     {
         throw new Exception($"Документ с номером {document.Number} уже существует");
     }
     await _documentRepository.AddDocumentAsync(document);
 }