mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.3 KiB
42 lines
1.3 KiB
using ErsatzTV.Core; |
|
using ErsatzTV.Core.Domain; |
|
using ErsatzTV.Infrastructure.Data; |
|
using ErsatzTV.Infrastructure.Extensions; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace ErsatzTV.Application.Artworks; |
|
|
|
public class GetArtworkHandler(IDbContextFactory<TvContext> dbContextFactory) : IRequestHandler<GetArtwork, Either<BaseError, Artwork>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory = dbContextFactory; |
|
|
|
public async Task<Either<BaseError, Artwork>> Handle( |
|
GetArtwork request, |
|
CancellationToken cancellationToken) |
|
{ |
|
try { |
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken); |
|
|
|
Option<Artwork> artwork = await dbContext.Artwork |
|
.AsNoTracking() |
|
.SelectOneAsync(a => a.Id, a => a.Id == request.Id) |
|
.MapT(Project); |
|
|
|
return artwork.ToEither(BaseError.New("Artwork not found")); |
|
|
|
} |
|
catch (Exception ex) |
|
{ |
|
return BaseError.New(ex.ToString()); |
|
} |
|
} |
|
|
|
private static Artwork Project(Artwork artwork) |
|
{ |
|
return new Artwork { |
|
Id = artwork.Id, |
|
Path = artwork.Path, |
|
ArtworkKind = artwork.ArtworkKind |
|
}; |
|
} |
|
}
|
|
|