Exemple #1
0
        public async Task <Bookmark> Add(Status status)
        {
            var entity = new Bookmark
            {
                Status     = status.ToCompleteString(),
                LastUpdate = DateTime.Now,
            };

            var newBookmark = await _context.Bookmarks.AddAsync(entity);

            await _context.SaveChangesAsync();

            return(newBookmark.Entity);
        }
        public async Task AddTvShowsWithPersons(List <TvShow> tvShows)
        {
            await _context.BulkInsertAsync(tvShows);

            await _context.SaveChangesAsync();

            foreach (var tvShow in tvShows.Where(tvShow => tvShow.Persons != null && tvShow.Persons.Any()))
            {
                tvShow.Persons.ForEach(p => p.TvShowIdFk = tvShow.Id);
                await _context.BulkInsertAsync(tvShow.Persons.OrderByDescending(p => p.Birthday).ToList());
            }

            await _context.SaveChangesAsync();
        }
Exemple #3
0
        private async Task SaveActors(List <Actor> actors)
        {
            var actorIds       = actors.Select(a => a.TvMazeId);
            var existingActors = await _dbContext.Actors.Where(e => actorIds.Contains(e.TvMazeId)).Select(a => a.TvMazeId).ToListAsync();

            var newActors = actors
                            .Where(e => !existingActors.Contains(e.TvMazeId))
                            .Select(actor => new Actor()
            {
                Birthday = actor.Birthday,
                TvMazeId = actor.TvMazeId,
                Name     = actor.Name
            });

            foreach (var actor in newActors)
            {
                _dbContext.Actors.Add(actor);
            }

            await _dbContext.SaveChangesAsync();
        }
        /// <inheritdoc />
        public async Task <Person> InsertIfNotExistsAsync(Person person)
        {
            var existsingPerson = await GetPersonAsync(person.ID);

            if (existsingPerson == null)
            {
                _dbContext.Persons.Add(person);
                try
                {
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateException ex) when(ex.IsSqlUniqueConstraint())
                {
                    // Ignore unique constraint exceptions (e.g. PK)
                }
            }
            return(person);
        }
Exemple #5
0
        /// <inheritdoc />
        public async Task <Show> InsertIfNotExistsAsync(Show show)
        {
            var existsingShow = await GetShowAsync(show.ID);

            if (existsingShow == null)
            {
                _dbContext.Shows.Add(show);
                try
                {
                    await _dbContext.SaveChangesAsync();
                }
                catch (DbUpdateException ex) when(ex.IsSqlUniqueConstraint())
                {
                    // Ignore unique constraint exceptions (e.g. PK)S
                }
            }
            return(show);
        }
        public async Task <string> StoreCastShow(int idShow)
        {
            try
            {
                // Get show from database using idShow
                var show = await _context.Shows.FindAsync(idShow);

                List <CastShow> listNewCastShow = new List <CastShow>();

                // if show == null => store new show using httpProvider
                if (show == null)
                {
                    using (var transaction = _context.Database.BeginTransaction())
                    {
                        CastShowPublicResponse response = await CastShowHttpProvider.GetPublicShowCast(idShow);

                        if (response != null)
                        {
                            var newShow = new Show()
                            {
                                Id   = response.Id,
                                Name = response.Name
                            };

                            foreach (var auxCast in response._embedded.cast)
                            {
                                var newCast = new Cast()
                                {
                                    Id       = auxCast.person.Id,
                                    Name     = auxCast.person.Name,
                                    Birthday = auxCast.person.Birthday
                                };

                                var storedCastShow = listNewCastShow.Find(x => x.CastId == auxCast.person.Id && x.ShowId == response.Id);
                                if (storedCastShow == null)
                                {
                                    var castShow = new CastShow {
                                        Cast = newCast, Show = newShow
                                    };
                                    await _context.AddRangeAsync(castShow);

                                    listNewCastShow.Add(castShow);
                                }
                            }

                            await _context.SaveChangesAsync();

                            // Commit transaction if all commands succeed, transaction will auto-rollback
                            // when disposed if either commands fails
                            transaction.Commit();
                        }
                        else
                        {
                            return("Show not found");
                        }
                    }
                }
                else
                {
                    return("Show is already stored. Try update");
                }
            }
            catch (Exception ex)
            {
                return("Error");
            }


            return("Success");
        }