public Guid Create(int organisationId, int centreId, int categoryId, string studentCode, string studentName, string description, string fileName, byte[] contents) { var newGuid = Guid.NewGuid(); var category = _nidanDataService.RetrieveDocumentTypes(organisationId) .FirstOrDefault(e => e.DocumentTypeId == categoryId); var centre = _nidanDataService.RetrieveCentre(organisationId, centreId, e => true); var basePath = CreateCentreBase(category.BasePath, centre.Name); var categoryFileName = string.Concat(category.Name, "_", newGuid, "_", fileName); var employeeDirectory = GetStudentDirectory(basePath, studentCode) ?? CreateStudentDirectory(basePath, studentName, studentCode); var categoryDirectory = Path.Combine(employeeDirectory, category.Name); var filePath = Path.Combine(categoryDirectory, categoryFileName); Directory.CreateDirectory(categoryDirectory); File.WriteAllBytes(filePath, contents); var document = new Entity.Document() { OrganisationId = organisationId, CentreId = centreId, DocumentTypeId = categoryId, StudentCode = studentCode, CreatedDateTime = DateTime.UtcNow.Date, Description = description, FileName = fileName, Location = filePath, StudentName = studentName, Guid = newGuid }; _nidanDataService.Create <Entity.Document>(organisationId, document); return(newGuid); }
private async Task <ValidationResult <Document> > CreateWorkerDocument(Document documentMeta, int personnelId, List <int> clientPersonnelIds, string userId) { var validationResult = new ValidationResult <Document>(); //upload document to document service var documentCategoryId = documentMeta.DocumentCategoryId; var documentCategories = await this.RetrieveDocumentCategoriesAsync(); var documentCategory = documentCategories.FirstOrDefault(e => e.DocumentCategoryId == documentCategoryId); if (documentCategory == null) { return(validationResult.Error("Document category not found")); } var apiDocument = _mapper.Map <Entity.Document>(documentMeta); apiDocument.Product = "Egharpay"; apiDocument.PersonnelId = personnelId.ToString(); apiDocument.CreatedBy = userId; apiDocument.CreatedDateTime = DateTime.UtcNow; var documentGuid = CreateDocument(documentCategory, personnelId.ToString(), "Test", documentMeta.Description, documentMeta.Filename, documentMeta.Bytes); if (documentGuid == null) { return(validationResult.Error("Document could not be saved, please try again")); } // create a worker document - if we have clientPersonnelIds, we need a worker document for each one var personnelDocument = new Entity.Document() { DocumentTypeId = documentCategoryId.Value, Guid = documentGuid, PersonnelId = personnelId.ToString(), FileName = documentMeta.Filename }; return(validationResult); }