Skip to content

Commit 7ea55ab

Browse files
committed
Set up database for Customers and Movies
1 parent 5b7db28 commit 7ea55ab

File tree

49 files changed

+2410
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2410
-29
lines changed

Vidly3/App_Start/RouteConfig.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ public static void RegisterRoutes(RouteCollection routes)
1313
{
1414
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
1515

16+
routes.MapMvcAttributeRoutes(); //allow attribute routing in controllers
17+
18+
////custom route below
19+
//routes.MapRoute(
20+
// "MoviesByReleaseDate", //name
21+
// "movies/released/{year}/{month}", //url
22+
// new { controller = "Movies", action = "ByReleaseDate"}, //defaults. must be same names as controller and action
23+
// new { year = @"\d{4}", month = @"\d{2}" }); //constraints. can also do "\\d, but @ makes up for first \
24+
// //would set year = {2015|2016} if we only wanted those exact years. | is an or
25+
1626
routes.MapRoute(
1727
name: "Default",
1828
url: "{controller}/{action}/{id}",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Linq;
5+
using System.Web;
6+
using System.Web.Mvc;
7+
using Vidly3.Models;
8+
9+
namespace Vidly3.Controllers
10+
{
11+
public class CustomersController : Controller
12+
{
13+
//for using db data in this controller
14+
private ApplicationDbContext _context;
15+
16+
public CustomersController()
17+
{
18+
_context = new ApplicationDbContext();
19+
}
20+
21+
protected override void Dispose(bool disposing)
22+
{
23+
_context.Dispose();
24+
}
25+
26+
//GET Customers
27+
public ViewResult Index()
28+
{
29+
//get all customers
30+
//use toList to be query over customers immediately instead of in foreach statement of view
31+
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
32+
33+
return View(customers);
34+
}
35+
36+
//GET Customers/Details/id
37+
public ActionResult Details(int id)
38+
{
39+
//get single customer by id
40+
var customer = _context.Customers.Include(c => c.MembershipType).SingleOrDefault(c => c.Id == id);
41+
42+
if (customer == null)
43+
return HttpNotFound();
44+
45+
return View(customer);
46+
}
47+
48+
////mock data
49+
////used to call GetCustomers() in methods instead of _context
50+
//private IEnumerable<Customer> GetCustomers()
51+
//{
52+
// return new List<Customer>
53+
// {
54+
// new Customer {Id = 1, Name = "John Smith"},
55+
// new Customer {Id = 2, Name = "Mary Williams"}
56+
// };
57+
//}
58+
}
59+
}
Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,127 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data.Entity;
34
using System.Linq;
45
using System.Web;
56
using System.Web.Mvc;
67
using Vidly3.Models; //require models
8+
using Vidly3.ViewModels; //require ViewModels for customers list
79

810
namespace Vidly3.Controllers
911
{
1012
public class MoviesController : Controller
1113
{
12-
// GET: Movies/Random
13-
public ActionResult Random()
14+
private ApplicationDbContext _context;
15+
16+
public MoviesController()
17+
{
18+
_context = new ApplicationDbContext();
19+
}
20+
21+
protected override void Dispose(bool disposing)
1422
{
15-
var movie = new Movie() { Name = "Shrek!" };
23+
_context.Dispose();
24+
}
25+
26+
public ViewResult Index()
27+
{
28+
//var movies = GetMovies();
29+
var movies = _context.Movies.Include(m => m.Genre).ToList();
1630

17-
return View(movie); //need view called Random that references movie
31+
return View(movies);
1832
}
33+
34+
public ActionResult Details(int id)
35+
{
36+
var movie = _context.Movies.Include(m => m.Genre).SingleOrDefault(m => m.Id == id);
37+
38+
if (movie == null)
39+
return HttpNotFound();
40+
41+
42+
return View(movie);
43+
}
44+
45+
//private IEnumerable<Movie> GetMovies()
46+
//{
47+
// return new List<Movie> {
48+
// new Movie { Id=1, Name = "Shrek"},
49+
// new Movie { Id=2, Name = "Wall-E"}
50+
// };
51+
//}
52+
//// GET: Movies/Random
53+
//public ActionResult Random() //uses Random.cshtml view
54+
//{
55+
// var movie = new Movie() { Name = "Shrek!" };
56+
57+
// //add this mock data after creating RandomMovieViewModel
58+
// var customers = new List<Customer>
59+
// {
60+
// new Customer {Name = "Customer 1"},
61+
// new Customer {Name = "Customer 2"}
62+
// };
63+
64+
// var viewModel = new RandomMovieViewModel
65+
// {
66+
// Movie = movie,
67+
// Customers = customers
68+
// };
69+
70+
// ////ViewData and ViewBag are shit. do not use
71+
// ////to use ViewData must reference @ViewData in view
72+
// //ViewData["Movie"] = movie; //no longer need to pass data in to View method
73+
74+
// //View(model) is short for var viewResult = new ViewResult(); viewResult.ViewData.Model
75+
// //movie would be on the Model property like it is in the view
76+
77+
// //return View(movie);
78+
79+
80+
// //after adding viewModel pass in the entire viewModel to include customers
81+
// return View(viewModel);
82+
//}
83+
84+
////Movies/released/{year}/{month}
85+
////include contraints with regex and range. 4 digits for year, 2 digits for month
86+
////can google ASP.NET Attribute Route Contraints to see others
87+
//[Route("movies/released/{year:regex(\\d{4})}/{month:regex(\\d{2}):range(1,12)}")]
88+
//public ActionResult ByReleaseDate(int year, int month)
89+
//{
90+
// return Content(year + "/" + month);
91+
//}
92+
93+
94+
95+
96+
97+
//// GET: Movies/Random
98+
//public ActionResult Random() //could also return a ViewResult
99+
//{
100+
// var movie = new Movie() { Name = "Shrek!" };
101+
102+
// return View(movie); //need view called Random that references movie. View is method from controller class.
103+
// //return Content("Hello World!"); //return plain text
104+
// //return HttpNotFound(); //return standard 404
105+
// //return new EmptyResult(); //return nothing
106+
// //return RedirectToAction("Index", "Home", new {page = 1, sortBy = "name"}); //specify action, controller to redirect to, and in object we provide the query string params
107+
//}
108+
109+
//public ActionResult Edit(int id) //id parameter taken automatically from url
110+
// //do not change name of param because it is {id} in RouteConfig.
111+
//{
112+
// return Content("id=" + id);
113+
//}
114+
115+
//// GET movies. return a query string as text
116+
//// ? to make param nullible. string is nullable automatically in C#
117+
//public ActionResult Index(int? pageIndex, string sortBy)
118+
//{
119+
// if (!pageIndex.HasValue) //set default
120+
// pageIndex = 1;
121+
// if (String.IsNullOrWhiteSpace(sortBy)) //set default
122+
// sortBy = "Name";
123+
124+
// return Content(String.Format("pageIndex={0}&sortBy={1}", pageIndex, sortBy));
125+
//}
19126
}
20127
}

Vidly3/Migrations/201912032000511_InitialModel.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
namespace Vidly3.Migrations
2+
{
3+
using System;
4+
using System.Data.Entity.Migrations;
5+
6+
public partial class InitialModel : DbMigration
7+
{
8+
public override void Up()
9+
{
10+
CreateTable(
11+
"dbo.Customers",
12+
c => new
13+
{
14+
Id = c.Int(nullable: false, identity: true),
15+
Name = c.String(),
16+
})
17+
.PrimaryKey(t => t.Id);
18+
19+
CreateTable(
20+
"dbo.AspNetRoles",
21+
c => new
22+
{
23+
Id = c.String(nullable: false, maxLength: 128),
24+
Name = c.String(nullable: false, maxLength: 256),
25+
})
26+
.PrimaryKey(t => t.Id)
27+
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
28+
29+
CreateTable(
30+
"dbo.AspNetUserRoles",
31+
c => new
32+
{
33+
UserId = c.String(nullable: false, maxLength: 128),
34+
RoleId = c.String(nullable: false, maxLength: 128),
35+
})
36+
.PrimaryKey(t => new { t.UserId, t.RoleId })
37+
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
38+
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
39+
.Index(t => t.UserId)
40+
.Index(t => t.RoleId);
41+
42+
CreateTable(
43+
"dbo.AspNetUsers",
44+
c => new
45+
{
46+
Id = c.String(nullable: false, maxLength: 128),
47+
Email = c.String(maxLength: 256),
48+
EmailConfirmed = c.Boolean(nullable: false),
49+
PasswordHash = c.String(),
50+
SecurityStamp = c.String(),
51+
PhoneNumber = c.String(),
52+
PhoneNumberConfirmed = c.Boolean(nullable: false),
53+
TwoFactorEnabled = c.Boolean(nullable: false),
54+
LockoutEndDateUtc = c.DateTime(),
55+
LockoutEnabled = c.Boolean(nullable: false),
56+
AccessFailedCount = c.Int(nullable: false),
57+
UserName = c.String(nullable: false, maxLength: 256),
58+
})
59+
.PrimaryKey(t => t.Id)
60+
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
61+
62+
CreateTable(
63+
"dbo.AspNetUserClaims",
64+
c => new
65+
{
66+
Id = c.Int(nullable: false, identity: true),
67+
UserId = c.String(nullable: false, maxLength: 128),
68+
ClaimType = c.String(),
69+
ClaimValue = c.String(),
70+
})
71+
.PrimaryKey(t => t.Id)
72+
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
73+
.Index(t => t.UserId);
74+
75+
CreateTable(
76+
"dbo.AspNetUserLogins",
77+
c => new
78+
{
79+
LoginProvider = c.String(nullable: false, maxLength: 128),
80+
ProviderKey = c.String(nullable: false, maxLength: 128),
81+
UserId = c.String(nullable: false, maxLength: 128),
82+
})
83+
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
84+
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
85+
.Index(t => t.UserId);
86+
87+
}
88+
89+
public override void Down()
90+
{
91+
DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
92+
DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
93+
DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
94+
DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
95+
DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
96+
DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
97+
DropIndex("dbo.AspNetUsers", "UserNameIndex");
98+
DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
99+
DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
100+
DropIndex("dbo.AspNetRoles", "RoleNameIndex");
101+
DropTable("dbo.AspNetUserLogins");
102+
DropTable("dbo.AspNetUserClaims");
103+
DropTable("dbo.AspNetUsers");
104+
DropTable("dbo.AspNetUserRoles");
105+
DropTable("dbo.AspNetRoles");
106+
DropTable("dbo.Customers");
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)