/// <summary> /// prevents wrong entries, because of not existing Ids /// </summary> /// <param name="document"></param> /// <returns>document</returns> public void CheckIfIdToConnectWithExists(Document document) { Person person = null; Course course = null; if (document.PersonId == null && document.CourseId == null) { throw new MissingInputException("You have to enter either Person or Course or both."); } if (document.PersonId != null) { person = entities.Persons.FirstOrDefault(c => c.Id == document.PersonId); if (person == null) { throw new EntryCouldNotBeFoundException("The Person you want to assign to this document could not be found."); } } if (document.CourseId != null) { course = entities.Courses.FirstOrDefault(c => c.Id == document.CourseId); if (course == null) { throw new EntryCouldNotBeFoundException("The Course you want to assign to this document could not be found."); } } }
/// <summary> /// Creates a unique file name, converts the DocumentString from Base 64 into Bytes and writes them into destination file /// </summary> /// <param name="document"></param> /// <param name="fileExtension"></param> /// <returns>Document</returns> public Document ConvertDocumentStringFromBase64AndSave(Document document, string fileExtension) { string fileName = document.Name + "_" + DateTime.Now.ToFileTime() + fileExtension; string destFile = documentMainPath + "\\DocumentsUploaded\\" + fileName; System.IO.File.WriteAllBytes(destFile, Convert.FromBase64String(document.DocumentString)); document.Url = destFile; document.Name = fileName; return(document); }
/// <summary> /// Deletes a file /// </summary> /// <param name="documentToDelete"></param> /// <returns></returns> public void DeleteRealDocument(Document documentToDelete) { string filename = documentToDelete.Url; if (File.Exists(filename)) { File.Delete(filename); } else { throw new FileDoesNotExistException("It seems that the File you want to delete does not exist."); } }
/// <summary> /// Creates a new document /// </summary> /// <param name="recDocument"></param> /// <returns>Document</returns> public Document CreateNewDocument(Document recDocument) { CheckIfIdToConnectWithExists(recDocument); if (recDocument.DocumentString != null) { recDocument = ConvertDocumentStringFromBase64AndSave(recDocument, ".pdf"); } recDocument.CreatedAt = DateTime.Now; recDocument.ModifiedAt = DateTime.Now; entities.Documents.Attach(recDocument); recDocument.CreateRelation(); entities.SaveChanges(); return(recDocument); }
/// <summary> /// receives data from Email_TemplateController, creates a Document and then a communication on database /// </summary> /// <param name="template"></param> /// <param name="person"></param> /// <param name="reminderId"></param> /// <param name="url"></param> /// <param name="name"></param> /// <returns>Communication</returns> public Communication CreateDocumentFromTemplate(EmailTemplate template, Person person, int?reminderId, string url, string name) { Document newDoc = new Document(); newDoc.Name = name; newDoc.Url = url; newDoc.Comment = "Document created from Template"; newDoc.Type = template.DocumentType; newDoc.CourseId = template.CourseId; newDoc.PersonId = person.Id; Document document = CreateNewDocument(newDoc); ///in this case Date = DateTime.Now, but can be different if we would make an entry about last weeks phone call DateTime date = DateTime.Now; Communication communication = communicationController.CreateCommunication(document, template, date, reminderId); return(communication); }
/// <summary> /// Deletes a document by its Id, removes entry in RelDocumentClasses and sets DocumentId in Absences and Communications to null /// </summary> /// <param name="id"></param> /// <returns></returns> public string DeleteById(int id) { Document documentToDelete = entities.Documents.SingleOrDefault(x => x.Id == id); if (documentToDelete == null) { throw new EntryCouldNotBeFoundException("The Document you want to delete could not be found."); } ///delete the Relations from Documents to Classes List <RelDocumentClass> relationList = entities.RelDocumentClasses.Where(x => x.DocId == id).ToList(); foreach (var item in relationList) { entities.RelDocumentClasses.Remove(item); } ///set the affected Absences - DocumentId to null List <Absence> absenceList = entities.Absences.Where(x => x.DocumentId == id).ToList(); foreach (var item in absenceList) { item.DocumentId = null; entities.Absences.Update(item); } ///set the affected Communications - DocumentId to null List <Communication> communicationList = entities.Communications.Where(x => x.DocumentId == id).ToList(); foreach (var item in communicationList) { item.DocumentId = null; entities.Communications.Update(item); } ///Deletes Document with its Path DeleteRealDocument(documentToDelete); ///Deletes Document entry in Database entities.Documents.Remove(documentToDelete); entities.SaveChanges(); return("Record has been successfully deleted"); }
/// <summary> /// Converts the DocumentString to Base64 and attaches it to the document we want to return /// </summary> /// <param name="document"></param> public void ConvertDocumentStringToBase64AndAttach(Document document) { var fileAsString = Convert.ToBase64String(System.IO.File.ReadAllBytes(document.Url)); document.DocumentString = fileAsString; }