diff --git a/Lactose/Controllers/AlbumController.cs b/Lactose/Controllers/AlbumController.cs index d1e1f83..69ae1d4 100644 --- a/Lactose/Controllers/AlbumController.cs +++ b/Lactose/Controllers/AlbumController.cs @@ -56,7 +56,28 @@ public class AlbumController( [HttpGet] public ActionResult> Search([FromQuery] PagedSearchParametersDto pagingOptions) { - throw new NotImplementedException(); + var uid = authService.GetUserData(User)?.Id; + var accessLevel = authService.GetUserData(User)?.AccessLevel ?? EAccessLevel.User; + + if(pagingOptions.Page < 0 || pagingOptions.PageSize < 0) return BadRequest(); + + var albums = albumRepository.SearchQuery(pagingOptions.Search ?? string.Empty, pagingOptions.Page, pagingOptions.PageSize).ToList(); + + switch (accessLevel) { + default: + case EAccessLevel.User: + List filteredAlbums; + // Only publicly shared albums or owned by user + filteredAlbums = albums.Where(x => x.UserOwnerId == uid || x.Assets.Any(x => x.IsPubliclyShared)).ToList(); + // Add shared albums + filteredAlbums.AddRange(albums.Where(x => x.Assets.Any(x => x.SharedWith!.Any(x => x.Id == uid)))); + return Ok(filteredAlbums.Select(x => x.ToAlbumPreviewDto()).ToList()); + break; + case EAccessLevel.Curator: + case EAccessLevel.Admin: + // All albums + return Ok(albums.Select(x => x.ToAlbumPreviewDto()).ToList()); + } } [HttpPost("{id}")] diff --git a/Lactose/Dtos/PagedParametersDTO.cs b/Lactose/Dtos/PagedParametersDTO.cs index 28dc8f1..0750d51 100644 --- a/Lactose/Dtos/PagedParametersDTO.cs +++ b/Lactose/Dtos/PagedParametersDTO.cs @@ -1,8 +1,8 @@ namespace Lactose.Dtos; public class PagedParametersDto { - public uint? Page { get; set; } = 0; - public uint? PageSize { get; set; } = 150; + public int Page { get; set; } = 0; + public int PageSize { get; set; } = 150; } public class PagedSearchParametersDto: PagedParametersDto { diff --git a/Lactose/Mapper/AlbumMapper.cs b/Lactose/Mapper/AlbumMapper.cs index 4206a8b..74bf7b3 100644 --- a/Lactose/Mapper/AlbumMapper.cs +++ b/Lactose/Mapper/AlbumMapper.cs @@ -19,4 +19,14 @@ public static class AlbumMapper { Person = album.PersonOwnerId, Images = album.Assets!.Select(asset => asset.Id).ToList() }; + + public static AlbumPreviewDto ToAlbumPreviewDto(this Album album) => new AlbumPreviewDto { + Id = album.Id, + Name = album.Title, + Owner = album.UserOwnerId, + Person = album.PersonOwnerId + }; + + + } diff --git a/Lactose/Repositories/AlbumRepository.cs b/Lactose/Repositories/AlbumRepository.cs index dfda630..3920974 100644 --- a/Lactose/Repositories/AlbumRepository.cs +++ b/Lactose/Repositories/AlbumRepository.cs @@ -18,6 +18,13 @@ public class AlbumRepository(LactoseDbContext context) : IAlbumRepository { } } + public IEnumerable SearchQuery(string query, int page = 0, int pageSize = 150) { + return context.Albums + .Where(x => x.Title.Contains(query)) + .Skip(page * pageSize) + .Take(pageSize); + } + public void Save() => context.SaveChanges(); public Album? Find(Guid id) => context.Albums.Find(id); diff --git a/Lactose/Repositories/IAlbumRepository.cs b/Lactose/Repositories/IAlbumRepository.cs index e8f7b61..579203e 100644 --- a/Lactose/Repositories/IAlbumRepository.cs +++ b/Lactose/Repositories/IAlbumRepository.cs @@ -67,4 +67,13 @@ public interface IAlbumRepository : IDisposable { /// /// The album to remove. public void Remove(Album album); + + /// + /// Searches for albums based on a query string. + /// + /// The search query string. + /// The page number for pagination (default is 0). + /// The number of results per page (default is 150). + /// A list of album IDs that match the search query. + public IEnumerable SearchQuery(string query, int page = 0, int pageSize = 150); }