Skip to content

Commit 1ba7087

Browse files
Advanced further with Repositories and Dynamic-Queries
1 parent ec6256e commit 1ba7087

26 files changed

Lines changed: 461 additions & 294 deletions

global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"projects": [
33
"src",
4-
"test"
4+
"test",
5+
"src/Services/Ordering"
56
],
67

78
"sdk": {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
using Microsoft.eShopOnContainers.Services.Ordering.SqlData.UnitOfWork;
8+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel;
9+
using Microsoft.EntityFrameworkCore;
10+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.RepositoryContracts;
11+
using Microsoft.eShopOnContainers.Services.Ordering.SqlData.Queries;
12+
13+
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
14+
{
15+
[Route("api/[controller]")]
16+
public class OrderingController : Controller
17+
{
18+
private OrderingDbContext _context;
19+
private IOrderRepository _orderRepository;
20+
private OrderingQueries _queries;
21+
22+
public OrderingController(IOrderRepository orderRepository,
23+
OrderingQueries orderingQueries,
24+
OrderingDbContext context)
25+
{
26+
//Injected objects from the IoC container
27+
_orderRepository = orderRepository;
28+
_queries = orderingQueries;
29+
30+
_context = context;
31+
}
32+
33+
34+
// GET api/ordering/orders
35+
[HttpGet("orders")]
36+
public async Task<IActionResult> GetAllOrders()
37+
{
38+
dynamic response = await _queries.GetAllOrdersIncludingValueObjectsAndChildEntities();
39+
return Ok(response);
40+
}
41+
42+
43+
// GET api/ordering/orders/xxxGUIDxxxx
44+
[HttpGet("orders/{orderId:Guid}")]
45+
public async Task<IActionResult> GetOrderByGuid(Guid orderId)
46+
{
47+
//var order = await _orderRepository.Get(orderId);
48+
49+
var order = await _context.Orders
50+
.Include(o => o.ShippingAddress)
51+
.Include(o => o.BillingAddress)
52+
.Where(o => o.Id == orderId)
53+
.SingleOrDefaultAsync<Order>();
54+
55+
// Dynamically generated a Response-Model that includes only the fields you need in the response.
56+
// This keeps the JSON response minimal.
57+
// Could also use var
58+
dynamic response = new
59+
{
60+
id = order.Id,
61+
orderDate = order.OrderDate,
62+
shippingAddress = order.ShippingAddress,
63+
billingAddress = order.BillingAddress,
64+
//items = order.Items.Select(i => i.Content)
65+
};
66+
67+
return Ok(response);
68+
}
69+
70+
//Alternate method if using several parameters instead of part of the URL
71+
// GET api/ordering/orders/?orderId=xxxGUIDxxx&otherParam=value
72+
//[HttpGet("orders")]
73+
//public Order GetOrderByGuid([FromUri] Guid orderId, [FromUri] string otherParam)
74+
75+
76+
// POST api/ordering/orders/create
77+
[HttpPut("orders/create")]
78+
public async Task<int> Post([FromBody]Order order)
79+
{
80+
return await _orderRepository.Add(order);
81+
82+
//_context.Orders.Add(order);
83+
//return await _context.SaveChangesAsync();
84+
}
85+
86+
// PUT api/ordering/orders/xxxOrderGUIDxxxx/update
87+
[HttpPut("orders/{orderId:Guid}/update")]
88+
public async Task<int> UpdateOrder(Guid orderID, [FromBody] Order orderToUpdate)
89+
{
90+
return await _orderRepository.Add(orderToUpdate);
91+
92+
//_context.Orders.Update(orderToUpdate);
93+
//return await _context.SaveChangesAsync();
94+
}
95+
96+
// DELETE api/ordering/orders/xxxOrderGUIDxxxx
97+
[HttpDelete("orders/{orderId:Guid}/delete")]
98+
public async Task<int> Delete(Guid id)
99+
{
100+
return await _orderRepository.Remove(id);
101+
102+
//Order orderToDelete = _context.Orders
103+
// .Where(o => o.Id == id)
104+
// .SingleOrDefault();
105+
106+
//_context.Orders.Remove(orderToDelete);
107+
//return await _context.SaveChangesAsync();
108+
}
109+
110+
}
111+
112+
}
113+
114+

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

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/Services/Ordering/Ordering.API/DTO/OrderDTO.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Services/Ordering/Ordering.API/DTO/OrderItemDTO.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Services/Ordering/Ordering.API/Migrations/20160909202620_MyFirstMigration.Designer.cs

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

src/Services/Ordering/Ordering.API/Migrations/20160909223213_Migration2.Designer.cs

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

src/Services/Ordering/Ordering.API/Migrations/20160909233852_Migration3.Designer.cs

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

src/Services/Ordering/Ordering.API/Migrations/OrderingContextModelSnapshot.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
using Microsoft.EntityFrameworkCore.Infrastructure;
44
using Microsoft.EntityFrameworkCore.Metadata;
55
using Microsoft.EntityFrameworkCore.Migrations;
6-
using Microsoft.eShopOnContainers.Services.Ordering.API.UnitOfWork;
6+
using Microsoft.eShopOnContainers.Services.Ordering.SqlData.UnitOfWork;
77

88
namespace Ordering.API.Migrations
99
{
10-
[DbContext(typeof(OrderingContext))]
10+
[DbContext(typeof(OrderingDbContext))]
1111
partial class OrderingContextModelSnapshot : ModelSnapshot
1212
{
1313
protected override void BuildModel(ModelBuilder modelBuilder)

src/Services/Ordering/Ordering.API/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "api/orders",
14+
"launchUrl": "api/ordering/orders",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}

0 commit comments

Comments
 (0)