public ActionResult Add(CarBindingModel model)
        {
            if (model != null && this.ModelState.IsValid)
            {
                this.LoadBrands();
                this.LoadImporters();

                var car = new Car()
                {
                    BrandId = model.BrandId,
                    YearManufactured = model.YearManufactured,
                    HorsePower = model.HorsePower,
                    ImporterId = model.ImporterId,
                    Description = model.Description
                };

                this.Data.Cars.Add(car);

                this.Data.SaveChanges();
                this.AddNotification("Добавихте нова кола!", NotificationType.SUCCESS);

                return this.RedirectToAction("Index", "Home");
            }

            this.LoadBrands();
            this.LoadImporters();

            return this.View(model);
        }
示例#2
0
 public void Delete(CarBindingModel model)
 {
     using (var context = new KorytoDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 context.CarDetails.RemoveRange(context.CarDetails.Where(rec =>
                                                                         rec.CarId == model.Id));
                 Car element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);
                 if (element != null)
                 {
                     context.Cars.Remove(element);
                     context.SaveChanges();
                 }
                 else
                 {
                     throw new Exception("Элемент не найден");
                 }
                 transaction.Commit();
             }
             catch (Exception)
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
示例#3
0
        public IActionResult Add(CarBindingModel c)
        {
            using (this.context)
            {
                if (!ModelState.IsValid)
                {
                    return(View(c));
                }

                Car car = new Car
                {
                    Make              = c.Make,
                    Model             = c.Model,
                    TravelledDistance = c.TravelledDistance
                };

                Logger logger = new Logger
                {
                    ModifiedTable = ModiefiedTable,
                    User          = User.Identity.Name,
                    Operation     = "Add",
                    Time          = DateTime.Now
                };

                this.context.Loggers.Add(logger);
                this.context.Cars.Add(car);
                this.context.SaveChanges();

                return(RedirectToAction(nameof(Index), new { make = car.Make }));
            }
        }
示例#4
0
 private Car CreateModel(CarBindingModel model, Car Car)
 {
     Car.Model    = model.Model;
     Car.Color    = model.Color;
     Car.Price    = model.Price;
     Car.BranchId = model.BranchId;
     return(Car);
 }
示例#5
0
 public void Insert(CarBindingModel model)
 {
     using (var context = new AutoshowDbContext())
     {
         context.Cars.Add(CreateModel(model, new Car()));
         context.SaveChanges();
     }
 }
示例#6
0
        public void AddCar(CarBindingModel model)
        {
            var car = new Car
            {
                Model      = model.Model,
                BrandId    = _hashidsService.Decode(model.BrandNumber),
                BodyTypeId = _hashidsService.Decode(model.BodyTypeNumber)
            };

            _carRepository.Add(car);
        }
