mirror of https://github.com/ErsatzTV/ErsatzTV.git
3 changed files with 71 additions and 1 deletions
@ -0,0 +1,6 @@ |
|||||||
|
using ErsatzTV.Core; |
||||||
|
using ErsatzTV.Core.Domain; |
||||||
|
|
||||||
|
namespace ErsatzTV.Application.Artworks; |
||||||
|
|
||||||
|
public record GetArtwork(int Id) : IRequest<Either<BaseError, Artwork>>; |
@ -0,0 +1,42 @@ |
|||||||
|
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 |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue