diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs index b0806f71c..02d209ad0 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs @@ -4,6 +4,7 @@ using Application.Queries; using MediatR; using Microsoft.AspNetCore.Mvc; + using Models; using System; using System.Threading.Tasks; @@ -13,7 +14,7 @@ private readonly IMediator _mediator; private readonly IOrderQueries _orderQueries; - public OrdersController(IMediator mediator,IOrderQueries orderQueries) + public OrdersController(IMediator mediator, IOrderQueries orderQueries) { if (mediator == null) { @@ -31,9 +32,21 @@ [Route("new")] [HttpPost] - public async Task AddOrder() + public async Task AddOrder([FromBody]NewOrderViewModel order) { - var newOrderRequest = new NewOrderRequest(); + var newOrderRequest = new NewOrderRequest() + { + Buyer =GetUserName(), //TODO + CardTypeId = 1, //TODO + CardHolderName = order.CardHolderName, + CardNumber = order.CardNumber, + CardExpiration = order.CardExpiration, + CardSecurityNumber = order.CardSecurityNumber, + State = order.ShippingState, + City = order.ShippingCity, + Country = order.ShippingCountry, + Street = order.ShippingStreet + }; var added = await _mediator.SendAsync(newOrderRequest); @@ -45,50 +58,38 @@ return BadRequest(); } - - [Route("cancel/{orderId:int}")] - [HttpPost] - public async Task CancelOrder(int orderId) + [Route("{orderId:int}")] + [HttpGet] + public async Task GetOrder(int orderId) { - var cancelOrderRequest = new CancelOrderRequest(orderId); - - var cancelled = await _mediator.SendAsync(cancelOrderRequest); - - if (cancelled) - { - return Ok(); - } + var order = await _orderQueries.GetOrder(orderId); - return BadRequest(); + + return Ok(order); } - - [Route("{orderId:int}")] + [Route("")] [HttpGet] - public async Task GetOrder(int orderId) + public async Task GetOrders() { - var order = await _orderQueries.GetOrder(orderId); + var orders = await _orderQueries.GetOrders(); - if ( order != null) - { - Ok(order); - } - return NotFound(); + return Ok(orders); } - [Route("pending")] + [Route("cardtypes")] [HttpGet] - public async Task GetPendingOrders(int orderId) + public async Task GetCardTypes() { - var orders = await _orderQueries.GetPendingOrders(); + var cardTypes = await _orderQueries.GetCardTypes(); - if (orders.Any()) - { - Ok(orders); - } + return Ok(cardTypes); + } - return NoContent(); + string GetUserName() + { + return "MOCK"; } } diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs b/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs new file mode 100644 index 000000000..55cb33c99 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs @@ -0,0 +1,14 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults +{ + using AspNetCore.Http; + using Microsoft.AspNetCore.Mvc; + + public class InternalServerErrorObjectResult : ObjectResult + { + public InternalServerErrorObjectResult(object error) + : base(error) + { + StatusCode = StatusCodes.Status500InternalServerError; + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs new file mode 100644 index 000000000..2c159c4b3 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/ApplicationModule.cs @@ -0,0 +1,28 @@ + + +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules +{ + using Application.Queries; + using Autofac; + using Domain.Repositories; + using Ordering.Infrastructure.Repositories; + + public class ApplicationModule + :Autofac.Module + { + protected override void Load(ContainerBuilder builder) + { + builder.RegisterType() + .As() + .InstancePerLifetimeScope(); + + builder.RegisterType() + .As() + .InstancePerLifetimeScope(); + + builder.RegisterType() + .As() + .InstancePerLifetimeScope(); + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs new file mode 100644 index 000000000..36427aefd --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs @@ -0,0 +1,43 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules +{ + using Application.Commands; + using Application.Decorators; + using Autofac; + using Autofac.Core; + using MediatR; + using System.Collections.Generic; + using System.Linq; + using System.Reflection; + + public class MediatorModule : Autofac.Module + { + protected override void Load(ContainerBuilder builder) + { + builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly) + .AsImplementedInterfaces(); + + builder.RegisterAssemblyTypes(typeof(NewOrderRequest).GetTypeInfo().Assembly) + .As(o => o.GetInterfaces() + .Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>))) + .Select(i => new KeyedService("IAsyncRequestHandler", i))); + + builder.Register(context => + { + var componentContext = context.Resolve(); + + return t => componentContext.Resolve(t); + }); + + builder.Register(context => + { + var componentContext = context.Resolve(); + + return t => (IEnumerable)componentContext.Resolve(typeof(IEnumerable<>).MakeGenericType(t)); + }); + + builder.RegisterGenericDecorator(typeof(LogDecorator<,>), + typeof(IAsyncRequestHandler<,>), + "IAsyncRequestHandler"); + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs new file mode 100644 index 000000000..2125fcd7f --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Filters/HttpGlobalExceptionFilter.cs @@ -0,0 +1,45 @@ +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Filters +{ + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.Mvc.Filters; + using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults; + using Microsoft.Extensions.Logging; + + public class HttpGlobalExceptionFilter : IExceptionFilter + { + private readonly IHostingEnvironment env; + private readonly ILogger logger; + + public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger logger) + { + this.env = env; + this.logger = logger; + } + + public void OnException(ExceptionContext context) + { + logger.LogError(new EventId(context.Exception.HResult), + context.Exception, + context.Exception.Message); + + var json = new JsonErrorResponse + { + Messages = new[] { "An error ocurr.Try it again." } + }; + + if (env.IsDevelopment()) + { + json.DeveloperMeesage = context.Exception; + } + + context.Result = new InternalServerErrorObjectResult(json); + } + + private class JsonErrorResponse + { + public string[] Messages { get; set; } + + public object DeveloperMeesage { get; set; } + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.Designer.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.Designer.cs similarity index 89% rename from src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.Designer.cs rename to src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.Designer.cs index bc974dd75..f7d11fbfb 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.Designer.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.Designer.cs @@ -8,8 +8,8 @@ using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; namespace Ordering.API.Infrastructure.Migrations { [DbContext(typeof(OrderingContext))] - [Migration("20161122162602_initial")] - partial class initial + [Migration("20161124133626_InitialModel")] + partial class InitialModel { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -29,16 +29,8 @@ namespace Ordering.API.Infrastructure.Migrations b.Property("Country"); - b.Property("CountryCode"); - - b.Property("Latitude"); - - b.Property("Longitude"); - b.Property("State"); - b.Property("StateCode"); - b.Property("Street"); b.Property("ZipCode"); @@ -62,15 +54,21 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); + b.HasIndex("FullName") + .IsUnique(); + b.ToTable("buyers","ordering"); }); modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.CardType", b => { b.Property("Id") - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd() + .HasDefaultValue(1); - b.Property("Name"); + b.Property("Name") + .IsRequired() + .HasAnnotation("MaxLength", 200); b.HasKey("Id"); @@ -85,8 +83,6 @@ namespace Ordering.API.Infrastructure.Migrations .HasAnnotation("SqlServer:HiLoSequenceSchema", "ordering") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); - b.Property("BillingAddressId"); - b.Property("BuyerId"); b.Property("OrderDate"); @@ -99,8 +95,6 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); - b.HasIndex("BillingAddressId"); - b.HasIndex("BuyerId"); b.HasIndex("PaymentId"); @@ -142,9 +136,12 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.OrderStatus", b => { b.Property("Id") - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd() + .HasDefaultValue(1); - b.Property("Name"); + b.Property("Name") + .IsRequired() + .HasAnnotation("MaxLength", 200); b.HasKey("Id"); @@ -159,6 +156,8 @@ namespace Ordering.API.Infrastructure.Migrations .HasAnnotation("SqlServer:HiLoSequenceSchema", "ordering") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); + b.Property("BuyerId"); + b.Property("CardHolderName") .IsRequired() .HasAnnotation("MaxLength", 200); @@ -175,6 +174,8 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); + b.HasIndex("BuyerId"); + b.HasIndex("CardTypeId"); b.ToTable("payments","ordering"); @@ -182,11 +183,6 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.Order", b => { - b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Address", "BillingAddress") - .WithMany() - .HasForeignKey("BillingAddressId") - .OnDelete(DeleteBehavior.SetNull); - b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Buyer", "Buyer") .WithMany() .HasForeignKey("BuyerId") @@ -194,8 +190,7 @@ namespace Ordering.API.Infrastructure.Migrations b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Payment", "Payment") .WithMany() - .HasForeignKey("PaymentId") - .OnDelete(DeleteBehavior.Cascade); + .HasForeignKey("PaymentId"); b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Address", "ShippingAddress") .WithMany() @@ -217,6 +212,11 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.Payment", b => { + b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Buyer") + .WithMany("Payments") + .HasForeignKey("BuyerId") + .OnDelete(DeleteBehavior.Cascade); + b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.CardType", "CardType") .WithMany() .HasForeignKey("CardTypeId") diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.cs similarity index 88% rename from src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.cs rename to src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.cs index b64a9b570..e60cf281f 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore.Metadata; namespace Ordering.API.Infrastructure.Migrations { - public partial class initial : Migration + public partial class InitialModel : Migration { protected override void Up(MigrationBuilder migrationBuilder) { @@ -36,11 +36,7 @@ namespace Ordering.API.Infrastructure.Migrations .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), City = table.Column(nullable: true), Country = table.Column(nullable: true), - CountryCode = table.Column(nullable: true), - Latitude = table.Column(nullable: false), - Longitude = table.Column(nullable: false), State = table.Column(nullable: true), - StateCode = table.Column(nullable: true), Street = table.Column(nullable: true), ZipCode = table.Column(nullable: true) }, @@ -67,9 +63,8 @@ namespace Ordering.API.Infrastructure.Migrations schema: "ordering", columns: table => new { - Id = table.Column(nullable: false) - .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), - Name = table.Column(nullable: true) + Id = table.Column(nullable: false, defaultValue: 1), + Name = table.Column(maxLength: 200, nullable: false) }, constraints: table => { @@ -81,9 +76,8 @@ namespace Ordering.API.Infrastructure.Migrations schema: "ordering", columns: table => new { - Id = table.Column(nullable: false) - .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), - Name = table.Column(nullable: true) + Id = table.Column(nullable: false, defaultValue: 1), + Name = table.Column(maxLength: 200, nullable: false) }, constraints: table => { @@ -96,6 +90,7 @@ namespace Ordering.API.Infrastructure.Migrations columns: table => new { Id = table.Column(nullable: false), + BuyerId = table.Column(nullable: false), CardHolderName = table.Column(maxLength: 200, nullable: false), CardNumber = table.Column(maxLength: 25, nullable: false), CardTypeId = table.Column(nullable: false), @@ -105,6 +100,13 @@ namespace Ordering.API.Infrastructure.Migrations constraints: table => { table.PrimaryKey("PK_payments", x => x.Id); + table.ForeignKey( + name: "FK_payments_buyers_BuyerId", + column: x => x.BuyerId, + principalSchema: "ordering", + principalTable: "buyers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_payments_cardtypes_CardTypeId", column: x => x.CardTypeId, @@ -120,7 +122,6 @@ namespace Ordering.API.Infrastructure.Migrations columns: table => new { Id = table.Column(nullable: false), - BillingAddressId = table.Column(nullable: true), BuyerId = table.Column(nullable: false), OrderDate = table.Column(nullable: false), PaymentId = table.Column(nullable: false), @@ -130,13 +131,6 @@ namespace Ordering.API.Infrastructure.Migrations constraints: table => { table.PrimaryKey("PK_orders", x => x.Id); - table.ForeignKey( - name: "FK_orders_address_BillingAddressId", - column: x => x.BillingAddressId, - principalSchema: "ordering", - principalTable: "address", - principalColumn: "Id", - onDelete: ReferentialAction.SetNull); table.ForeignKey( name: "FK_orders_buyers_BuyerId", column: x => x.BuyerId, @@ -150,7 +144,7 @@ namespace Ordering.API.Infrastructure.Migrations principalSchema: "ordering", principalTable: "payments", principalColumn: "Id", - onDelete: ReferentialAction.Cascade); + onDelete: ReferentialAction.Restrict); table.ForeignKey( name: "FK_orders_address_ShippingAddressId", column: x => x.ShippingAddressId, @@ -194,10 +188,11 @@ namespace Ordering.API.Infrastructure.Migrations }); migrationBuilder.CreateIndex( - name: "IX_orders_BillingAddressId", + name: "IX_buyers_FullName", schema: "ordering", - table: "orders", - column: "BillingAddressId"); + table: "buyers", + column: "FullName", + unique: true); migrationBuilder.CreateIndex( name: "IX_orders_BuyerId", @@ -229,6 +224,12 @@ namespace Ordering.API.Infrastructure.Migrations table: "orderItems", column: "OrderId"); + migrationBuilder.CreateIndex( + name: "IX_payments_BuyerId", + schema: "ordering", + table: "payments", + column: "BuyerId"); + migrationBuilder.CreateIndex( name: "IX_payments_CardTypeId", schema: "ordering", @@ -259,19 +260,19 @@ namespace Ordering.API.Infrastructure.Migrations schema: "ordering"); migrationBuilder.DropTable( - name: "address", + name: "payments", schema: "ordering"); migrationBuilder.DropTable( - name: "buyers", + name: "address", schema: "ordering"); migrationBuilder.DropTable( - name: "payments", + name: "orderstatus", schema: "ordering"); migrationBuilder.DropTable( - name: "orderstatus", + name: "buyers", schema: "ordering"); migrationBuilder.DropTable( diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/OrderingContextModelSnapshot.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/OrderingContextModelSnapshot.cs index ac998114d..75837ccef 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/OrderingContextModelSnapshot.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Migrations/OrderingContextModelSnapshot.cs @@ -28,16 +28,8 @@ namespace Ordering.API.Infrastructure.Migrations b.Property("Country"); - b.Property("CountryCode"); - - b.Property("Latitude"); - - b.Property("Longitude"); - b.Property("State"); - b.Property("StateCode"); - b.Property("Street"); b.Property("ZipCode"); @@ -61,15 +53,21 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); + b.HasIndex("FullName") + .IsUnique(); + b.ToTable("buyers","ordering"); }); modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.CardType", b => { b.Property("Id") - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd() + .HasDefaultValue(1); - b.Property("Name"); + b.Property("Name") + .IsRequired() + .HasAnnotation("MaxLength", 200); b.HasKey("Id"); @@ -84,8 +82,6 @@ namespace Ordering.API.Infrastructure.Migrations .HasAnnotation("SqlServer:HiLoSequenceSchema", "ordering") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); - b.Property("BillingAddressId"); - b.Property("BuyerId"); b.Property("OrderDate"); @@ -98,8 +94,6 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); - b.HasIndex("BillingAddressId"); - b.HasIndex("BuyerId"); b.HasIndex("PaymentId"); @@ -141,9 +135,12 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.OrderStatus", b => { b.Property("Id") - .ValueGeneratedOnAdd(); + .ValueGeneratedOnAdd() + .HasDefaultValue(1); - b.Property("Name"); + b.Property("Name") + .IsRequired() + .HasAnnotation("MaxLength", 200); b.HasKey("Id"); @@ -158,6 +155,8 @@ namespace Ordering.API.Infrastructure.Migrations .HasAnnotation("SqlServer:HiLoSequenceSchema", "ordering") .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.SequenceHiLo); + b.Property("BuyerId"); + b.Property("CardHolderName") .IsRequired() .HasAnnotation("MaxLength", 200); @@ -174,6 +173,8 @@ namespace Ordering.API.Infrastructure.Migrations b.HasKey("Id"); + b.HasIndex("BuyerId"); + b.HasIndex("CardTypeId"); b.ToTable("payments","ordering"); @@ -181,11 +182,6 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.Order", b => { - b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Address", "BillingAddress") - .WithMany() - .HasForeignKey("BillingAddressId") - .OnDelete(DeleteBehavior.SetNull); - b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Buyer", "Buyer") .WithMany() .HasForeignKey("BuyerId") @@ -193,8 +189,7 @@ namespace Ordering.API.Infrastructure.Migrations b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Payment", "Payment") .WithMany() - .HasForeignKey("PaymentId") - .OnDelete(DeleteBehavior.Cascade); + .HasForeignKey("PaymentId"); b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Address", "ShippingAddress") .WithMany() @@ -216,6 +211,11 @@ namespace Ordering.API.Infrastructure.Migrations modelBuilder.Entity("Microsoft.eShopOnContainers.Services.Ordering.Domain.Payment", b => { + b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.Buyer") + .WithMany("Payments") + .HasForeignKey("BuyerId") + .OnDelete(DeleteBehavior.Cascade); + b.HasOne("Microsoft.eShopOnContainers.Services.Ordering.Domain.CardType", "CardType") .WithMany() .HasForeignKey("CardTypeId") diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs b/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs index f16427b7a..017bb2aaa 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs @@ -1,12 +1,12 @@ - -namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure { using AspNetCore.Builder; using Microsoft.EntityFrameworkCore; + using Microsoft.eShopOnContainers.Services.Ordering.Domain; using Ordering.Infrastructure; + using System.Linq; using System.Threading.Tasks; - public class OrderingContextSeed { public static async Task SeedAsync(IApplicationBuilder applicationBuilder) @@ -18,6 +18,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure { context.Database.Migrate(); + if (!context.CardTypes.Any()) + { + context.CardTypes.Add(CardType.Amex); + context.CardTypes.Add(CardType.Visa); + context.CardTypes.Add(CardType.MasterCard); + + await context.SaveChangesAsync(); + } + + if (!context.OrderStatus.Any()) + { + context.OrderStatus.Add(OrderStatus.Canceled); + context.OrderStatus.Add(OrderStatus.InProcess); + context.OrderStatus.Add(OrderStatus.Shipped); + } + await context.SaveChangesAsync(); } } diff --git a/src/Services/Ordering/Ordering.API/Models/NewOrderViewModel.cs b/src/Services/Ordering/Ordering.API/Models/NewOrderViewModel.cs new file mode 100644 index 000000000..c79e3c3bf --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Models/NewOrderViewModel.cs @@ -0,0 +1,25 @@ +using System; + +namespace Microsoft.eShopOnContainers.Services.Ordering.API.Models +{ + public class NewOrderViewModel + { + public string ShippingCity { get; set; } + + public string ShippingStreet { get; set; } + + public string ShippingState { get; set; } + + public string ShippingCountry { get; set; } + + public string CardType { get; set; } + + public string CardNumber { get; set; } + + public string CardHolderName { get; set; } + + public DateTime CardExpiration { get; set; } + + public string CardSecurityNumber { get; set; } + } +} diff --git a/src/Services/Ordering/Ordering.API/Project_Readme.html b/src/Services/Ordering/Ordering.API/Project_Readme.html deleted file mode 100644 index 1a0f5b51a..000000000 --- a/src/Services/Ordering/Ordering.API/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/src/Services/Ordering/Ordering.API/Properties/launchSettings.json b/src/Services/Ordering/Ordering.API/Properties/launchSettings.json index faa3c234a..150597acd 100644 --- a/src/Services/Ordering/Ordering.API/Properties/launchSettings.json +++ b/src/Services/Ordering/Ordering.API/Properties/launchSettings.json @@ -11,7 +11,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/environmentInfo/machinename", + "launchUrl": "/swagger/ui", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index fe8d01f6e..58c3d8442 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -1,5 +1,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API { + using Autofac; + using Autofac.Extensions.DependencyInjection; + using Infrastructure; + using Infrastructure.AutofacModules; + using Infrastructure.Filters; using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -8,6 +13,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Ordering.Infrastructure; + using System; using System.Reflection; public class Startup @@ -31,10 +37,16 @@ public IConfigurationRoot Configuration { get; } - public void ConfigureServices(IServiceCollection services) + public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. - services.AddMvc(); + + services.AddMvc(options=> + { + + options.Filters.Add(typeof(HttpGlobalExceptionFilter)); + + }).AddControllersAsServices(); services.AddEntityFrameworkSqlServer() .AddDbContext(options => @@ -56,8 +68,22 @@ }); }); - services.AddMediatR(typeof(Startup)); //TODO:pending - + services.AddSingleton(this.Configuration); + + services.AddOptions(); + + + + //configure autofac + + var container = new ContainerBuilder(); + container.Populate(services); + + container.RegisterModule(new MediatorModule()); + container.RegisterModule(new ApplicationModule()); + + + return new AutofacServiceProvider(container.Build()); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) @@ -69,7 +95,9 @@ { app.UseDeveloperExceptionPage(); } - + + OrderingContextSeed.SeedAsync(app).Wait(); + app.UseMvc(); app.UseSwagger() diff --git a/src/Services/Ordering/Ordering.API/docker-compose.yml b/src/Services/Ordering/Ordering.API/docker-compose.yml index cb76c339d..4a31a1a26 100644 --- a/src/Services/Ordering/Ordering.API/docker-compose.yml +++ b/src/Services/Ordering/Ordering.API/docker-compose.yml @@ -10,16 +10,13 @@ services: - ConnectionString=Server=ordering.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word ports: - "81:80" -# (Go to Production): For secured/final deployment, remove Ports mapping and -# leave just the internal expose section -# expose: -# - "80" - extra_hosts: - - "CESARDLBOOKVHD:10.0.75.1" depends_on: - ordering.data ordering.data: - image: eshop/ordering.data.sqlserver.linux + image: microsoft/mssql-server-linux + environment: + - ACCEPT_EULA=Y + - SA_PASSWORD=Pass@word ports: - "1433:1433" \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/project.json b/src/Services/Ordering/Ordering.API/project.json index 46249899f..f65f41ad6 100644 --- a/src/Services/Ordering/Ordering.API/project.json +++ b/src/Services/Ordering/Ordering.API/project.json @@ -5,6 +5,7 @@ "type": "platform" }, "MediatR.Extensions.Microsoft.DependencyInjection": "1.0.1", + "Autofac.Extensions.DependencyInjection": "4.0.0", "Microsoft.AspNetCore.Mvc": "1.0.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", @@ -12,6 +13,7 @@ "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0", "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0", + "Microsoft.Extensions.Logging.Abstractions": "1.0.0", "Microsoft.Extensions.Logging.Console": "1.0.0", "Microsoft.Extensions.Logging.Debug": "1.0.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",