Exemple #1
0
        public void Setup()
        {
            config = Options.Create(new AppSettings
            {
                Secret = "dsadggggggggghhhhhhh"
            });

            registerValidator = new RegisterValidator();
            roleValidator     = new RoleValidator();
            user = new RegisterPostModel();

            movie = new MoviePostModel
            {
                Title       = "movie1",
                Description = "description",
                Duration    = 120,
                Year        = 2018,
                Director    = "Alina",
                Date        = DateTime.Now,
                Rating      = 9,
                Watched     = "yes"
            };

            user = new RegisterPostModel
            {
                FirstName = "alina",
                LastName  = "demian",
                Username  = "******",
                Email     = "*****@*****.**",
                Password  = "******"
            };
        }
        public MoviePostModel GetById(int id)
        {
            var res = Context.Movies
                      .Include(e => e.Comments)
                      .FirstOrDefault(f => f.Id == id);

            return(MoviePostModel.FromMovie(res));
        }
Exemple #3
0
        //acum mutam logica din Controller pe Service.
        //Nu il eliminam dar Controller-ul va apela Service si nu va mai apela UI-ul Service-ul

        public Movie Create(MoviePostModel movie)
        {
            Movie toAdd = MoviePostModel.ToMovie(movie);

            context.Movies.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
Exemple #4
0
        public Movie Create(MoviePostModel movieDTO)
        {
            Movie addMovie = MoviePostModel.ToMovie(movieDTO);

            DbContext.Movies.Add(addMovie);
            DbContext.SaveChanges();
            return(addMovie);
        }
        //acum mutam logica din Controller pe Service.
        //Nu il eliminam dar Controller-ul va apela Service si nu va mai apela UI-ul Service-ul

        public Movie Create(MoviePostModel movie, User addedBy)
        {
            Movie toAdd = MoviePostModel.ToMovie(movie);

            toAdd.Owner = addedBy;
            context.Movies.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
Exemple #6
0
        public Movie Create(MoviePostModel movie, User addedBy)
        {
            Movie toAdd = MoviePostModel.ToMovie(movie);

            toAdd.Owner = addedBy;      //adaugam persoana care a adaugat acest Movie movie
            context.Movies.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
Exemple #7
0
        public IActionResult Post([FromBody] MoviePostModel movie)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            movieService.Create(movie);
            return(Ok(movie));
        }
        public IActionResult Put(int id, [FromBody] MoviePostModel movie)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            service.Upsert(id, movie);
            return(Ok());
        }
Exemple #9
0
        public void Post([FromBody] MoviePostModel movie)
        {
            //if (!ModelState.IsValid)
            //{

            //}
            //context.Movies.Add(movie);
            //context.SaveChanges();
            movieService.Create(movie);
        }
        public Movie Create(MoviePostModel movie, User addedBy)
        {
            Movie toAdd = MoviePostModel.ToMovie(movie);

            movie.DateClosed = null;
            movie.DateAdded  = DateTime.Now;
            toAdd.Owner      = addedBy;
            context.Movies.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
Exemple #11
0
        public void Post([FromBody] MoviePostModel movie)
        {
            User addedBy = usersService.GetCurentUser(HttpContext);

            ////if(addedBy.UserRole == UserRole.UserManager)
            // //{
            // //    return Forbid();      //trebuie returnat tipul IActionResult
            // //}

            moviesService.Create(movie, addedBy);
        }
        public void Setup()
        {
            config = Options.Create(new AppSettings
            {
                Secret = "dsadggggggggghhhhhhh"
            });

            registerValidator = new RegisterValidator();
            roleValidator     = new RoleValidator();
            user = new RegisterPostModel();

            movie = new MoviePostModel
            {
                Title       = "movie2",
                Description = "description",
                Duration    = 120,
                Year        = 2018,
                Director    = "Alina",
                Date        = DateTime.Now,
                Rating      = 9,
                Watched     = "yes",
                Comments    = new List <Comment>()
                {
                    new Comment
                    {
                        Important = true,
                        Text      = "asd",
                        Owner     = null
                    },
                    new Comment
                    {
                        Important = false,
                        Text      = "abc",
                        Owner     = null
                    },
                    new Comment
                    {
                        Important = true,
                        Text      = "bda",
                        Owner     = null
                    }
                }
            };

            user = new RegisterPostModel
            {
                FirstName = "alina",
                LastName  = "demian",
                Username  = "******",
                Email     = "*****@*****.**",
                Password  = "******"
            };
        }
        /// <summary>
        /// Create a new Movie, using MoviePostModel
        /// </summary>
        /// <param name="movie">New Movie object</param>
        /// <returns></returns>
        public Movie Create(MoviePostModel movie)
        {
            Movie convertedMovie = MoviePostModel.ToMovie(movie);

            if (convertedMovie == null)
            {
                return(null);
            }

            Context.Movies.Add(convertedMovie);
            Context.SaveChanges();
            return(convertedMovie);
        }
        public void GetCommentsFilteredByValueShouldReturnAListOfFilteredComments()
        {
            var options = new DbContextOptionsBuilder <DataDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetCommentsFilteredByValueShouldReturnAListOfFilteredComments))
                          .Options;

            using (var context = new DataDbContext(options))
            {
                var commentsService = new CommentService(context);
                var movieService    = new MovieService(context);

                var MovieWithComments = new MoviePostModel()
                {
                    Title             = "Nu Stiu",
                    Description       = "SF",
                    MovieGenre        = "Comedy",
                    DurationInMinutes = 120,
                    ReleseYear        = 2018,
                    Director          = "Alexandru",
                    DateAdded         = new DateTime(03 / 06 / 2019),
                    Rating            = 3,
                    WasWatched        = "YES",
                    Comments          = new List <CommentGetModel>()
                    {
                        new CommentGetModel()
                        {
                            Text      = "A test comment 1 filtered",
                            Important = false
                        },
                        new CommentGetModel()
                        {
                            Text      = "A test comment 2 filtered",
                            Important = true
                        },
                        new CommentGetModel()
                        {
                            Text      = "A test comment 3",
                            Important = false
                        }
                    }
                };
                movieService.Create(MovieWithComments);

                List <CommentGetModel> comments = commentsService.GetAllComments("filtered");
                int numberOfComments            = comments.Count;

                Assert.IsNotNull(comments);
                Assert.AreEqual(2, numberOfComments);
            }
        }
        /// <summary>
        /// Update or Add
        /// </summary>
        /// <param name="id">Id for update</param>
        /// <param name="movies">New Movie object we want to add</param>
        /// <returns></returns>
        public Movie Upsert(int id, MoviePostModel movies)
        {
            var existing = Context.Movies.AsNoTracking().FirstOrDefault(c => c.Id == id);

            if (existing == null)
            {
                var movie = Context.Movies.Add(MoviePostModel.ToMovie(movies));
                Context.SaveChanges();
                return(movie.Entity);
            }
            var res = MoviePostModel.ToUpdateMovie(movies, existing);

            Context.SaveChanges();

            return(res);
        }
        public Movie Upsert(int id, MoviePostModel movie)
        {
            var existing = context.Movies.AsNoTracking().FirstOrDefault(f => f.Id == id);

            if (existing == null)
            {
                Movie toAdd = MoviePostModel.ToMovie(movie);
                context.Movies.Add(toAdd);
                context.SaveChanges();
                return(toAdd);
            }
            Movie toUpdate = MoviePostModel.ToMovie(movie);

            toUpdate.Id = id;
            context.Movies.Update(toUpdate);
            context.SaveChanges();
            return(toUpdate);
        }
