-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdersController.cs
More file actions
95 lines (79 loc) · 2.76 KB
/
OrdersController.cs
File metadata and controls
95 lines (79 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DataModel;
using WebApiService;
namespace WebApiService.Controllers {
[Route("api/[controller]")]
[ApiController]
public class OrdersController : ControllerBase {
private readonly CrmContext _context;
public OrdersController(CrmContext context) {
_context = context;
}
// GET: api/Orders
[HttpGet]
public async Task<ActionResult<IEnumerable<Order>>> GetOrders() {
return await _context.Orders.Include(order => order.Customer)
.Include(order => order.Items)
.ThenInclude(orderItem => orderItem.Product)
.ToListAsync();
}
// GET: api/Orders/5
[HttpGet("{id}")]
public async Task<ActionResult<Order>> GetOrder(int id) {
var order = await _context.Orders.FindAsync(id);
if (order == null) {
return NotFound();
}
return order;
}
// PUT: api/Orders/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutOrder(int id, Order order) {
if (id != order.Id) {
return BadRequest();
}
_context.Entry(order).State = EntityState.Modified;
try {
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) {
if (!OrderExists(id)) {
return NotFound();
}
else {
throw;
}
}
return NoContent();
}
// POST: api/Orders
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Order>> PostOrder(Order order) {
_context.Orders.Add(order);
await _context.SaveChangesAsync();
return CreatedAtAction("GetOrder", new { id = order.Id }, order);
}
// DELETE: api/Orders/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteOrder(int id) {
var order = await _context.Orders.FindAsync(id);
if (order == null) {
return NotFound();
}
_context.Orders.Remove(order);
await _context.SaveChangesAsync();
return NoContent();
}
private bool OrderExists(int id) {
return _context.Orders.Any(e => e.Id == id);
}
}
}