Example #1
0
        private void SaveOfferToDatabase(Offer offer)
        {
            using (var ctx = new AssistantContext())
            {
                ctx.PackagedSolutions.Attach(offer.PackagedSolution);

                offer.Appliances = AppliancesInOffer.ToList();
                offer.Salaries   = SalariesInOffer.ToList();
                offer.Materials  = MaterialsInOffer.ToList();

                // Set existing unitprices to modified state
                foreach (var unit in offer.Appliances.Concat(offer.Salaries).Concat(offer.Materials).Where(u => u.Id != 0))
                {
                    ctx.Entry(unit).State = EntityState.Modified;
                }

                if (offer.Client.CreationDate == default(DateTime))
                {
                    offer.Client.CreationDate = DateTime.Now;
                }

                if (offer.CreationDate == default(DateTime))
                {
                    offer.CreationDate = DateTime.Now;
                }

                ctx.Entry(Offer).State = Offer.Id == 0 ? EntityState.Added : EntityState.Modified;

                ctx.SaveChanges();
            }
            IsDataSaved = true;
        }
        private async void RunEditApplianceDialog()
        {
            var customDialog = new CustomDialog();

            var dialogViewModel = new CreateApplianceDialogViewModel(SelectedAppliance, false,
                                                                     instanceCancel => _dialogCoordinator.HideMetroDialogAsync(this, customDialog),
                                                                     instanceCompleted =>
            {
                _dialogCoordinator.HideMetroDialogAsync(this, customDialog);
                FilteredCollectionView.Refresh();
                AppliancesInPackagedSolutionView.Refresh();

                // Update appliance changes in database
                using (var ctx = new AssistantContext())
                {
                    ctx.Entry(SelectedAppliance).State           = EntityState.Modified;
                    ctx.Entry(SelectedAppliance.DataSheet).State = EntityState.Modified;

                    ctx.SaveChanges();
                }
            });

            customDialog.Content = new CreateApplianceDialogView {
                DataContext = dialogViewModel
            };

            await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
        }
        /// <summary>
        /// Save the viewed packaged solution to the database
        /// </summary>
        private void SaveCurrentPackagedSolution()
        {
            /* IMPORTANT
             * A packaged solution should not be saved if there exists
             *  a solar collector without a container tied to it. */
            using (var ctx = new AssistantContext())
            {
                // Copy visible list of appliance instances to the list in the model
                var newApplianceInstanceList = AppliancesInPackagedSolution.ToList();

                // Mark packaged solution as added if new or modified if old
                ctx.Entry(PackagedSolution).State = PackagedSolution.Id == 0 ? EntityState.Added : EntityState.Modified;

                // Mark removed appliance instances as deleted
                if (PackagedSolution.Id > 0)
                {
                    var previousPackagedSolution = ctx.PackagedSolutions.Include(a => a.ApplianceInstances).FirstOrDefault(p => p.Id == PackagedSolution.Id);
                    previousPackagedSolution?.ApplianceInstances.ToList().ForEach(instance =>
                    {
                        if (newApplianceInstanceList.All(a => a.Id != instance.Id))
                        {
                            ctx.Entry(instance).State = EntityState.Deleted;
                        }
                    });
                }

                // Attach appliances and appliance instances to avoid duplicates
                foreach (var appInstance in newApplianceInstanceList)
                {
                    ctx.Entry(appInstance.Appliance.DataSheet).State = EntityState.Unchanged;
                    ctx.Entry(appInstance.Appliance).State           = EntityState.Unchanged;

                    ctx.Entry(appInstance).State = appInstance.Id == 0 ? EntityState.Added : EntityState.Unchanged;
                }

                // Set new appliance instance list
                PackagedSolution.ApplianceInstances = newApplianceInstanceList;

                // Set the creation date to now
                if (PackagedSolution.CreationDate == default(DateTime))
                {
                    PackagedSolution.CreationDate = DateTime.Now;
                }

                // Save database changes
                ctx.SaveChanges();
            }

            // Set save state to true and notify print function of change
            IsDataSaved = true;
            PrintEnergyLabelCmd.NotifyCanExecuteChanged();
        }
        /// <summary>
        /// Drops an appliance from the database and removes it from the displayed list in the UI
        /// </summary>
        /// <param name="appliance"></param>
        private async void DropAppliance(Appliance appliance)
        {
            // Check if the appliance is used in any packaged solutions
            using (var ctx = new AssistantContext())
            {
                var conflictingSolutions = ctx.PackagedSolutions.Include(a => a.ApplianceInstances)
                                           .Where(s => s.ApplianceInstances.Any(a => a.Appliance.Id == appliance.Id))
                                           .ToList();
                if (conflictingSolutions.Count > 0)
                {
                    var formattedSolutionString = string.Join("\n", conflictingSolutions.Select(x => $"- {x.Name}"));
                    await _dialogCoordinator.ShowMessageAsync(this, "Fejl",
                                                              $"Komponentet kan ikke slettes, da det findes i følgende pakkeløsninger:\n{formattedSolutionString}");

                    return;
                }
            }

            // Remove from current solution
            foreach (var existingAppliance in AppliancesInPackagedSolution.Where(a => a.Appliance == appliance))
            {
                AppliancesInPackagedSolution.Remove(existingAppliance);
            }

            // Remove from visual list of appliances
            Appliances.Remove(appliance);

            // Remove from database
            using (var ctx = new AssistantContext())
            {
                ctx.Entry(appliance).State = EntityState.Deleted;

                ctx.SaveChanges();
            }
        }
