-
-
Save dcomartin/f8908e2884bf74d29c2d8c31f4f7af13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Catalog.Contracts; | |
using Microsoft.EntityFrameworkCore; | |
namespace Catalog | |
{ | |
public interface IProductQuery | |
{ | |
Task<Product> GetProduct(Guid productId); | |
} | |
public class Product | |
{ | |
public Guid Id { get; set; } | |
public string Name { get; set; } | |
public decimal Price { get; set; } | |
} | |
public class ProductQuery : IProductQuery | |
{ | |
private readonly CatalogDbContext _dbContext; | |
public ProductQuery(CatalogDbContext dbContext) | |
{ | |
_dbContext = dbContext; | |
} | |
public async Task<Product> GetProduct(Guid productId) | |
{ | |
return await _dbContext.Products | |
.Where(x => x.Id == productId) | |
.Select(x => new Product | |
{ | |
Id = x.Id, | |
Name = x.Name, | |
Price = x.Price | |
}) | |
.SingleAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment