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); }
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; } } } }
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 })); } }
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); }
public void Insert(CarBindingModel model) { using (var context = new AutoshowDbContext()) { context.Cars.Add(CreateModel(model, new Car())); context.SaveChanges(); } }
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); }
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; } } }
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")); }
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)); }
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(); } }
// 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)); }
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("Сотрудник не найден"); } } }
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); } }
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(); }
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()); } }
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)) })
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); } }
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; } } }
public ActionResult Create(CarBindingModel car) { _carService.AddCar(car); return(RedirectToAction("Index")); }
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; } } } }