public CarEditModel BuildEdit(Guid id)
        {
            var car = carRepository.Find(id);

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

            var links = carWorkLinkRepository.GetByCarId(id);

            var carWorkList = links.Select(link => new CarWorkForm()
            {
                Date = link.Date, WorkId = link.WorkId, Id = link.Id
            }).ToList();

            var form = new CarForm()
            {
                Brand      = car.Brand,
                Model      = car.Model,
                Number     = car.Number,
                EngineType = car.EngineType,
                Works      = carWorkList
            };

            return(new CarEditModel(id, form, engineTypesListModelBuilder.Build(), BuildWorkList()));
        }
        public CarItemModel Build(Guid id)
        {
            var car = carRepository.Find(id);

            if (car == null)
            {
                throw new Exception("Машина не найдена");
            }

            var links = carWorkLinkRepository.GetByCarId(id);

            var workIds = links.Select(link => link.WorkId);

            var works = workRepository.GetByIds(workIds);

            var workList = new List <CarWorkItemModel>();

            foreach (var link in links)
            {
                var workInLink = works.FirstOrDefault(work => work.Id == link.WorkId);

                workList.Add(new CarWorkItemModel()
                {
                    Date = link.Date.ToString("dd/MM/yyyy HH:mm"), Description = workInLink?.Description
                });
            }

            return(new CarItemModel(id, car.Brand, car.Model, car.Number, car.EngineType, workList));
        }
        public void HandleEdit(CarForm form, CarValidationResult valResult)
        {
            var car = valResult.Car;

            car.Brand      = form.Brand;
            car.Model      = form.Model;
            car.Number     = form.Number;
            car.EngineType = form.EngineType;

            var linksFromDb = carWorkLinkRepository.GetByCarId(car.Id);

            entityRepository.DeleteRangeOnSave(linksFromDb);

            foreach (var work in form.Works)
            {
                var link = carWorkLinkFactory.Create(car.Id, work.WorkId, work.Date);

                entityRepository.InsertOnSave(link);
            }

            entityRepository.SaveChanges();
        }