Advertisement
GabrielDas

Untitled

Apr 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using GameStore.Data;
  5. using GameStore.Models;
  6. using Microsoft.AspNetCore.Mvc;
  7.  
  8. namespace GameStore.Controllers
  9. {
  10. public class GameController : Controller
  11. {
  12.  
  13. public IActionResult Index()
  14. {
  15. using (var db = new GameStoreDbContext())
  16. {
  17.  
  18. var allGames = db.Games.ToList();
  19. return View(allGames);
  20.  
  21.  
  22. }
  23. }
  24.  
  25. [HttpGet]
  26. public IActionResult Create()
  27. {
  28.  
  29. return View();
  30.  
  31. }
  32.  
  33. [HttpPost]
  34. public IActionResult Create(Game game)
  35. {
  36. using (var db = new GameStoreDbContext())
  37. {
  38. if (!ModelState.IsValid)
  39. {
  40. return RedirectToAction("Index");
  41.  
  42. }
  43.  
  44. db.Games.Add(game);
  45. db.SaveChanges();
  46. }
  47.  
  48. return RedirectToAction("Index");
  49. }
  50.  
  51. [HttpGet]
  52. public IActionResult Edit(int id)
  53. {
  54. using (var db = new GameStoreDbContext())
  55. {
  56. var toEdit = db.Games.FirstOrDefault(x=>x.Id == id);
  57.  
  58. if (toEdit==null)
  59. {
  60. return RedirectToAction("Index");
  61.  
  62. }
  63.  
  64. return View(toEdit);
  65.  
  66. }
  67.  
  68. }
  69.  
  70. [HttpPost]
  71. public IActionResult Edit(Game game)
  72. {
  73. using (var db = new GameStoreDbContext())
  74. {
  75. var toEdit = db.Games.FirstOrDefault(x => x.Id == game.Id);
  76.  
  77. if (toEdit == null)
  78. {
  79. return RedirectToAction("Index");
  80.  
  81. }
  82.  
  83. toEdit.Name = game.Name;
  84. toEdit.Dlc = game.Dlc;
  85. toEdit.Platform = game.Platform;
  86. toEdit.Price = game.Price;
  87.  
  88. db.SaveChanges();
  89.  
  90.  
  91. return RedirectToAction("Index");
  92.  
  93. }
  94.  
  95.  
  96. }
  97.  
  98. [HttpGet]
  99. public IActionResult Delete(int id)
  100. {
  101. using (var db = new GameStoreDbContext())
  102. {
  103. var toDelete = db.Games.FirstOrDefault(x => x.Id == id);
  104.  
  105. if (toDelete == null)
  106. {
  107. return RedirectToAction("Index");
  108.  
  109. }
  110.  
  111. return View(toDelete);
  112.  
  113. }
  114. }
  115.  
  116. [HttpPost]
  117. public IActionResult Delete(Game game)
  118. {
  119.  
  120. using (var db = new GameStoreDbContext())
  121. {
  122. var toDelete = db.Games.FirstOrDefault(x => x.Id == game.Id);
  123.  
  124. if (toDelete == null)
  125. {
  126. return RedirectToAction("Index");
  127.  
  128. }
  129.  
  130. db.Games.Remove(toDelete);
  131. db.SaveChanges();
  132.  
  133.  
  134. return RedirectToAction("Index");
  135.  
  136. }
  137.  
  138.  
  139. }
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement