예제 #1
0
        public IActionResult AddToFavorite(int id)
        {
            Favorites favorite = new Favorites
            {
                ApiId  = id,
                UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value
            };

            if (_context.Favorites.Where(x => (x.ApiId == id) && (x.UserId == User.FindFirst(ClaimTypes.NameIdentifier).Value)).ToList().Count > 0)
            {
                return(RedirectToAction("Favorites"));
            }
            if (ModelState.IsValid)
            {
                _context.Favorites.Add(favorite);
                _context.SaveChanges();
            }
            return(RedirectToAction("Favorites"));
        }
예제 #2
0
        private static void DoSaveFilm(Film film)
        {
            if (film == null)
            {
                return;
            }
            // Создать объект контекста
            FilmsDbContext context = new FilmsDbContext();

            // Вставить данные в таблицу Films с помощью LINQ
            context.Films.Add(film);

            // Сохранить изменения в БД
            context.SaveChanges();
        }
예제 #3
0
        public IActionResult CreateFilm()
        {
            var newFilm = new Film {
                Title = "MIB", Description = "Some B Shit"
            };
            var newFilm2 = new Film {
                Title = "MIB2", Description = "Some B Shit2"
            };

            _db.Films.Add(newFilm);
            _db.Films.Add(newFilm2);

            _db.SaveChanges();

            return(Ok());
        }
예제 #4
0
 public bool RemoveFilm(int id)
 {
     using (var ctx = new FilmsDbContext())
     {
         var model = ctx.Films.FirstOrDefault(f => f.Id == id);
         if (model != null)
         {
             ctx.Films.Remove(model);
             ctx.SaveChanges();
         }
         else
         {
             throw new Exception($"В базе нет ыильма с ID = {id}");
         }
     }
     return(true);
 }
예제 #5
0
        public bool SaveFilm(FilmViewModel film)
        {
            if (film == null)
            {
                throw new NullReferenceException("film");
            }

            using (var ctx = new FilmsDbContext())
            {
                var model = film.Id == 0
                    ? new Film()
                    : ctx.Films
                            .Include("Actors")
                            .Include("Ganre")
                            .FirstOrDefault(f => f.Id == film.Id);

                model.Name        = film.Name;
                model.Producer    = film.Producer;
                model.Score       = film.Score;
                model.GanreId     = SaveGanre(film.Ganre);
                model.Description = film.Description;
                model.Budget      = film.Budget;
                var actorsIds     = CompareAndSaveActors(film.Actors);
                var existActorsId = model.Actors.Select(a => a.Id).ToList();
                var actorsToAdd   = actorsIds.Except(existActorsId).ToList();
                var actorsToDel   = existActorsId.Except(actorsIds).ToList();
                var addActors     = ctx.Actors.Where(a => actorsToAdd.Contains(a.Id)).ToList();
                model.Actors.RemoveAll(a => actorsToDel.Contains(a.Id));
                model.Actors.AddRange(addActors);

                if (film.Id == 0)
                {
                    ctx.Films.Add(model);
                }
                ctx.SaveChanges();
            }
            return(true);
        }
예제 #6
0
        public List <int> CompareAndSaveActors(List <string> actorNames)
        {
            if (actorNames == null)
            {
                throw new NullReferenceException("actorNames");
            }
            List <int> result = new List <int>();

            using (var ctx = new FilmsDbContext())
            {
                var exist = ctx.Actors.Where(a => actorNames.Contains(a.Name));
                result.AddRange(exist.Select(a => a.Id).ToList());
                var existNames = exist.Select(a => a.Name).ToList();
                var newNames   = actorNames.Except(existNames).ToList();
                var newActors  = newNames.Select(n => new Actor()
                {
                    Name = n
                }).ToList();
                newActors = ctx.Actors.AddRange(newActors).ToList();
                ctx.SaveChanges();
                result.AddRange(newActors.Select(a => a.Id).ToList());
            }
            return(result);
        }