Exemple #17
0
        public void Post([FromBody] MoviePostModel movie)
        {
            //if (!ModelState.IsValid)
            //{

            //}
            //context.Movies.Add(movie);
            //context.SaveChanges();
            // NEXT TIME: folosirea permisiunilor.
            User addedBy = usersService.GetCurentUser(HttpContext);

            //if (addedBy.UserRole == UserRole.UserManager)
            //{
            //    return Forbid();
            //}
            movieService.Create(movie, addedBy);
            // movieService.Create(movie);
        }
Exemple #18
0
        public void UpsertShouldModifyTheGivenMovie()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyTheGivenMovie))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService = new MovieService(context);
                var added        = new MoviePostModel()

                {
                    Title        = "Titanic",
                    Duration     = 100,
                    Genre        = "Thriller",
                    WatchedState = "Yes",
                    DateAdded    = DateTime.Parse("2019-06-15T00:00:00"),
                    DateClosed   = null,

                    Comments = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "A nice movie",
                        }
                    },
                };

                var toAdd  = movieService.Create(added, null);
                var update = new MoviePostModel()
                {
                    Title = "Updated"
                };

                var toUp         = movieService.Create(update, null);
                var updateResult = movieService.Upsert(toUp.Id, toUp);


                Assert.IsNotNull(updateResult);
                Assert.AreEqual(toUp.Title, updateResult.Title);
            }
        }