示例#7
0
        public void Delete(CarBindingModel model)
        {
            var element = CarStorage.GetElement(new CarBindingModel {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            CarStorage.Delete(model);
        }
        public void AddElement(CarBindingModel model)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    var car = context.Cars.FirstOrDefault(record => record.CarName == model.CarName);

                    if (car != null)
                    {
                        throw new Exception("Такое авто уже существует.");
                    }
                    else
                    {
                        car = new Car
                        {
                            CarName = model.CarName,
                            Price   = model.Price,
                            Year    = model.Year
                        };
                    }

                    context.Cars.Add(car);
                    context.SaveChanges();

                    var duplicates = model.CarDetails
                                     .GroupBy(record => record.DetailId)
                                     .Select(record => new
                    {
                        detailId = record.Key,
                        amount   = record.Sum(rec => rec.Amount)
                    });

                    foreach (var duplicate in duplicates)
                    {
                        context.CarDetails.Add(new CarDetail
                        {
                            CarId    = car.Id,
                            DetailId = duplicate.detailId,
                            Amount   = duplicate.amount
                        });
                        context.SaveChanges();
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
示例#9
0
        public async Task <IActionResult> Add(CarBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            var car = Mapper.Map <Car>(model);

            await this.carService.Add(car);

            return(RedirectToAction("All", "Car"));
        }
示例#10
0
 public List <CarViewModel> Read(CarBindingModel model)
 {
     if (model == null)
     {
         return(CarStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <CarViewModel> {
             CarStorage.GetElement(model)
         });
     }
     return(CarStorage.GetFilteredList(model));
 }
示例#11
0
        public void Update(CarBindingModel model)
        {
            using (var context = new AutoshowDbContext())
            {
                var Car = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);

                if (Car == null)
                {
                    throw new Exception("Сотрудник не найден");
                }
                CreateModel(model, Car);
                context.SaveChanges();
            }
        }
示例#12
0
        // GET: Elements/Edit/5
        public ActionResult Edit(int id)
        {
            var viewModel    = Service.GetElement(id);
            var bindingModel = new CarBindingModel
            {
                Id       = id,
                ClientId = Globals.AuthClient.Id,
                VIN      = viewModel.VIN,
                Brand    = viewModel.Brand,
                Model    = viewModel.Model,
                Mileage  = viewModel.Mileage
            };

            return(View(bindingModel));
        }
示例#13
0
        public void Delete(CarBindingModel model)
        {
            using (var context = new AutoshowDbContext())
            {
                Car Car = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);

                if (Car != null)
                {
                    context.Cars.Remove(Car);
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Сотрудник не найден");
                }
            }
        }
示例#14
0
        public void CreateOrUpdate(CarBindingModel model)
        {
            var element = CarStorage.GetElement(new CarBindingModel {
                Model = model.Model
            });

            if (element != null && element.Id != model.Id)
            {
                throw new Exception("Уже есть автомобиль такой модели");
            }
            if (model.Id.HasValue)
            {
                CarStorage.Update(model);
            }
            else
            {
                CarStorage.Insert(model);
            }
        }
示例#15
0
        public void AddElement(CarBindingModel model)
        {
            var element = context.Cars.FirstOrDefault(rec => rec.VIN == model.VIN);

            if (element != null)
            {
                throw new Exception("Уже есть автомобиль с данным VIN-номером");
            }

            context.Cars.Add(new Car
            {
                VIN      = model.VIN,
                Brand    = model.Brand,
                ClientId = model.ClientId,
                Mileage  = model.Mileage,
                Model    = model.Model
            });
            context.SaveChanges();
        }
示例#16
0
        public List <CarViewModel> GetFilteredList(CarBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }

            using (var context = new AutoshowDbContext())
            {
                return(context.Cars
                       .Where(rec => rec.Model.Contains(model.Model))
                       .Select(rec => new CarViewModel
                {
                    Id = rec.Id,
                    Model = rec.Model,
                    Color = rec.Color,
                    Price = rec.Price
                }).ToList());
            }
        }
