OrdersController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using Microsoft.EntityFrameworkCore;
  8. using ShoeShopMvc.Models;
  9. namespace ShoeShopMvc.Controllers
  10. {
  11. public class OrdersController : Controller
  12. {
  13. private readonly ShoeShopContext _context;
  14. public OrdersController(ShoeShopContext context)
  15. {
  16. _context = context;
  17. }
  18. // GET: Orders
  19. public async Task<IActionResult> Index()
  20. {
  21. return View(await _context.Orders.ToListAsync());
  22. }
  23. // GET: Orders/Details/5
  24. public async Task<IActionResult> Details(string id)
  25. {
  26. if (id == null)
  27. {
  28. return NotFound();
  29. }
  30. var order = await _context.Orders
  31. .FirstOrDefaultAsync(m => m.OrdersId == id);
  32. if (order == null)
  33. {
  34. return NotFound();
  35. }
  36. return View(order);
  37. }
  38. // GET: Orders/Create
  39. public IActionResult Create()
  40. {
  41. return View();
  42. }
  43. // POST: Orders/Create
  44. // To protect from overposting attacks, enable the specific properties you want to bind to.
  45. // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
  46. [HttpPost]
  47. [ValidateAntiForgeryToken]
  48. public async Task<IActionResult> Create([Bind("OrdersId,OrdersDate,DeliveryDate,AddressPickpoints,ClientId,Cod,StatusId")] Order order)
  49. {
  50. if (ModelState.IsValid)
  51. {
  52. _context.Add(order);
  53. await _context.SaveChangesAsync();
  54. return RedirectToAction(nameof(Index));
  55. }
  56. return View(order);
  57. }
  58. // GET: Orders/Edit/5
  59. public async Task<IActionResult> Edit(string id)
  60. {
  61. if (id == null)
  62. {
  63. return NotFound();
  64. }
  65. var order = await _context.Orders.FindAsync(id);
  66. if (order == null)
  67. {
  68. return NotFound();
  69. }
  70. return View(order);
  71. }
  72. // POST: Orders/Edit/5
  73. // To protect from overposting attacks, enable the specific properties you want to bind to.
  74. // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
  75. [HttpPost]
  76. [ValidateAntiForgeryToken]
  77. public async Task<IActionResult> Edit(string id, [Bind("OrdersId,OrdersDate,DeliveryDate,AddressPickpoints,ClientId,Cod,StatusId")] Order order)
  78. {
  79. if (id != order.OrdersId)
  80. {
  81. return NotFound();
  82. }
  83. if (ModelState.IsValid)
  84. {
  85. try
  86. {
  87. _context.Update(order);
  88. await _context.SaveChangesAsync();
  89. }
  90. catch (DbUpdateConcurrencyException)
  91. {
  92. if (!OrderExists(order.OrdersId))
  93. {
  94. return NotFound();
  95. }
  96. else
  97. {
  98. throw;
  99. }
  100. }
  101. return RedirectToAction(nameof(Index));
  102. }
  103. return View(order);
  104. }
  105. // GET: Orders/Delete/5
  106. public async Task<IActionResult> Delete(string id)
  107. {
  108. if (id == null)
  109. {
  110. return NotFound();
  111. }
  112. var order = await _context.Orders
  113. .FirstOrDefaultAsync(m => m.OrdersId == id);
  114. if (order == null)
  115. {
  116. return NotFound();
  117. }
  118. return View(order);
  119. }
  120. // POST: Orders/Delete/5
  121. [HttpPost, ActionName("Delete")]
  122. [ValidateAntiForgeryToken]
  123. public async Task<IActionResult> DeleteConfirmed(string id)
  124. {
  125. var order = await _context.Orders.FindAsync(id);
  126. if (order != null)
  127. {
  128. _context.Orders.Remove(order);
  129. }
  130. await _context.SaveChangesAsync();
  131. return RedirectToAction(nameof(Index));
  132. }
  133. private bool OrderExists(string id)
  134. {
  135. return _context.Orders.Any(e => e.OrdersId == id);
  136. }
  137. }
  138. }