Implemented Search function on Album endpoint. Closes #5
This commit is contained in:
@@ -56,7 +56,28 @@ public class AlbumController(
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<List<AlbumPreviewDto>> 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<Album> 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}")]
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -18,6 +18,13 @@ public class AlbumRepository(LactoseDbContext context) : IAlbumRepository {
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Album> 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);
|
||||
|
||||
@@ -67,4 +67,13 @@ public interface IAlbumRepository : IDisposable {
|
||||
/// </summary>
|
||||
/// <param name="album">The album to remove.</param>
|
||||
public void Remove(Album album);
|
||||
|
||||
/// <summary>
|
||||
/// Searches for albums based on a query string.
|
||||
/// </summary>
|
||||
/// <param name="query">The search query string.</param>
|
||||
/// <param name="page">The page number for pagination (default is 0).</param>
|
||||
/// <param name="pageSize">The number of results per page (default is 150).</param>
|
||||
/// <returns>A list of album IDs that match the search query.</returns>
|
||||
public IEnumerable<Album> SearchQuery(string query, int page = 0, int pageSize = 150);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user