Exemple #19
0
        public void GetByIdShouldReturnTaskWithCorrectId()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnTaskWithCorrectId))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService = new MovieService(context);
                var added        = new MoviePostModel()

                {
                    Title        = "Titanic",
                    Duration     = 100,
                    Genre        = "Thriller",
                    WatchedState = "Yes",
                    DateAdded    = DateTime.Parse("2019-06-15T00:00:00"),
                    DateClosed   = null,

                    Comments = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "A nice movie",
                        }
                    },
                };

                var current  = movieService.Create(added, null);
                var expected = movieService.GetById(current.Id);

                Assert.IsNotNull(expected);
                Assert.AreEqual(expected.Title, current.Title);
                Assert.AreEqual(expected.Duration, current.Duration);
                Assert.AreEqual(expected.WatchedState, current.WatchedState);
                Assert.AreEqual(expected.Genre, current.Genre);
                Assert.AreEqual(expected.Id, current.Id);
            }
        }
Exemple #20
0
        public void DeleteMovieWithCommentsShouldDeleteMoviesAndComments()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(DeleteMovieWithCommentsShouldDeleteMoviesAndComments))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var moviesService = new MovieService(context);

                var expected = new MoviePostModel()
                {
                    Title        = "Titanic",
                    Duration     = 100,
                    Genre        = "Thriller",
                    WatchedState = "Yes",
                    DateAdded    = DateTime.Parse("2019-06-15T00:00:00"),
                    DateClosed   = null,

                    Comments = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "A nice movie",
                        }
                    },
                };

                var actual               = moviesService.Create(expected, null);
                var afterDelete          = moviesService.Delete(actual.Id);
                int numberOfCommentsInDb = context.Comments.CountAsync().Result;
                var resultExpense        = context.Movies.Find(actual.Id);

                Assert.IsNotNull(afterDelete);
                Assert.IsNull(resultExpense);
                Assert.AreEqual(0, numberOfCommentsInDb);
            }
        }
Exemple #21
0
        public void GetAllShouldReturnCorrectNumberOfPagesForComments()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(GetAllShouldReturnCorrectNumberOfPagesForComments))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService   = new MovieService(context);
                var commentService = new CommentService(context);
                var added          = new MoviePostModel()

                {
                    Title        = "Tianic",
                    Duration     = 100,
                    Genre        = "Thriller",
                    WatchedState = "Yes",
                    DateAdded    = DateTime.Parse("2019-06-15T00:00:00"),
                    DateClosed   = null,

                    Comments = new List <Comment>()
                    {
                        new Comment
                        {
                            Important = true,
                            Text      = "A nice movie",
                            Owner     = null
                        }
                    },
                };

                var current     = movieService.Create(added, null);
                var allComments = commentService.GetAll(string.Empty, 1);
                //var allComments = commentService.GetAll(string.Empty, 1);

                Assert.AreEqual(1, allComments.NumberOfPages);
            }
        }
Exemple #22
0
        public void CreateShouldReturnTheCreatedMovie()
        {
            var options = new DbContextOptionsBuilder <MoviesDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(CreateShouldReturnTheCreatedMovie))
                          .Options;

            using (var context = new MoviesDbContext(options))
            {
                var movieService = new MovieService(context);
                var userService  = new UserService(context, config, registerValidator, roleValidator);

                userService.Register(user);

                var addedUser = context.Users.Last();

                movieService.Create(movie, addedUser);

                MoviePostModel movie2 = new MoviePostModel
                {
                    Title       = "movie2",
                    Description = "description",
                    Duration    = 120,
                    Year        = 2018,
                    Director    = "Alina",
                    Date        = DateTime.Now,
                    Rating      = 9,
                    Watched     = "yes"
                };

                movieService.Create(movie2, addedUser);

                int movies = movieService.GetAll(1).Entries.Count();

                Assert.AreEqual(2, movies);
            }
        }
        public IActionResult Put(int id, [FromBody] MoviePostModel movie)
        {
            var result = movieService.Upsert(id, movie);

            return(Ok(result));
        }
        public void Post([FromBody] MoviePostModel movie)
        {
            User addedBy = usersService.GetCurrentUser(HttpContext);

            movieService.Create(movie, addedBy);
        }
Exemple #25
0
 public void Post([FromBody] MoviePostModel movie)
 {
     service.Create(movie);
 }