Skip to content

Commit

Permalink
fixes delete record with file field - now deletes files also
Browse files Browse the repository at this point in the history
rumen-yankov committed Jun 10, 2024

Verified

This commit was signed with the committer’s verified signature.
AlexAegis Sandor
1 parent 74c5ee3 commit 2eec3e2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions WebVella.Erp/Api/RecordManager.cs
Original file line number Diff line number Diff line change
@@ -1657,6 +1657,7 @@ public QueryResponse DeleteRecord(Entity entity, Guid id)

List<KeyValuePair<string, object>> storageRecordData = new List<KeyValuePair<string, object>>();


var query = EntityQuery.QueryEQ("id", id);
var entityQuery = new EntityQuery(entity.Name, "*", query);

@@ -1679,6 +1680,28 @@ public QueryResponse DeleteRecord(Entity entity, Guid id)
}
}

#region <--- check if entity has any file fields and delete files related to this record --->

var entityObj = entityManager.ReadEntities().Object.Single(x => x.Name == entity.Name);
var fileFields = entityObj.Fields.Where(x => x.GetFieldType() == FieldType.FileField).ToList();
var record = response.Object.Data[0];

var filesToDelete = new List<string>();
foreach (var fileField in fileFields)
{
if (!string.IsNullOrWhiteSpace((string)record[fileField.Name]))
filesToDelete.Add((string)record[fileField.Name]);
}

if (filesToDelete.Any())
{
var dbFileRep = new DbFileRepository();
foreach (var filepath in filesToDelete)
dbFileRep.Delete(filepath);
}

#endregion

CurrentContext.RecordRepository.Delete(entity.Name, id);

if (hooksExists && executeHooks)

0 comments on commit 2eec3e2

Please sign in to comment.