示例#17
0
 public List <CarViewModel> Read(CarBindingModel model)
 {
     using (var context = new KorytoDatabase())
     {
         return(context.Cars
                .Where(rec => model == null || rec.Id == model.Id)
                .ToList()
                .Select(rec => new CarViewModel
         {
             Id = rec.Id,
             CarName = rec.CarName,
             Price = rec.Price,
             FullPrice = rec.FullPrice,
             Year = rec.Year,
             CarDetails = context.CarDetails
                          .Include(recPC => recPC.Detail)
                          .Where(recPC => recPC.CarId == rec.Id)
                          .ToDictionary(recPC => recPC.DetailId, recPC =>
                                        (recPC.Detail?.DetailName, recPC.Count, recPC.Sum))
         })
示例#18
0
        public CarViewModel GetElement(CarBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }

            using (var context = new AutoshowDbContext())
            {
                var Car = context.Cars
                          .FirstOrDefault(rec => rec.Id == model.Id);
                return(Car != null ?
                       new CarViewModel
                {
                    Id = Car.Id,
                    Model = Car.Model,
                    Color = Car.Color,
                    Price = Car.Price
                } : null);
            }
        }
示例#19
0
        public void UpdateElement(CarBindingModel model)
        {
            var element = context.Cars.FirstOrDefault(rec => rec.VIN == model.VIN && rec.Id != model.Id);

            if (element != null)
            {
                throw new Exception("Уже есть автомобиль с таким VIN-номером");
            }

            element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }

            element.VIN     = model.VIN;
            element.Model   = model.Model;
            element.Brand   = model.Brand;
            element.Mileage = model.Mileage;
            context.SaveChanges();
        }
        public void UpdateElement(CarBindingModel model)
        {
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    var car = context.Cars
                              .FirstOrDefault(record => record.CarName == model.CarName && record.Id != model.Id);

                    if (car != null)
                    {
                        throw new Exception("Уже есть автомобиль с таким названием");
                    }

                    car = context.Cars
                          .FirstOrDefault(record => record.Id == model.Id);

                    if (car == null)
                    {
                        throw new Exception("Автомобиль не найден");
                    }

                    car.CarName = model.CarName;
                    car.Price   = model.Price;
                    car.Year    = model.Year;
                    context.SaveChanges();

                    var IDs = model.CarDetails.Select(
                        record => record.DetailId)
                              .Distinct();

                    var updateDetails = context.CarDetails.Where(
                        record => record.CarId == model.Id && IDs.Contains(record.DetailId));

                    foreach (var updateDetail in updateDetails)
                    {
                        updateDetail.Amount = model.CarDetails.FirstOrDefault(record => record.Id == updateDetail.Id).Amount;
                    }

                    context.SaveChanges();

                    context.CarDetails
                    .RemoveRange(context.CarDetails
                                 .Where(record => record.CarId == model.Id && !IDs.Contains(record.DetailId)));

                    context.SaveChanges();

                    var groupDetails = model.CarDetails
                                       .Where(record => record.Id == 0)
                                       .GroupBy(record => record.DetailId)
                                       .Select(record => new { detailId = record.Key, amount = record.Sum(r => r.Amount) });

                    foreach (var groupDetail in groupDetails)
                    {
                        var detail = context.CarDetails
                                     .FirstOrDefault(record => record.CarId == model.Id && record.DetailId == groupDetail.detailId);

                        if (detail != null)
                        {
                            detail.Amount += groupDetail.amount;
                            context.SaveChanges();
                        }
                        else
                        {
                            context.CarDetails.Add(new CarDetail
                            {
                                CarId    = model.Id,
                                DetailId = groupDetail.detailId,
                                Amount   = groupDetail.amount
                            });

                            context.SaveChanges();
                        }
                    }
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
示例#21
0
 public ActionResult Create(CarBindingModel car)
 {
     _carService.AddCar(car);
     return(RedirectToAction("Index"));
 }
示例#22
0
        public void CreateOrUpdate(CarBindingModel model)
        {
            using (var context = new KorytoDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Car element = context.Cars.FirstOrDefault(rec => rec.CarName == model.CarName && rec.Id != model.Id);
                        if (element != null)
                        {
                            throw new Exception("Уже есть автомобиль с таким названием");
                        }
                        if (model.Id.HasValue)
                        {
                            element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);
                            if (element == null)
                            {
                                throw new Exception("Элемент не найден");
                            }
                        }
                        else
                        {
                            element = new Car();
                            context.Cars.Add(element);
                        }
                        element.CarName   = model.CarName;
                        element.Price     = model.Price;
                        element.FullPrice = model.FullPrice;
                        element.Year      = model.Year;

                        context.SaveChanges();

                        if (model.Id.HasValue)
                        {
                            var CarDetails = context.CarDetails.Where(rec
                                                                      => rec.CarId == model.Id.Value).ToList();
                            context.CarDetails.RemoveRange(CarDetails.Where(rec =>
                                                                            !model.CarDetails.ContainsKey(rec.DetailId)).ToList());
                            context.SaveChanges();
                            foreach (var updateDetail in CarDetails)
                            {
                                updateDetail.Count =
                                    model.CarDetails[updateDetail.DetailId].Item2;

                                model.CarDetails.Remove(updateDetail.DetailId);
                            }
                            context.SaveChanges();
                        }
                        foreach (var pc in model.CarDetails)
                        {
                            context.CarDetails.Add(new CarDetail
                            {
                                CarId    = element.Id,
                                DetailId = pc.Key,
                                Count    = pc.Value.Item2,
                                Sum      = pc.Value.Item3
                            });
                            context.SaveChanges();
                        }
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }