public async Task Add(CarSubgroupAddDto carSubgroupDto)
        {
            var carSubgroupRepository = DataContextManager.CreateRepository <ICarSubgroupRepository>();

            var carSubgroup = await carSubgroupRepository.GetByName(carSubgroupDto.Name);

            if (carSubgroup != null)
            {
                throw new AddingException();
            }

            var carRepository = DataContextManager.CreateRepository <ICarRepository>();
            var cars          = await carRepository.GetCarsByIds(carSubgroupDto.CarIds);

            var newCarSubgroup = new CarSubgroup
            {
                Name              = carSubgroupDto.Name,
                CreateDate        = DateTime.Now,
                CarInCarSubgroups = cars.Select(car => new CarInCarSubgroup
                {
                    Car = car
                })
                                    .ToList()
            };

            await carSubgroupRepository.Add(newCarSubgroup);
        }
        public async Task Add(CarSubgroup carSubgroup)
        {
            await Context.Set <CarSubgroup>()
            .AddAsync(carSubgroup);

            await Context.SaveChangesAsync();
        }
        private void DeleteCars(CarSubgroup carSubgroup, IEnumerable <Car> cars)
        {
            var carIdsToDelete = new List <int>();

            foreach (var carInCarSubgroup in carSubgroup.CarInCarSubgroups)
            {
                if (!cars.Any(x => x.Id == carInCarSubgroup.CarId))
                {
                    carIdsToDelete.Add(carInCarSubgroup.CarId);
                }
            }

            carSubgroup.CarInCarSubgroups.RemoveAll(x => carIdsToDelete.Contains(x.CarId));
        }
 private void AddCars(CarSubgroup carSubgroup, IEnumerable <Car> cars)
 {
     foreach (var car in cars)
     {
         if (!carSubgroup.CarInCarSubgroups.Any(x => x.CarId == car.Id))
         {
             carSubgroup.CarInCarSubgroups.Add(new CarInCarSubgroup
             {
                 CarId         = car.Id,
                 CarSubgroupId = carSubgroup.Id
             });
         }
     }
 }
 public async Task Update(CarSubgroup carSubgroup)
 {
     Context.Entry <CarSubgroup>(carSubgroup).State = EntityState.Modified;
     await Context.SaveChangesAsync();
 }