Skip to content

Commit

Permalink
Inv service #9
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Aug 10, 2018
1 parent 02fb19f commit 102b00b
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static class WebHostExtensions
public static IServiceProvider MigrateDbContext<TDbContext>(this IServiceProvider serviceProvider)
where TDbContext : DbContext
{
return serviceProvider.MigrateDbContext<TDbContext>((context, services) => {
return serviceProvider.MigrateDbContext<TDbContext>((context, services) =>
{
InstanceSeedData(services, context, typeof(ISeedData<>));
});
}
Expand All @@ -30,10 +31,11 @@ public static IWebHost RegisterDbContext<TDbContext>(this IWebHost webHost)
});
}

internal static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext, IServiceProvider> seeder)
where TContext : DbContext
internal static IWebHost MigrateDbContext<TContext>(this IWebHost webHost,
Action<TContext, IServiceProvider> seeder)
where TContext : DbContext
{
using (IServiceScope scope = webHost.Services.CreateScope())
using (var scope = webHost.Services.CreateScope())
{
scope.ServiceProvider.MigrateDbContext(seeder);
}
Expand All @@ -47,45 +49,34 @@ public static void InstanceSeedData(this IServiceProvider resolver, DbContext co
var scanAssemblyPattern = configuration.GetSection("EfCore")["FullyQualifiedPrefix"];
var seeders = scanAssemblyPattern.ResolveModularGenericTypes(seedData, context.GetType());

if (seeders == null)
{
return;
}
if (seeders == null) return;

foreach (var seeder in seeders)
{
var seedInstance = Activator.CreateInstance(seeder, new[] { configuration });
if (seedInstance != null)
{
var method = seeder.GetMethod("SeedAsync");
((Task)method.Invoke(seedInstance, new[] { context })).Wait();
}
var seedInstance = Activator.CreateInstance(seeder, configuration);
var method = seeder.GetMethod("SeedAsync");
((Task)method.Invoke(seedInstance, new object[] {context})).Wait();
}
}

/// <summary>
/// This function will open up the door to make the Setup page
/// Because we can call to this function for provision new database
/// This function will open up the door to make the Setup page
/// Because we can call to this function for provision new database
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="serviceProvider"></param>
/// <param name="seeder"></param>
/// <returns></returns>
public static IServiceProvider MigrateDbContext<TDbContext>(
this IServiceProvider serviceProvider,
Action<TDbContext, IServiceProvider> seeder)
where TDbContext : DbContext
this IServiceProvider serviceProvider,
Action<TDbContext, IServiceProvider> seeder)
where TDbContext : DbContext
{
var logger = serviceProvider.GetRequiredService<ILogger<TDbContext>>();
var context = serviceProvider.GetRequiredService<TDbContext>();

logger.LogInformation($"[VND] Migrating database associated with {typeof(TDbContext).FullName} context.");
context.Database.OpenConnection();
// context.Database.EnsureCreated();
if (!context.AllMigrationsApplied())
{
context.Database.Migrate();
}
if (!context.AllMigrationsApplied()) context.Database.Migrate();

logger.LogInformation($"[VND] Start to seed data for {typeof(TDbContext).FullName} context.");
seeder(context, serviceProvider);
Expand Down
2 changes: 1 addition & 1 deletion src/services/cart/Extensions/CartExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace VND.CoolStore.Services.Cart.Domain
{
public static class CartExtensions
{
public static CartDto ToCartDto(this Cart cart)
public static CartDto ToDto(this Cart cart)
{
return new CartDto
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
using Microsoft.AspNetCore.Mvc;
using VND.Fw.Infrastructure.AspNetCore;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.FW.Infrastructure.AspNetCore;

namespace VND.CoolStore.Services.Cart.v1.UseCases.Checkout
{
[ApiVersion("1.0")]
[Route("api/carts")]
public class CartController : EvtControllerBase
public class Controller : EvtControllerBase
{
public CartController(IMediator mediator) : base(mediator) { }
public Controller(IMediator mediator) : base(mediator) { }

[HttpPut]
[Route("{cartId:guid}/checkout")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Threading;
using System.Threading.Tasks;
using VND.CoolStore.Services.Cart.v1.Extensions;
using VND.CoolStore.Services.Cart.v1.Services;
using VND.Fw.Domain;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.Fw.Infrastructure.EfCore.Extensions;

namespace VND.CoolStore.Services.Cart.v1.UseCases.Checkout
{
public class CartRequestHandler : TxRequestHandlerBase<CheckoutRequest, CheckoutResponse>
public class RequestHandler : TxRequestHandlerBase<CheckoutRequest, CheckoutResponse>
{
public CartRequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf)
public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf)
: base(uow, qrf)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
using Microsoft.AspNetCore.Mvc;
using VND.Fw.Infrastructure.AspNetCore;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.FW.Infrastructure.AspNetCore;

namespace VND.CoolStore.Services.Cart.v1.UseCases.DeleteItemInCart
{
[ApiVersion("1.0")]
[Route("api/carts")]
public class CartController : EvtControllerBase
public class Controller : EvtControllerBase
{
public CartController(IMediator mediator) : base(mediator) { }
public Controller(IMediator mediator) : base(mediator) { }

[HttpDelete]
[Route("{cartId:guid}/items/{productId:guid}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
using System.Threading.Tasks;
using VND.CoolStore.Services.Cart.Domain;
using VND.CoolStore.Services.Cart.v1.Extensions;
using VND.CoolStore.Services.Cart.v1.Services;
using VND.Fw.Domain;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.Fw.Infrastructure.EfCore.Extensions;

namespace VND.CoolStore.Services.Cart.v1.UseCases.DeleteItemInCart
{
public class CartRequestHandler : TxRequestHandlerBase<DeleteItemRequest, DeleteItemResponse>
public class RequestHandler : TxRequestHandlerBase<DeleteItemRequest, DeleteItemResponse>
{
private readonly ICatalogGateway _catalogGateway;
public CartRequestHandler(ICatalogGateway cgw, IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf)
public RequestHandler(ICatalogGateway cgw, IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf)
: base(uow, qrf)
{
_catalogGateway = cgw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
using Microsoft.AspNetCore.Mvc;
using VND.Fw.Infrastructure.AspNetCore;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.FW.Infrastructure.AspNetCore;

namespace VND.CoolStore.Services.Cart.v1.UseCases.GetCartById
{
[ApiVersion("1.0")]
[Route("api/carts")]
public class CartController : EvtControllerBase
public class Controller : EvtControllerBase
{
public CartController(IMediator mediator) : base(mediator) { }
public Controller(IMediator mediator) : base(mediator) { }

[HttpGet]
[Route("{id}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace VND.CoolStore.Services.Cart.v1.UseCases.GetCartById
{
public class CartRequestHandler : RequestHandlerBase<GetCartRequest, GetCartResponse>
public class RequestHandler : RequestHandlerBase<GetCartRequest, GetCartResponse>
{
private readonly ICatalogGateway _catalogGateway;
private readonly NoTaxCaculator _priceCalculator;

public CartRequestHandler(ICatalogGateway cgw, IUnitOfWorkAsync uow,
public RequestHandler(ICatalogGateway cgw, IUnitOfWorkAsync uow,
IQueryRepositoryFactory qrf, NoTaxCaculator priceCalculator) : base(uow, qrf)
{
_catalogGateway = cgw;
Expand All @@ -29,7 +29,7 @@ public override async Task<GetCartResponse> Handle(GetCartRequest request, Cance
cartInfo = await cartInfo.InitCart(_catalogGateway, isPopulatePrice: true);
cartInfo = _priceCalculator.Execute(cartInfo);

return new GetCartResponse { Result = cartInfo.ToCartDto() };
return new GetCartResponse { Result = cartInfo.ToDto() };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
using Microsoft.AspNetCore.Mvc;
using VND.Fw.Infrastructure.AspNetCore;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.FW.Infrastructure.AspNetCore;

namespace VND.CoolStore.Services.Cart.v1.UseCases.InsertItemToNewCart
{
[ApiVersion("1.0")]
[Route("api/carts")]
public class CartController : EvtControllerBase
public class Controller : EvtControllerBase
{
public CartController(IMediator mediator) : base(mediator) { }
public Controller(IMediator mediator) : base(mediator) { }

[HttpPost]
[Auth(Policy = "access_cart_api")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace VND.CoolStore.Services.Cart.v1.UseCases.InsertItemToNewCart
{
public class CartRequestHandler : TxRequestHandlerBase<InsertItemToNewCartRequest, InsertItemToNewCartResponse>
public class RequestHandler : TxRequestHandlerBase<InsertItemToNewCartRequest, InsertItemToNewCartResponse>
{
private readonly ICatalogGateway _catalogGateway;
private readonly NoTaxCaculator _priceCalculator;

public CartRequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf,
public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf,
ICatalogGateway catalogGateway, NoTaxCaculator priceCalculator) : base(uow, qrf)
{
_catalogGateway = catalogGateway;
Expand All @@ -39,7 +39,7 @@ public override async Task<InsertItemToNewCartResponse> TxHandle(InsertItemToNew

await cartRepository.AddAsync(cart);

return new InsertItemToNewCartResponse { Result = cart.ToCartDto() };
return new InsertItemToNewCartResponse { Result = cart.ToDto() };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
using Microsoft.AspNetCore.Mvc;
using VND.Fw.Infrastructure.AspNetCore;
using VND.Fw.Infrastructure.AspNetCore.CleanArch;
using VND.FW.Infrastructure.AspNetCore;

namespace VND.CoolStore.Services.Cart.v1.UseCases.UpdateItemInCart
{
[ApiVersion("1.0")]
[Route("api/carts")]
public class CartController : EvtControllerBase
public class Controller : EvtControllerBase
{
public CartController(IMediator mediator) : base(mediator) { }
public Controller(IMediator mediator) : base(mediator) { }

[HttpPut]
[Auth(Policy = "access_cart_api")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace VND.CoolStore.Services.Cart.v1.UseCases.UpdateItemInCart
{
public class CartRequestHandler : TxRequestHandlerBase<UpdateItemInCartRequest, UpdateItemInCartResponse>
public class RequestHandler : TxRequestHandlerBase<UpdateItemInCartRequest, UpdateItemInCartResponse>
{
private readonly ICatalogGateway _catalogGateway;
private readonly NoTaxCaculator _priceCalculator;

public CartRequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf,
public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf,
ICatalogGateway catalogGateway, NoTaxCaculator priceCalculator) : base(uow, qrf)
{
_catalogGateway = catalogGateway;
Expand Down Expand Up @@ -65,7 +65,7 @@ public class CartRequestHandler : TxRequestHandlerBase<UpdateItemInCartRequest,
else
await cartItemRepository.AddAsync(item);

return new UpdateItemInCartResponse {Result = cart.ToCartDto()};
return new UpdateItemInCartResponse {Result = cart.ToDto()};
}
}
}
9 changes: 9 additions & 0 deletions src/services/inventory/Dtos/InventoryDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace VND.CoolStore.Services.Inventory.Dtos
{
public class InventoryDto
{
public string Location { get; set; }
public int Quantity { get; set; }
public string Link { get; set; }
}
}
17 changes: 17 additions & 0 deletions src/services/inventory/Extensions/InventoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using VND.CoolStore.Services.Inventory.Dtos;

namespace VND.CoolStore.Services.Inventory.Extensions
{
public static class InventoryExtensions
{
public static InventoryDto ToDto(this Domain.Inventory inv)
{
return new InventoryDto
{
Link = inv.Link,
Location = inv.Location,
Quantity = inv.Quantity
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using VND.CoolStore.Services.Inventory.Infrastructure.Db;
using VND.Fw.Infrastructure.EfCore.Migration;

namespace VND.CoolStore.Services.Inventory.Infrastructure.Db.Seeders
namespace VND.CoolStore.Services.Inventory.Infrastructure.Seeders
{
public class InventorySeeder : SeedDataBase<InventoryDbContext>
{
Expand All @@ -15,7 +16,7 @@ public override async Task SeedAsync(InventoryDbContext context)
{
var inventorySet = context.Set<Domain.Inventory>();

if (inventorySet.Count() > 0) return;
if (inventorySet.Any()) return;

var inventory1 = new Domain.Inventory(
new System.Guid("25E6BA6E-FDDB-401D-99B2-33DDC9F29322"))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace VND.CoolStore.Services.Inventory.Migrations
{
public partial class InitDb : Migration
public partial class InitInvDb : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Expand Down
11 changes: 0 additions & 11 deletions src/services/inventory/Model/InventoryModel.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/services/inventory/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using VND.CoolStore.Services.Inventory.Infrastructure.Db;
using VND.CoolStore.Services.Inventory.UseCases.Service;
using VND.CoolStore.Services.Inventory.UseCases.Service.Impl;
using VND.Fw.Infrastructure.AspNetCore.Miniservice;
using VND.Fw.Infrastructure.EfCore.SqlServer;

Expand Down Expand Up @@ -41,7 +39,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddScoped(sp => serviceParams);
services.AddEfCoreSqlServer();
services.AddMiniService<InventoryDbContext>();
services.AddScoped<IInventoryService, InventoryService>();
}

public void Configure(IApplicationBuilder app)
Expand Down
9 changes: 0 additions & 9 deletions src/services/inventory/UseCases/Service/IInventoryService.cs

This file was deleted.

0 comments on commit 102b00b

Please sign in to comment.