PageRenderTime 1637ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/CAPAMovies/src/CAPAMovies/Data/ApplicationDbContext.cs

https://gitlab.com/pavel.ivanov/JP
C# | 277 lines | 255 code | 19 blank | 3 comment | 11 complexity | f0957d8f9701d1a525b4a80c9074dc80 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using CAPAMovies.Models;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.EntityFrameworkCore.Metadata;
  9. using Microsoft.Extensions.DependencyInjection;
  10. namespace CAPAMovies.Data
  11. {
  12. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  13. {
  14. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
  15. : base(options)
  16. {
  17. }
  18. public DbSet<Movie> Movies { get; set; }
  19. public DbSet<UserMovie> UserMovies { get; set; }
  20. public DbSet<Format> Formats { get; set; }
  21. protected override void OnModelCreating(ModelBuilder builder)
  22. {
  23. base.OnModelCreating(builder);
  24. // Customize the ASP.NET Identity model and override the defaults if needed.
  25. // For example, you can rename the ASP.NET Identity table names and more.
  26. // Add your customizations after calling base.OnModelCreating(builder);
  27. builder.Entity<UserMovie>().HasKey(k => new { k.UserID, k.MovieID, k.FormatID }); //Set composite key
  28. builder.Entity<UserMovie>().HasOne(f => f.User).WithMany().OnDelete(DeleteBehavior.Restrict); //Do not delete the User when UserMovie is deleted
  29. builder.Entity<UserMovie>().HasOne(f => f.Movie).WithMany().OnDelete(DeleteBehavior.Restrict); //Do not delete the Movie when UserMovie is deleted
  30. builder.Entity<UserMovie>().HasOne(f => f.Format).WithMany().OnDelete(DeleteBehavior.Restrict); //Do not delete the Format when UserMovie is deleted
  31. builder.Entity<Format>().HasAlternateKey(f => f.FormatName); //Add unique constraint
  32. }
  33. public static void Seed(IApplicationBuilder app)
  34. {
  35. SeedUsers(app);
  36. SeedFormats(app);
  37. SeedMovies(app);
  38. SeedUserMovies(app);
  39. }
  40. public static void SeedUsers(IApplicationBuilder app)
  41. {
  42. var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>();
  43. context.Database.EnsureCreated();
  44. string[] userEmail = { "admin@user.com", "crystalstevens@capamovies.com", "alexhouston@capamovies.com", "paulivanov@capamovies.com", "aaronday@capamovies.com" };
  45. const string password = "Secret123$";
  46. var userManager = app.ApplicationServices.GetRequiredService<UserManager<ApplicationUser>>();
  47. var roleManager = app.ApplicationServices.GetRequiredService<RoleManager<IdentityRole>>();
  48. foreach (var s in userEmail)
  49. {
  50. var user = userManager.Users.SingleOrDefault(u => u.Email == s);
  51. if (user != null) continue;
  52. user = new ApplicationUser()
  53. {
  54. UserName = s,
  55. Email = s,
  56. EmailConfirmed = true
  57. };
  58. var result = userManager.CreateAsync(user, password).Result;
  59. context.SaveChanges();
  60. }
  61. if (!roleManager.RoleExistsAsync("Admin").Result)
  62. {
  63. var result = roleManager.CreateAsync(new IdentityRole { Name = "Admin" }).Result;
  64. context.SaveChanges();
  65. }
  66. var manager = userManager.Users.SingleOrDefault(u => u.Email == "admin@user.com");
  67. if (manager != null)
  68. {
  69. var result = userManager.AddToRoleAsync(manager, "Admin").Result;
  70. context.SaveChanges();
  71. }
  72. }
  73. public static void SeedFormats(IApplicationBuilder app)
  74. {
  75. var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>();
  76. if (context.Formats.AsNoTracking().Any()) return;
  77. var format = new[]
  78. {
  79. new Format { FormatName = "None", Physical = false },
  80. new Format { FormatName = "DVD", Physical = true },
  81. new Format { FormatName = "Blu-Ray", Physical = true },
  82. new Format { FormatName = "3D Blu-Ray", Physical = true },
  83. new Format { FormatName = "iTunes", Physical = false },
  84. new Format { FormatName = "UltraViolet", Physical = false },
  85. new Format { FormatName = "MP4", Physical = false },
  86. new Format { FormatName = "AVI", Physical = false },
  87. new Format { FormatName = "HD", Physical = true },
  88. new Format { FormatName = "VHS", Physical = true }
  89. };
  90. context.Formats.AddRange(format);
  91. context.SaveChanges();
  92. }
  93. public static void SeedMovies(IApplicationBuilder app)
  94. {
  95. var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>();
  96. if (context.Movies.AsNoTracking().Any()) return;
  97. var movies = new[]
  98. {
  99. new Movie
  100. {
  101. ID = "tt0076759",
  102. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BYzQ2OTk4N2QtOGQwNy00MmI3LWEwNmEtOTk0OTY3NDk2MGJkL2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNjc1NTYyMjg@._V1_UX182_CR0,0,182,268_AL_.jpg",
  103. Title = "Star Wars: Episode IV - A New Hope",
  104. Year = 1977
  105. },
  106. new Movie
  107. {
  108. ID = "tt0458339",
  109. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BMTYzOTc2NzU3N15BMl5BanBnXkFtZTcwNjY3MDE3NQ@@._V1_UX182_CR0,0,182,268_AL_.jpg",
  110. Title = "Captain America: The First Avenger",
  111. Year = 2011
  112. },
  113. new Movie
  114. {
  115. ID = "tt0120815",
  116. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BZjhkMDM4MWItZTVjOC00ZDRhLThmYTAtM2I5NzBmNmNlMzI1XkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_SX300.jpg",
  117. Title = "Saving Private Ryan",
  118. Year = 1998
  119. },
  120. new Movie
  121. {
  122. ID = "tt0172495",
  123. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BMTgwMzQzNTQ1Ml5BMl5BanBnXkFtZTgwMDY2NTYxMTE@._V1_SX300.jpg",
  124. Title = "Gladiator",
  125. Year = 2000
  126. },
  127. new Movie
  128. {
  129. ID = "tt0988045",
  130. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BMTg0NjEwNjUxM15BMl5BanBnXkFtZTcwMzk0MjQ5Mg@@._V1_SX300.jpg",
  131. Title = "Sherlock Holmes",
  132. Year = 2009
  133. },
  134. new Movie
  135. {
  136. ID = "tt0258463",
  137. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BM2JkNGU0ZGMtZjVjNS00NjgyLWEyOWYtZmRmZGQyN2IxZjA2XkEyXkFqcGdeQXVyNTIzOTk5ODM@._V1_SX300.jpg",
  138. Title = "The Bourne Identity",
  139. Year = 2002
  140. },
  141. new Movie
  142. {
  143. ID = "tt0499549",
  144. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg",
  145. Title = "Avatar",
  146. Year = 2009
  147. },
  148. new Movie
  149. {
  150. ID = "tt0109830",
  151. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BYThjM2MwZGMtMzg3Ny00NGRkLWE4M2EtYTBiNWMzOTY0YTI4XkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_UY268_CR10,0,182,268_AL_.jpg",
  152. Title = "Forest Gump",
  153. Year = 1994
  154. },
  155. new Movie
  156. {
  157. ID = "tt0099685",
  158. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BNThjMzczMjctZmIwOC00NTQ4LWJhZWItZDdhNTk5ZTdiMWFlXkEyXkFqcGdeQXVyNDYyMDk5MTU@._V1_SX300.jpg",
  159. Title = "Goodfellas",
  160. Year = 1990
  161. },
  162. new Movie
  163. {
  164. ID = "tt0169547",
  165. PicLink = "https://images-na.ssl-images-amazon.com/images/M/MV5BMjM4NTI5NzYyNV5BMl5BanBnXkFtZTgwNTkxNTYxMTE@._V1_SX300.jpg",
  166. Title = "American Beauty",
  167. Year = 1999
  168. }
  169. };
  170. context.Movies.AddRange(movies);
  171. context.SaveChanges();
  172. }
  173. public static void SeedUserMovies(IApplicationBuilder app)
  174. {
  175. var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>();
  176. if (context.UserMovies.AsNoTracking().Any()) return;
  177. var userManager = app.ApplicationServices.GetRequiredService<UserManager<ApplicationUser>>();
  178. var ids = userManager.Users.Where(u => u.Email.EndsWith("@capamovies.com")).Select(u => u.Id).ToList();
  179. if (ids.Count() < 4) return;
  180. var userMovies = new[]
  181. {
  182. new UserMovie
  183. {
  184. UserID = ids[0],
  185. MovieID = "tt0076759",
  186. FormatID = 2,
  187. TitleSort = "Star Wars: Episode IV - A New Hope",
  188. DateAdded = new DateTime(2017, 1, 1),
  189. StarRating = 3,
  190. Location = "Shelf 1"
  191. },
  192. new UserMovie
  193. {
  194. UserID = ids[0],
  195. MovieID = "tt0076759",
  196. FormatID = 3,
  197. TitleSort = "Star Wars: Episode IV - A New Hope",
  198. DateAdded = new DateTime(2017, 1, 2),
  199. StarRating = 3,
  200. Location = "Shelf 1"
  201. },
  202. new UserMovie
  203. {
  204. UserID = ids[0],
  205. MovieID = "tt0258463",
  206. FormatID = 6,
  207. TitleSort = "Bourne Identity, The",
  208. DateAdded = new DateTime(2017, 2, 1),
  209. StarRating = 4
  210. },
  211. new UserMovie
  212. {
  213. UserID = ids[1],
  214. MovieID = "tt0988045",
  215. FormatID = 5,
  216. TitleSort = "Sherlock Holmes",
  217. DateAdded = new DateTime(2017, 2, 2),
  218. StarRating = 4,
  219. Location = "Shelf 3"
  220. },
  221. new UserMovie
  222. {
  223. UserID = ids[2],
  224. MovieID = "tt0169547",
  225. FormatID = 4,
  226. TitleSort = "American Beauty",
  227. DateAdded = new DateTime(2017, 1, 31),
  228. StarRating = 3,
  229. Location = "Desk"
  230. },
  231. new UserMovie
  232. {
  233. UserID = ids[2],
  234. MovieID = "tt0120815",
  235. FormatID = 5,
  236. TitleSort = "Saving Private Ryan",
  237. DateAdded = new DateTime(2017, 2, 10),
  238. StarRating = 5,
  239. Location = "Bookcase",
  240. Custom = "Kick Ass!!!"
  241. },
  242. new UserMovie
  243. {
  244. UserID = ids[3],
  245. MovieID = "tt0458339",
  246. FormatID = 4,
  247. TitleSort = "Captain America: The First Avenger",
  248. DateAdded = new DateTime(2016, 10, 14),
  249. StarRating = 5,
  250. Location = "Bookshelf",
  251. Custom = "Awesome!!!"
  252. }
  253. };
  254. context.UserMovies.AddRange(userMovies);
  255. context.SaveChanges();
  256. }
  257. }
  258. }