Skip to content

Commit

Permalink
[#24] update file-item Find() to load parents to a max-depth of 4
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphoeninger committed Oct 29, 2024
1 parent d351e36 commit 2c1a221
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions DAL/DAL.FileSharing/Repos/FileItemRepo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DAL.FileSharing.Repos;
namespace DAL.FileSharing.Repos;

public class FileItemRepo : TemporalTableBaseRepo<FileItem>, IFileItemRepo
{
Expand All @@ -10,9 +10,25 @@ internal FileItemRepo(DbContextOptions<ApplicationDBContext> options) : base(opt
#region methods
public override FileItem? Find(int id)
{
var fileItem = Context.FileItems.Include(x => x.FileItems).First(x => x.Id == id);
FileItem? fileItem = Context.FileItems.Include(x => x.FileItems)
.First(x => x.Id == id);

int count = 0;
if (fileItem != null) LoadParents(fileItem);

return fileItem;

// nested
void LoadParents(FileItem _fileItem)
{
if (_fileItem.ParentId == null) return; // gate
Context.Entry(_fileItem)
.Reference(c => c.ParentNavigation)
.Load();
count += 1;
if (count >= 3 || _fileItem.ParentNavigation?.ParentId == null) return;
LoadParents(_fileItem.ParentNavigation);
}
}
#endregion
}

0 comments on commit 2c1a221

Please sign in to comment.