Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using GameStore.Data;
- using GameStore.Models;
- using Microsoft.AspNetCore.Mvc;
- namespace GameStore.Controllers
- {
- public class GameController : Controller
- {
- public IActionResult Index()
- {
- using (var db = new GameStoreDbContext())
- {
- var allGames = db.Games.ToList();
- return View(allGames);
- }
- }
- [HttpGet]
- public IActionResult Create()
- {
- return View();
- }
- [HttpPost]
- public IActionResult Create(Game game)
- {
- using (var db = new GameStoreDbContext())
- {
- if (!ModelState.IsValid)
- {
- return RedirectToAction("Index");
- }
- db.Games.Add(game);
- db.SaveChanges();
- }
- return RedirectToAction("Index");
- }
- [HttpGet]
- public IActionResult Edit(int id)
- {
- using (var db = new GameStoreDbContext())
- {
- var toEdit = db.Games.FirstOrDefault(x=>x.Id == id);
- if (toEdit==null)
- {
- return RedirectToAction("Index");
- }
- return View(toEdit);
- }
- }
- [HttpPost]
- public IActionResult Edit(Game game)
- {
- using (var db = new GameStoreDbContext())
- {
- var toEdit = db.Games.FirstOrDefault(x => x.Id == game.Id);
- if (toEdit == null)
- {
- return RedirectToAction("Index");
- }
- toEdit.Name = game.Name;
- toEdit.Dlc = game.Dlc;
- toEdit.Platform = game.Platform;
- toEdit.Price = game.Price;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- [HttpGet]
- public IActionResult Delete(int id)
- {
- using (var db = new GameStoreDbContext())
- {
- var toDelete = db.Games.FirstOrDefault(x => x.Id == id);
- if (toDelete == null)
- {
- return RedirectToAction("Index");
- }
- return View(toDelete);
- }
- }
- [HttpPost]
- public IActionResult Delete(Game game)
- {
- using (var db = new GameStoreDbContext())
- {
- var toDelete = db.Games.FirstOrDefault(x => x.Id == game.Id);
- if (toDelete == null)
- {
- return RedirectToAction("Index");
- }
- db.Games.Remove(toDelete);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement