/// <summary> /// Deletes the File. /// </summary> /// <param name="dto">The dto.</param> /// <returns>IList<ValidationResult>.</returns> /// <exception cref="System.ArgumentNullException"></exception> public OperationResult <File> DeleteFile(FileDto dto, Guid?registryId) { //Validazione degli argomenti if (dto == null) { throw new ArgumentNullException(nameof(dto)); } using (var transaction = Session.ExecuteInTransaction()) { //Recupero l'entità tramite l'id File entity = _fileRepository.Load(dto.Id); //Eseguo la validazione logica IList <ValidationResult> vResults = ValidateEntity(entity); if (!vResults.Any()) { var registry = _registryRepository.Load(registryId); registry.Files.Remove(entity); _registryRepository.Save(registry); //Eseguo la cancellazione logica var filepathFisical = HttpContext.Current.Server.MapPath(entity.FilepathVirtual); if (System.IO.File.Exists(filepathFisical)) { System.IO.File.Delete(filepathFisical); } //Commit Esplicito transaction.ExecuteCommit(); } else { transaction.ExecuteRollback(); } //Ritorno i risultati return(new OperationResult <File> { ReturnedValue = entity, ValidationResults = vResults }); } }
/// <summary> /// Creates the File. /// </summary> /// <param name="dto">The dto.</param> /// <returns>IList<ValidationResult>.</returns> public OperationResult <File> CreateFile(FileDto dto, Guid?registryId) { //Validazione degli argomenti if (dto == null) { throw new ArgumentNullException(nameof(dto)); } if (!registryId.HasValue) { throw new ArgumentNullException(nameof(registryId)); } //Dichiaro la lista di risultati di ritorno IList <ValidationResult> vResults = new List <ValidationResult>(); //Eseguo in transazione using (var transaction = Session.ExecuteInTransaction()) { //Definisco l'entità File entity = new File(); entity.Name = dto.Name; entity.Size = dto.Size; entity.Type = dto.Type; entity.FilepathVirtual = dto.FilepathVirtual; entity.FilepathPhysical = dto.FilepathPhysical; var documentDirectory = CreateFileDirectory(); entity.FilepathVirtual = string.Format("{0}/{1}", documentDirectory, entity.Name); entity.FilepathPhysical = HttpContext.Current.Server.MapPath(entity.FilepathVirtual); //Eseguo la validazione logica vResults = ValidateEntity(entity); if (!vResults.Any()) { //Salvataggio su db WriteDocumentToDisk(documentDirectory, dto.FilepathBodypart, entity.Name); Registry registry = _registryRepository.Load(registryId); registry.Files.Add(entity); vResults = ValidateEntity(registry); if (!vResults.Any()) { _registryRepository.Save(registry); } //Commit Esplicito transaction.ExecuteCommit(); } else { transaction.ExecuteRollback(); } //Ritorno i risultati return(new OperationResult <File> { ReturnedValue = entity, ValidationResults = vResults }); } }