From cccd326c6e9a50962ab4fe883c90c8c8a5ee6692 Mon Sep 17 00:00:00 2001 From: redz Date: Tue, 5 Nov 2019 13:30:12 +0800 Subject: [PATCH] feat: add product mysql storage --- Galaxy.Product.GrpcHost/Startup.cs | 2 ++ .../ServiceCollectionExtensions.cs | 10 +++++++++ Galaxy.Product/Entity/ProductContext.cs | 22 +++++++++++++++++++ Galaxy.Product/Entity/ProductEntity.cs | 7 +++++- Galaxy.Product/Galaxy.Product.csproj | 1 + Galaxy.Product/ProductService.cs | 20 ++++++++--------- 6 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 Galaxy.Product/Entity/ProductContext.cs diff --git a/Galaxy.Product.GrpcHost/Startup.cs b/Galaxy.Product.GrpcHost/Startup.cs index 53c9d9f..643be1e 100644 --- a/Galaxy.Product.GrpcHost/Startup.cs +++ b/Galaxy.Product.GrpcHost/Startup.cs @@ -20,6 +20,8 @@ public void ConfigureServices(IServiceCollection services) services.AddCodeFirstGrpc(config => { config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal; }); + + services.AddProductService(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/Galaxy.Product/Configuration/ServiceCollectionExtensions.cs b/Galaxy.Product/Configuration/ServiceCollectionExtensions.cs index 0e4bb8c..0cbbb3e 100644 --- a/Galaxy.Product/Configuration/ServiceCollectionExtensions.cs +++ b/Galaxy.Product/Configuration/ServiceCollectionExtensions.cs @@ -1,6 +1,8 @@ using Galaxy.Product; using Galaxy.Product.Abstraction; +using Galaxy.Product.Entity; using Grpc.Net.Client; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using ProtoBuf.Grpc.Client; using System; @@ -14,6 +16,8 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddProductService(this IServiceCollection services) { services.AddScoped(); + services.AddDbContext(); + return services; } @@ -33,5 +37,11 @@ public static IServiceCollection AddProductServiceAsGrpc(this IServiceCollection return services; } + + private static IServiceCollection AddDbContext(this IServiceCollection services) + { + services.AddDbContext(option => option.UseMySql("server=mysql.zzhong.me;userid=lims;password=Dev@2017;database=Galaxy;")); + return services; + } } } diff --git a/Galaxy.Product/Entity/ProductContext.cs b/Galaxy.Product/Entity/ProductContext.cs new file mode 100644 index 0000000..5320851 --- /dev/null +++ b/Galaxy.Product/Entity/ProductContext.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Galaxy.Product.Entity +{ + public class ProductContext : DbContext + { + public ProductContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity(); + + base.OnModelCreating(builder); + } + } +} diff --git a/Galaxy.Product/Entity/ProductEntity.cs b/Galaxy.Product/Entity/ProductEntity.cs index f4a48fb..3ca9baf 100644 --- a/Galaxy.Product/Entity/ProductEntity.cs +++ b/Galaxy.Product/Entity/ProductEntity.cs @@ -1,10 +1,15 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Text; namespace Galaxy.Product.Entity { + [Table("Product")] public class ProductEntity - { + { + public string Id { get; set; } + + public string Name { get; set; } } } diff --git a/Galaxy.Product/Galaxy.Product.csproj b/Galaxy.Product/Galaxy.Product.csproj index 5cadb7e..7214b69 100644 --- a/Galaxy.Product/Galaxy.Product.csproj +++ b/Galaxy.Product/Galaxy.Product.csproj @@ -6,6 +6,7 @@ + diff --git a/Galaxy.Product/ProductService.cs b/Galaxy.Product/ProductService.cs index 9042ff4..0bb8546 100644 --- a/Galaxy.Product/ProductService.cs +++ b/Galaxy.Product/ProductService.cs @@ -1,5 +1,7 @@ using Galaxy.Product.Abstraction; using Galaxy.Product.Abstraction.Models.Dtos; +using Galaxy.Product.Entity; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -10,13 +12,11 @@ namespace Galaxy.Product { public class ProductService : IProductService { - private List products { get; set; } + private ProductContext context { get; set; } - public ProductService() + public ProductService(ProductContext context) { - products = new List(); - products.Add(new ProductDto() { Id = "1", Name = "Apple" }); - products.Add(new ProductDto() { Id = "2", Name = "Orange" }); + this.context = context; } public Task CreateAsync(ProductDto product) @@ -26,12 +26,10 @@ public Task CreateAsync(ProductDto product) public async Task> GetListAsync(ProductQueryDto query) { - var task = await Task.Run(() => - { - Thread.Sleep(100); - return products.AsEnumerable(); - }); - return task; + var entities = await this.context.Set().ToListAsync(); + var products = new List(); + entities.ForEach(entity => products.Add(new ProductDto() { Id = entity.Id, Name = entity.Name })); + return products; } } }