|
|
|
|
@@ -30,18 +30,16 @@ public class PersonRepository(LactoseDbContext context) : IPersonRepository {
|
|
|
|
|
if (accessLevel != EAccessLevel.User)
|
|
|
|
|
return GetAll();
|
|
|
|
|
|
|
|
|
|
var all = context.People
|
|
|
|
|
.Include(p => p.Albums)!
|
|
|
|
|
.ThenInclude(a => a.Assets)!
|
|
|
|
|
.ThenInclude(a => a.SharedWith)
|
|
|
|
|
return context.People
|
|
|
|
|
.Include(p => p.Albums)
|
|
|
|
|
.Where(p => p.Albums!.Any(a =>
|
|
|
|
|
a.Assets!.Any(asset => asset.DeletedAt == null && (
|
|
|
|
|
asset.IsPubliclyShared ||
|
|
|
|
|
asset.OwnerId == userId ||
|
|
|
|
|
asset.SharedWith!.Any(u => u.Id == userId)
|
|
|
|
|
))
|
|
|
|
|
))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return all.Where(p => p.Albums != null && p.Albums.Any(a =>
|
|
|
|
|
a.Assets != null && a.Assets.Any(asset => asset.DeletedAt == null && (
|
|
|
|
|
asset.IsPubliclyShared ||
|
|
|
|
|
asset.OwnerId == userId ||
|
|
|
|
|
(asset.SharedWith != null && asset.SharedWith.Any(u => u.Id == userId))
|
|
|
|
|
))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
@@ -61,38 +59,30 @@ public class PersonRepository(LactoseDbContext context) : IPersonRepository {
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public IEnumerable<Person> SearchQuery(string query, int page = 0, int pageSize = 30, string? sortBy = null, bool sortAsc = true, Guid userId = default, EAccessLevel accessLevel = EAccessLevel.User) {
|
|
|
|
|
IQueryable<Person> peopleQuery = context.People
|
|
|
|
|
.Include(p => p.Albums)!
|
|
|
|
|
.ThenInclude(a => a.Assets)!
|
|
|
|
|
.ThenInclude(a => a.SharedWith);
|
|
|
|
|
IQueryable<Person> peopleQuery = context.People.Include(p => p.Albums);
|
|
|
|
|
|
|
|
|
|
// Apply search filter
|
|
|
|
|
if (!string.IsNullOrEmpty(query))
|
|
|
|
|
peopleQuery = peopleQuery.Where(p => EF.Functions.ILike(p.Name, $"%{query}%"));
|
|
|
|
|
|
|
|
|
|
// Apply sorting
|
|
|
|
|
if (accessLevel == EAccessLevel.User)
|
|
|
|
|
peopleQuery = peopleQuery.Where(p => p.Albums!.Any(a =>
|
|
|
|
|
a.Assets!.Any(asset => asset.DeletedAt == null && (
|
|
|
|
|
asset.IsPubliclyShared ||
|
|
|
|
|
asset.OwnerId == userId ||
|
|
|
|
|
asset.SharedWith!.Any(u => u.Id == userId)
|
|
|
|
|
))
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
IOrderedQueryable<Person> ordered = sortBy?.ToLower() switch
|
|
|
|
|
{
|
|
|
|
|
"created" => sortAsc ? peopleQuery.OrderBy(p => p.CreatedAt) : peopleQuery.OrderByDescending(p => p.CreatedAt),
|
|
|
|
|
"albums" => sortAsc ? peopleQuery.OrderBy(p => p.Albums.Count) : peopleQuery.OrderByDescending(p => p.Albums.Count),
|
|
|
|
|
_ => sortAsc ? peopleQuery.OrderBy(p => p.Name) : peopleQuery.OrderByDescending(p => p.Name),
|
|
|
|
|
"albums" => sortAsc ? peopleQuery.OrderBy(p => p.Albums!.Count) : peopleQuery.OrderByDescending(p => p.Albums!.Count),
|
|
|
|
|
_ => sortAsc ? peopleQuery.OrderBy(p => p.Name) : peopleQuery.OrderByDescending(p => p.Name),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = ordered
|
|
|
|
|
return ordered
|
|
|
|
|
.Skip(page * pageSize)
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
// Filter by visibility client-side (the nested SharedWith.Any() cannot be translated to SQL)
|
|
|
|
|
if (accessLevel == EAccessLevel.User)
|
|
|
|
|
result = result.Where(p => p.Albums != null && p.Albums.Any(a =>
|
|
|
|
|
a.Assets != null && a.Assets.Any(asset => asset.DeletedAt == null && (
|
|
|
|
|
asset.IsPubliclyShared ||
|
|
|
|
|
asset.OwnerId == userId ||
|
|
|
|
|
(asset.SharedWith != null && asset.SharedWith.Any(u => u.Id == userId))
|
|
|
|
|
)))).ToList();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
.Take(pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|