Skip to content

Commit

Permalink
Merged code from 2-1 with master, cleaned up comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaytonanderson committed Jan 23, 2017
1 parent 77e2085 commit 0e1a0bc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
11 changes: 4 additions & 7 deletions CharacterSheetApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,44 @@ namespace CharacterSheetApp.Controllers
{
public class HomeController : Controller
{
// this adds a new variable for our ApplicationDbContext class named _context
private readonly ApplicationDbContext _context;

public HomeController(ApplicationDbContext context)
{
// we will set our readonly variable to the variable passed in to the method. This is known as a Construction Injection. Injects ApplicationDbContext into our controller.
_context = context;
}

public IActionResult Create(Character character)
{
_context.Characters.Add(character);
// commits the changes to the DB:
_context.SaveChanges();

return RedirectToAction("Index");
}

public IActionResult Index()
{
// .ToList() takes our db collection into a List collection.
var model = _context.Characters.ToList();

return View(model);
}

public IActionResult GetActive()
{
// the .Where() method accepts a LAMBDA EXPRESSION (e.g., e => . . . ) which checks for any record in the collection that returns true for what we are looking for.
var model = _context.Characters.Where(e => e.IsActive).ToList();

return View(model);
}

public IActionResult Details(string name)
{
// .FirstOrDefault() returns a single result, rather than a collection. The lambda expression here checks for a record that has a name which matches the input name.
var model = _context.Characters.FirstOrDefault(e => e.Name == name);

return View(model);
}

public IActionResult Update(Character character)
{
// to update a record, we can use Entity to locate and set our data, then set its State to Modified, letting the EntityFramework know we have changed this record.
_context.Entry(character).State = EntityState.Modified;
_context.SaveChanges();

Expand Down
2 changes: 0 additions & 2 deletions CharacterSheetApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
// Infrastructure lets us have an in-memory database; if we were to use a SQL server, MySQL server, or other, we will change this accordingly.
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace CharacterSheetApp
Expand All @@ -20,7 +19,6 @@ public void ConfigureServices(IServiceCollection services)
services.AddEntityFramework()
.AddDbContext<Models.ApplicationDbContext>();

// allows MVC to be accessible throughout the application
services.AddMvc();
}
}
Expand Down

0 comments on commit 0e1a0bc

Please sign in to comment.