Skip to content

Commit d25f1c6

Browse files
committed
Finished first iteration over ordering.data
1 parent 1ae4d01 commit d25f1c6

15 files changed

Lines changed: 324 additions & 311 deletions

src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Application.Queries;
55
using MediatR;
66
using Microsoft.AspNetCore.Mvc;
7+
using Models;
78
using System;
89
using System.Threading.Tasks;
910

@@ -13,7 +14,7 @@ public class OrdersController : Controller
1314
private readonly IMediator _mediator;
1415
private readonly IOrderQueries _orderQueries;
1516

16-
public OrdersController(IMediator mediator,IOrderQueries orderQueries)
17+
public OrdersController(IMediator mediator, IOrderQueries orderQueries)
1718
{
1819
if (mediator == null)
1920
{
@@ -31,9 +32,21 @@ public OrdersController(IMediator mediator,IOrderQueries orderQueries)
3132

3233
[Route("new")]
3334
[HttpPost]
34-
public async Task<IActionResult> AddOrder()
35+
public async Task<IActionResult> AddOrder([FromBody]NewOrderViewModel order)
3536
{
36-
var newOrderRequest = new NewOrderRequest();
37+
var newOrderRequest = new NewOrderRequest()
38+
{
39+
Buyer =GetUserName(), //TODO
40+
CardTypeId = 1, //TODO
41+
CardHolderName = order.CardHolderName,
42+
CardNumber = order.CardNumber,
43+
CardExpiration = order.CardExpiration,
44+
CardSecurityNumber = order.CardSecurityNumber,
45+
State = order.ShippingState,
46+
City = order.ShippingCity,
47+
Country = order.ShippingCountry,
48+
Street = order.ShippingStreet
49+
};
3750

3851
var added = await _mediator.SendAsync(newOrderRequest);
3952

@@ -45,50 +58,38 @@ public async Task<IActionResult> AddOrder()
4558
return BadRequest();
4659
}
4760

48-
49-
[Route("cancel/{orderId:int}")]
50-
[HttpPost]
51-
public async Task<IActionResult> CancelOrder(int orderId)
61+
[Route("{orderId:int}")]
62+
[HttpGet]
63+
public async Task<IActionResult> GetOrder(int orderId)
5264
{
53-
var cancelOrderRequest = new CancelOrderRequest(orderId);
54-
55-
var cancelled = await _mediator.SendAsync(cancelOrderRequest);
56-
57-
if (cancelled)
58-
{
59-
return Ok();
60-
}
65+
var order = await _orderQueries.GetOrder(orderId);
6166

62-
return BadRequest();
67+
68+
return Ok(order);
6369
}
6470

65-
66-
[Route("{orderId:int}")]
71+
[Route("")]
6772
[HttpGet]
68-
public async Task<IActionResult> GetOrder(int orderId)
73+
public async Task<IActionResult> GetOrders()
6974
{
70-
var order = await _orderQueries.GetOrder(orderId);
75+
var orders = await _orderQueries.GetOrders();
7176

72-
if ( order != null)
73-
{
74-
Ok(order);
75-
}
7677

77-
return NotFound();
78+
return Ok(orders);
7879
}
7980

80-
[Route("pending")]
81+
[Route("cardtypes")]
8182
[HttpGet]
82-
public async Task<IActionResult> GetPendingOrders(int orderId)
83+
public async Task<IActionResult> GetCardTypes()
8384
{
84-
var orders = await _orderQueries.GetPendingOrders();
85+
var cardTypes = await _orderQueries.GetCardTypes();
8586

86-
if (orders.Any())
87-
{
88-
Ok(orders);
89-
}
87+
return Ok(cardTypes);
88+
}
9089

91-
return NoContent();
90+
string GetUserName()
91+
{
92+
return "MOCK";
9293
}
9394
}
9495

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults
2+
{
3+
using AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
public class InternalServerErrorObjectResult : ObjectResult
7+
{
8+
public InternalServerErrorObjectResult(object error)
9+
: base(error)
10+
{
11+
StatusCode = StatusCodes.Status500InternalServerError;
12+
}
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+

2+
3+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules
4+
{
5+
using Application.Queries;
6+
using Autofac;
7+
using Domain.Repositories;
8+
using Ordering.Infrastructure.Repositories;
9+
10+
public class ApplicationModule
11+
:Autofac.Module
12+
{
13+
protected override void Load(ContainerBuilder builder)
14+
{
15+
builder.RegisterType<OrderQueries>()
16+
.As<IOrderQueries>()
17+
.InstancePerLifetimeScope();
18+
19+
builder.RegisterType<BuyerRepository>()
20+
.As<IBuyerRepository>()
21+
.InstancePerLifetimeScope();
22+
23+
builder.RegisterType<OrderRepository>()
24+
.As<IOrderRepository>()
25+
.InstancePerLifetimeScope();
26+
}
27+
}
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules
2+
{
3+
using Application.Commands;
4+
using Application.Decorators;
5+
using Autofac;
6+
using Autofac.Core;
7+
using MediatR;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Reflection;
11+
12+
public class MediatorModule : Autofac.Module
13+
{
14+
protected override void Load(ContainerBuilder builder)
15+
{
16+
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
17+
.AsImplementedInterfaces();
18+
19+
builder.RegisterAssemblyTypes(typeof(NewOrderRequest).GetTypeInfo().Assembly)
20+
.As(o => o.GetInterfaces()
21+
.Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>)))
22+
.Select(i => new KeyedService("IAsyncRequestHandler", i)));
23+
24+
builder.Register<SingleInstanceFactory>(context =>
25+
{
26+
var componentContext = context.Resolve<IComponentContext>();
27+
28+
return t => componentContext.Resolve(t);
29+
});
30+
31+
builder.Register<MultiInstanceFactory>(context =>
32+
{
33+
var componentContext = context.Resolve<IComponentContext>();
34+
35+
return t => (IEnumerable<object>)componentContext.Resolve(typeof(IEnumerable<>).MakeGenericType(t));
36+
});
37+
38+
builder.RegisterGenericDecorator(typeof(LogDecorator<,>),
39+
typeof(IAsyncRequestHandler<,>),
40+
"IAsyncRequestHandler");
41+
}
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Filters
2+
{
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Mvc.Filters;
5+
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.ActionResults;
6+
using Microsoft.Extensions.Logging;
7+
8+
public class HttpGlobalExceptionFilter : IExceptionFilter
9+
{
10+
private readonly IHostingEnvironment env;
11+
private readonly ILogger<HttpGlobalExceptionFilter> logger;
12+
13+
public HttpGlobalExceptionFilter(IHostingEnvironment env, ILogger<HttpGlobalExceptionFilter> logger)
14+
{
15+
this.env = env;
16+
this.logger = logger;
17+
}
18+
19+
public void OnException(ExceptionContext context)
20+
{
21+
logger.LogError(new EventId(context.Exception.HResult),
22+
context.Exception,
23+
context.Exception.Message);
24+
25+
var json = new JsonErrorResponse
26+
{
27+
Messages = new[] { "An error ocurr.Try it again." }
28+
};
29+
30+
if (env.IsDevelopment())
31+
{
32+
json.DeveloperMeesage = context.Exception;
33+
}
34+
35+
context.Result = new InternalServerErrorObjectResult(json);
36+
}
37+
38+
private class JsonErrorResponse
39+
{
40+
public string[] Messages { get; set; }
41+
42+
public object DeveloperMeesage { get; set; }
43+
}
44+
}
45+
}

src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161122162602_initial.Designer.cs renamed to src/Services/Ordering/Ordering.API/Infrastructure/Migrations/20161124133626_InitialModel.Designer.cs

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)