/// <summary>
 /// Update Constructor. Call if you want to load an automobile for updates.
 /// </summary>
 /// <param name="miscController">Controller for small operations - side table controller</param>
 /// <param name="automobileController">Controller for the automobile table.</param>
 /// <param name="Model">The AutomobileDataModel you are going to update.</param>
 public AutomobileDataInput(MiscController miscController, AutomobileController automobileController, AutomobileDataModel Model)
 {
     InitializeComponent();
     this.miscController       = miscController;
     this.automobileController = automobileController;
     this.Model = Model;
 }
 private CarViewModel ConvertDataModel_To_ViewModel(AutomobileDataModel model)
 {
     return(new CarViewModel()
     {
         ID = model.Id, DRN = model.DRN, ModelName = model.Engine.CarModel.Name
     });
 }
        private AutomobileDataModel MakeModelFromToken(AutomobileSaveToken token)
        {
            OwnerDataModel      owner  = null;
            AutomobileDataModel result = new AutomobileDataModel();
            ColorDataModel      color  = null;
            EngineDataModel     engine = null;


            owner  = context.Owners.FirstOrDefault(o => o.TelephoneNumber == token.OwnerTelephoneNumber);
            color  = context.Colors.FirstOrDefault(c => c.Id == token.ColorId);
            engine = context.Engines.FirstOrDefault(e => e.Id == token.EngineID);


            if (owner == null)
            {
                owner = new OwnerDataModel()
                {
                    Name            = token.OwnerName,
                    TelephoneNumber = token.OwnerTelephoneNumber,
                };
            }

            result.DRN          = token.DRN;
            result.ChassiNumber = token.ChassiNumber;
            result.Color        = color;
            result.ColorId      = color.Id;
            result.Description  = token.Description;
            result.Engine       = engine;
            result.EngineId     = engine.Id;

            result.Owner   = owner;
            result.OwnerId = owner.Id;
            result.Year    = token.Year;

            if (!owner.Automobiles.Contains(result))
            {
                owner.Automobiles.Add(result);
            }

            return(result);
        }
        public void UpdateAutomobile(AutomobileSaveToken token, AutomobileDataModel model)
        {
            var updatedModel = MakeModelFromToken(token);

            updatedModel.Id = model.Id;
            var _model = context.Automobiles.FirstOrDefault(a => a.Id == model.Id);

            if (_model != null)
            {
                _model.ChassiNumber = updatedModel.ChassiNumber;
                _model.Color        = updatedModel.Color;
                _model.ColorId      = updatedModel.ColorId;
                _model.Description  = updatedModel.Description;
                _model.DRN          = updatedModel.DRN;
                _model.Engine       = updatedModel.Engine;
                _model.EngineId     = updatedModel.EngineId;

                _model.Owner   = updatedModel.Owner;
                _model.OwnerId = updatedModel.OwnerId;
                _model.Year    = updatedModel.Year;
            }
            EditEntity <AutomobileDataModel>(_model);
        }