Ejemplo n.º 1
0
        public async void CanUpdateSpoiler()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("CanUpdateSpoiler")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Spoiler spoiler = new Spoiler();
                spoiler.SpoilerText = "Spoilers!";

                context.Spoilers.Add(spoiler);
                await context.SaveChangesAsync();

                spoiler.SpoilerText = "NoSpoilers!";

                context.Spoilers.Update(spoiler);
                await context.SaveChangesAsync();

                var spoilerText = await context.Spoilers.FirstOrDefaultAsync(x => x.SpoilerText == spoiler.SpoilerText);

                Assert.Equal("NoSpoilers!", spoilerText.SpoilerText);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes the spoiler.
        /// </summary>
        /// <param name="id">The identifier.</param>
        public async void RemoveSpoiler(int id)
        {
            var spoiler = await _context.Spoilers.FindAsync(id);

            _context.Spoilers.Remove(spoiler);
            await _context.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutSpoiler([FromRoute] int id, [FromBody] Spoiler spoiler)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != spoiler.ID)
            {
                return(BadRequest());
            }

            await _spoilerContext.UpdateSpoiler(id, spoiler);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SpoilerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async void CanDeleteMovie()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("CanDeleteMovie")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Movie movie = new Movie();
                movie.ID    = "i231231";
                movie.Title = "My Movie";

                context.Movies.Add(movie);

                context.SaveChanges();

                context.Movies.Remove(movie);
                await context.SaveChangesAsync();

                var moviesText = await context.Movies.ToListAsync();

                Assert.DoesNotContain(movie, moviesText);
            }
        }
Ejemplo n.º 5
0
        public async void CanUpdateMovie()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("CanUpdateMovie")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Movie movie = new Movie();
                movie.ID    = "i231231";
                movie.Title = "My Movie";
                context.Movies.Add(movie);

                context.SaveChanges();

                movie.Title = "NO TITLE";

                context.Movies.Update(movie);
                await context.SaveChangesAsync();

                var movieTitle = await context.Movies.FirstOrDefaultAsync(x => x.ID == movie.ID);

                Assert.Equal("NO TITLE", movieTitle.Title);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Attempts to Get Movie from Custom API Database; if not found, Creates Movie from OMDB
        /// </summary>
        /// <param name="imdbId">IMDBID</param>
        /// <param name="loadSpoilers">Spoilers associated with Movie</param>
        /// <returns>Returns a Movie</returns>
        public async Task <Movie> GetMovieOrCreate(string imdbId, bool loadSpoilers = false)
        {
            var movie = await _context.Movies.FirstOrDefaultAsync(m => m.ID == imdbId);

            // If movie isn't in our database, try to get it from external api
            if (movie == null)
            {
                MovieDescription mDescripton = await GetMovieExternal(imdbId);

                if (mDescripton != null)
                {
                    movie = new Movie {
                        ID = mDescripton.ImdbID, Title = mDescripton.Title, Genre = mDescripton.Genre, Plot = mDescripton.Plot, Year = mDescripton.Year, Poster = mDescripton.Poster
                    };
                    _context.Movies.Add(movie);
                    await _context.SaveChangesAsync();
                }
            }

            if (movie != null && loadSpoilers)
            {
                movie.Spoilers = await _context.Spoilers.Where(x => x.MovieID == movie.ID).ToListAsync();
            }
            return(movie);
        }
Ejemplo n.º 7
0
        public async void CanDeleteSpoiler()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("SpoilerDelete")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Spoiler spoiler = new Spoiler();
                spoiler.SpoilerText = "Spoilers!";

                context.Spoilers.Add(spoiler);
                await context.SaveChangesAsync();

                context.Spoilers.Remove(spoiler);
                await context.SaveChangesAsync();

                var spoilerText = await context.Spoilers.ToListAsync();

                Assert.DoesNotContain(spoiler, spoilerText);
            }
        }
Ejemplo n.º 8
0
        public async void CanCreateMovie()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("CanCreateMovie")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Movie movie = new Movie();
                movie.ID = "i231231";
                context.Movies.Add(movie);
                await context.SaveChangesAsync();

                Assert.True(await context.Movies.AnyAsync(x => x.ID == movie.ID));
            }
        }
Ejemplo n.º 9
0
        public async void CanCreateSpoiler()
        {
            DbContextOptions <SpoiltContext> options =
                new DbContextOptionsBuilder <SpoiltContext>()
                .UseInMemoryDatabase("GetSpoilerText")
                .Options;

            using (SpoiltContext context = new SpoiltContext(options))
            {
                Spoiler spoiler = new Spoiler();
                spoiler.SpoilerText = "Spoilers!";

                context.Spoilers.Add(spoiler);
                await context.SaveChangesAsync();

                Assert.True(await context.Spoilers.AnyAsync(x => x.ID == spoiler.ID));
            }
        }