Example #1
0
        public IActionResult Post([FromBody]Movie movie)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (movie.Id == Guid.Empty)
                    {
                        movie.Id = Guid.NewGuid();
                        Actor actor = new Actor() { Name = "Chuck Norris", MovieId = movie.Id };
                        
                        _unitOfWork.MovieRepository.Add(movie);
                        _unitOfWork.ActorRepository.Add(actor);
                        _unitOfWork.Save();

                        return new ObjectResult(movie);
                    }
                    else
                    {
                        
                        var original = _unitOfWork.MovieRepository.Get(m => m.Id == movie.Id);
                        original.Actors.Add(new Actor() { Name = "Van Damme", MovieId = movie.Id });
                        original.Title = movie.Title;
                        original.Director = movie.Director;
                        original.TicketPrice = movie.TicketPrice;
                        original.ReleaseDate = movie.ReleaseDate;
                        _unitOfWork.MovieRepository.SaveChanges();
                        return new ObjectResult(original);
                        /*string username = "******";
                        string password = "******";
                        
                        SignInResult signInStatus =  await _signInManager.PasswordSignInAsync(username, password, false, false);
                        if (signInStatus == SignInResult.Success)
                        {
                            var original = UoW.MovieRepository.Get(m => m.Id == movie.Id);
                            original.Actors.Add(new Actor() { Name = "Van Damme", MovieId = movie.Id });
                            original.Title = movie.Title;
                            original.Director = movie.Director;
                            original.TicketPrice = movie.TicketPrice;
                            original.ReleaseDate = movie.ReleaseDate;
                            UoW.MovieRepository.SaveChanges();
                            return new ObjectResult(original);
                        }
                        else
                        {
                            return new BadRequestObjectResult("NOTLOGGEDUSER");
                        }*/

                    }
                }
            }
            catch (System.Exception ex)
            {
                // log?
                var x = ex.ToString();
            }
            return new BadRequestObjectResult(ModelState);

        }
Example #2
0
        private static async Task CreateSampleData(IServiceProvider applicationServices)
        {
            using (var dbContext = applicationServices.GetService<ApplicationContext>())
            {
                try
                {
                    // dummy movies
                    var movies = new List<Movie>
                        {
                            new Movie {Title="Star Wars", Director="Lucas", TicketPrice=5, ReleaseDate=DateTime.Today},
                            new Movie {Title="King Kong", Director="Jackson", TicketPrice=5, ReleaseDate=DateTime.Today},
                            new Movie {Title="Memento", Director="Nolan", TicketPrice=5, ReleaseDate=DateTime.Today}
                        };
                    Movie withActor = new Movie() { Id = Guid.NewGuid(), Title = "Up in smoke", Director = "Zapata", TicketPrice = 5, ReleaseDate = DateTime.Today };
                    Actor actor = new Actor() { MovieId = withActor.Id, Name = "Me" };
                    
                    movies.Add(withActor);
                    movies.ForEach(m => dbContext.Movies.Add(m));

                    dbContext.Add(actor);

                    dbContext.SaveChanges();
                    var rolesManager = applicationServices.GetService<RoleManager<ApplicationRole>>();
                    var regularUser = new ApplicationRole()
                    {
                        Name = "RegularUser",
                        NormalizedName = "Regular User",
                        Disabled = false
                    };
                    await rolesManager.CreateAsync(regularUser);


                    var superAdmin = new ApplicationRole()
                    {
                        Name = "SuperAdmin",
                        NormalizedName = "Super Admin",
                        Disabled = false
                    };
                    await rolesManager.CreateAsync(superAdmin);
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("UserMenuDisplay", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("AdminMenuDisplay", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("SuperAdminMenuDisplay", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("MovieCreate", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("MovieEdit", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("MovieDelete", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("MovieList", "1"));

                    

                    var admin = new ApplicationRole()
                    {
                        Name = "Admin",
                        NormalizedName = "Admininstrator",
                        Disabled = false
                    };
                    await rolesManager.CreateAsync(admin);
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("UserMenuDisplay", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("AdminMenuDisplay", "1"));
                    await rolesManager.AddClaimAsync(superAdmin, new Claim("MovieList", "1"));

                    var userManager = applicationServices.GetService<UserManager<ApplicationUser>>();
                    // add super user
                    var nicolas = new ApplicationUser()
                    {
                        UserName = "******",
                        Email = "*****@*****.**"
                    };
                    var result = await userManager.CreateAsync(nicolas, "P@55w0rd");
                    
                    await userManager.AddClaimAsync(nicolas, new Claim("FullControl", "1"));
                    await userManager.AddToRoleAsync(nicolas, "SuperAdmin");
                    // add normal user
                    var marina = new ApplicationUser()
                    {
                        UserName = "******",
                        Email = "*****@*****.**"
                    };
                    await userManager.CreateAsync(marina, "P@55w0rd");
                    await userManager.AddToRoleAsync(marina, "Admin");
                }
                catch (Exception ex)
                {
                    //log?
                    var excLog = ex.ToString();
                }
            }
        }