Ready to test pulling random assets
This commit is contained in:
@ -1,93 +1,30 @@
|
||||
using ImmichToSlideshow.Models.Immich;
|
||||
using ImmichToSlideshow.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace ImmichToSlideshow.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class AssetsController(AssetService assetService) : ControllerBase
|
||||
{
|
||||
|
||||
private readonly string _ContentType = "application/json";
|
||||
private readonly AssetService _AssetService = assetService;
|
||||
|
||||
[HttpGet()]
|
||||
public IActionResult Get()
|
||||
{
|
||||
ReadOnlyCollection<Asset> assets = _AssetService.Get();
|
||||
AssetResponse?[] assetResponses = AssetResponse.FromDomain(assets);
|
||||
return Ok(assetResponses);
|
||||
}
|
||||
[HttpGet("columns")]
|
||||
public IActionResult GetColumns() =>
|
||||
Content(_AssetService.GetColumns(), _ContentType);
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Create(CreateAssetRequest request)
|
||||
{
|
||||
// mapping to internal representation
|
||||
Asset asset = request.ToDomain();
|
||||
[HttpGet("owner-ids")]
|
||||
public IActionResult GetOwnerIds() =>
|
||||
Content(_AssetService.GetOwnerIds(), _ContentType);
|
||||
|
||||
// invoke the use case
|
||||
_AssetService.Create(asset);
|
||||
[HttpGet("{ownerId:guid}")]
|
||||
public IActionResult Get(Guid ownerId) =>
|
||||
Content(_AssetService.GetAssets(ownerId), _ContentType);
|
||||
|
||||
// mapping to external representation
|
||||
AssetResponse assetResponse = AssetResponse.FromDomain(asset);
|
||||
|
||||
// return 201 created response
|
||||
return CreatedAtAction(
|
||||
actionName: nameof(Get),
|
||||
routeValues: new { AssetId = asset.Id },
|
||||
value: assetResponse);
|
||||
}
|
||||
|
||||
[HttpGet("{assetId:guid}")]
|
||||
public IActionResult Get(Guid assetId)
|
||||
{
|
||||
//get the asset
|
||||
Asset? asset = _AssetService.Get(assetId);
|
||||
|
||||
// mapping to external representation
|
||||
AssetResponse? assetResponse = AssetResponse.FromDomain(asset);
|
||||
|
||||
// return 200 ok response
|
||||
return assetResponse is null
|
||||
? Problem(statusCode: StatusCodes.Status404NotFound, detail: $"Asset not found {assetId}")
|
||||
: Ok(assetResponse);
|
||||
}
|
||||
|
||||
public record CreateAssetRequest(string Id,
|
||||
string DeviceAssetId,
|
||||
string OwnerId,
|
||||
string OriginalFileName,
|
||||
string Path)
|
||||
{
|
||||
|
||||
public Asset ToDomain() =>
|
||||
Asset.Get(id: Id,
|
||||
deviceAssetId: DeviceAssetId,
|
||||
ownerId: OwnerId,
|
||||
originalFileName: OriginalFileName,
|
||||
path: Path);
|
||||
|
||||
}
|
||||
|
||||
public record AssetResponse(string Id,
|
||||
string DeviceAssetId,
|
||||
string OwnerId,
|
||||
string OriginalFileName,
|
||||
string Path)
|
||||
{
|
||||
|
||||
public static AssetResponse? FromDomain(Asset? asset) =>
|
||||
asset is null ? null : new AssetResponse(
|
||||
Id: asset.Id,
|
||||
DeviceAssetId: asset.DeviceAssetId,
|
||||
OwnerId: asset.OwnerId,
|
||||
OriginalFileName: asset.OriginalFileName,
|
||||
Path: asset.Path);
|
||||
|
||||
public static AssetResponse?[] FromDomain(IEnumerable<Asset> assets) =>
|
||||
assets.Select(FromDomain).ToArray();
|
||||
|
||||
}
|
||||
[HttpGet("{ownerId:guid}/random-paths")]
|
||||
public IActionResult GetRandomPaths(Guid ownerId) =>
|
||||
Ok(_AssetService.GetRandomPaths(ownerId));
|
||||
|
||||
}
|
Reference in New Issue
Block a user