Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Galaxy.Product.GrpcHost/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions Galaxy.Product/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +16,8 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddProductService(this IServiceCollection services)
{
services.AddScoped<IProductService, ProductService>();
services.AddDbContext();

return services;
}

Expand All @@ -33,5 +37,11 @@ public static IServiceCollection AddProductServiceAsGrpc(this IServiceCollection

return services;
}

private static IServiceCollection AddDbContext(this IServiceCollection services)
{
services.AddDbContext<ProductContext>(option => option.UseMySql("server=mysql.zzhong.me;userid=lims;password=Dev@2017;database=Galaxy;"));
return services;
}
}
}
22 changes: 22 additions & 0 deletions Galaxy.Product/Entity/ProductContext.cs
Original file line number Diff line number Diff line change
@@ -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<ProductContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<ProductEntity>();

base.OnModelCreating(builder);
}
}
}
7 changes: 6 additions & 1 deletion Galaxy.Product/Entity/ProductEntity.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
1 change: 1 addition & 0 deletions Galaxy.Product/Galaxy.Product.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.6" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.24.0" />
</ItemGroup>
Expand Down
20 changes: 9 additions & 11 deletions Galaxy.Product/ProductService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +12,11 @@ namespace Galaxy.Product
{
public class ProductService : IProductService
{
private List<ProductDto> products { get; set; }
private ProductContext context { get; set; }

public ProductService()
public ProductService(ProductContext context)
{
products = new List<ProductDto>();
products.Add(new ProductDto() { Id = "1", Name = "Apple" });
products.Add(new ProductDto() { Id = "2", Name = "Orange" });
this.context = context;
}

public Task<ProductDto> CreateAsync(ProductDto product)
Expand All @@ -26,12 +26,10 @@ public Task<ProductDto> CreateAsync(ProductDto product)

public async Task<IEnumerable<ProductDto>> GetListAsync(ProductQueryDto query)
{
var task = await Task.Run(() =>
{
Thread.Sleep(100);
return products.AsEnumerable();
});
return task;
var entities = await this.context.Set<ProductEntity>().ToListAsync();
var products = new List<ProductDto>();
entities.ForEach(entity => products.Add(new ProductDto() { Id = entity.Id, Name = entity.Name }));
return products;
}
}
}