Initial implementation of AlbumController Get action

This commit is contained in:
REDCODE
2024-11-11 00:40:00 +01:00
parent 9bd6632264
commit cf77a0943f
2 changed files with 69 additions and 10 deletions

View File

@@ -1,7 +1,10 @@
using Lactose.Dtos;
using Lactose.Dtos.Album;
using Lactose.Mapper;
using Lactose.Models;
using Lactose.Repositories;
using Lactose.Services;
using Lactose.Types;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -10,10 +13,44 @@ namespace Lactose.Controllers;
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class AlbumController(LactoseAuthService authService, IAlbumRepository albumRepository) : ControllerBase {
public class AlbumController(
ILogger<AssetController> logger,
LactoseAuthService authService,
IAlbumRepository albumRepository
) : ControllerBase {
[HttpGet("{id}")]
public ActionResult<AlbumFullDto> Get([FromRoute] Guid id) {
throw new NotImplementedException();
Guid? uid = authService.GetUserData(User)?.Id;
EAccessLevel accessLevel = authService.GetUserData(User)?.AccessLevel ?? EAccessLevel.User;
var album = albumRepository.Find(id);
// If the album does not exist, return a 404
if (album == null) return NotFound();
// If the album has no assets, return an empty list
if (album.Assets == null) return Ok(album.ToAlbumFullDto());
//Storage for assets that will be sent out;
List<Asset> assets = new();
switch (accessLevel) {
case EAccessLevel.User:
// Only publicly shared assets or owned by user
assets.AddRange(album.Assets.Where(x => x.IsPubliclyShared || x.OwnerId == uid));
// Add shared assets
assets.AddRange(album.Assets.Where(x => x.SharedWith!.Any(x => x.Id == uid)));
break;
case EAccessLevel.Curator:
case EAccessLevel.Admin:
// All assets
assets.AddRange(album.Assets);
break;
}
// changes the retrieved album to the new filtered
album.Assets = assets;
return Ok(album.ToAlbumFullDto());
}
[HttpGet]
@@ -21,33 +58,33 @@ public class AlbumController(LactoseAuthService authService, IAlbumRepository al
throw new NotImplementedException();
}
[Authorize(Roles = "Admin, Curator")]
[HttpPost]
[Authorize(Roles = "Admin, Curator")]
public ActionResult Update([FromBody] AlbumUpdateDto albumDto) {
throw new NotImplementedException();
}
[Authorize(Roles = "Admin, Curator")]
[HttpPost]
[Authorize(Roles = "Admin, Curator")]
public ActionResult BulkUpdate([FromBody] BulkDto<AlbumUpdateDto> bulkDto) {
throw new NotImplementedException();
}
[Authorize(Roles = "Admin, Curator")]
[HttpPut]
[Authorize(Roles = "Admin, Curator")]
public ActionResult Create([FromBody] AlbumCreateDto albumDto) {
throw new NotImplementedException();
}
[Authorize(Roles = "Admin, Curator")]
[HttpDelete]
[Authorize(Roles = "Admin, Curator")]
public ActionResult BulkDelete([FromBody] List<Guid> albums) {
throw new NotImplementedException();
}
[Authorize(Roles = "Admin, Curator")]
[HttpDelete("{id}")]
[Authorize(Roles = "Admin, Curator")]
public ActionResult Delete(Guid id) {
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,22 @@
using Lactose.Dtos.Album;
using Lactose.Models;
namespace Lactose.Mapper;
/// <summary>
/// provides mapping methods for converting Album objects to DTOs.
/// </summary>
public static class AlbumMapper {
/// <summary>
/// Maps an Album object to an AlbumFullDto object.
/// </summary>
/// <param name="album">The album to map.</param>
/// <returns>An AlbumFullDto object.</returns>
public static AlbumFullDto ToAlbumFullDto(this Album album) => new AlbumFullDto {
Id = album.Id,
Name = album.Title,
Owner = album.UserOwnerId,
Person = album.PersonOwnerId,
Images = album.Assets!.Select(asset => asset.Id).ToList()
};
}