/ProductQuery.cs Secret
Created
October 21, 2020 21:50
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