Example #5
0
 public ActionResult Edit([Bind(Include = "ID,UserName,PeoID,Password,DomUserType,CreateDate,ExpireDate,DomStatus,Descriptions")] User user)
 {
     if (ModelState.IsValid)
     {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.PeoID = new SelectList(db.People, "ID", "Name", user.PeoID);
     return(View(user));
 }
Example #6
0
        /// <summary>
        /// Метод редагує та зберігає Book, обовязкова передача по ref !!! Так як метод змінює існуючий контекст.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="bookSelect"></param>
        public void EditBook(ref AssistantContext db, ref Book bookSelect)
        {
            if (bookSelect == null)
            {
                return;
            }
            Book        book    = bookSelect as Book;
            MessageView message = new MessageView(book.NameBook);

            if (message.ShowDialog() == true)
            {
                book.NameBook        = message.Message;
                db.Entry(book).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Example #7
0
        /// <summary>
        /// Метод редагує та зберігає Section, обовязкова передача по ref !!! Так як метод змінює існуючий контекст.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="sectionSelect"></param>
        public void EditSection(ref AssistantContext db, ref Section sectionSelect)
        {
            if (sectionSelect == null)
            {
                return;
            }
            Section     section = sectionSelect as Section;
            MessageView message = new MessageView(section.NameSection);

            if (message.ShowDialog() == true)
            {
                section.NameSection     = message.Message;
                db.Entry(section).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Example #8
0
        /// <summary>
        /// Метод редагує та зберігає Topic, обовязкова передача по ref !!! Так як метод змінює існуючий контекст.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="topicSelect"></param>
        public void EditTopic(ref AssistantContext db, ref Topic topicSelect)
        {
            if (topicSelect == null)
            {
                return;
            }
            Topic       topic   = topicSelect as Topic;
            MessageView message = new MessageView(topic.NameTopic);

            if (message.ShowDialog() == true)
            {
                topic.NameTopic       = message.Message;
                db.Entry(topic).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
        }
Example #9
0
        public CompanyInfoDialogViewModel(Action <CompanyInfoDialogViewModel> closeHandler, Action <CompanyInfoDialogViewModel> completionHandler)
        {
            LoadDataFromDatabase();
            PropertyChanged += OnInputReceived;
            OnPropertyChanged(); //Make save button clickable if information is already there

            SaveCommand = new RelayCommand(x =>
            {
                completionHandler(this);

                // Save changes to database
                using (var ctx = new AssistantContext())
                {
                    ctx.Entry(CompanyInformation).State = EntityState.Modified;
                    ctx.CompanyInformation.AddOrUpdate(CompanyInformation);
                    ctx.SaveChanges();
                }
            }, x => hasRequiredInformation);

            CloseCommand = new RelayCommand(x =>
            {
                closeHandler(this);
            });
        }