diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs | 50 |
1 files changed, 17 insertions, 33 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs index 73d4c49cb..92dd8273d 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Sites/ViewModels/SiteDetailsViewVM.cs @@ -13,6 +13,7 @@ using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.Authentication; using Tango.BL.ActionLogs; using Tango.BL.DTO; +using Tango.BL.Builders; namespace Tango.MachineStudio.Sites.ViewModels { @@ -41,13 +42,6 @@ namespace Tango.MachineStudio.Sites.ViewModels set { _organizations = value; RaisePropertyChangedAuto(); } } - private Organization _selectedOrganization; - public Organization SelectedOrganization - { - get { return _selectedOrganization; } - set { _selectedOrganization = value; RaisePropertyChangedAuto(); } - } - private SelectedObjectCollection<Rml> _rmls; public SelectedObjectCollection<Rml> Rmls { @@ -97,31 +91,30 @@ namespace Tango.MachineStudio.Sites.ViewModels else { _isNew = false; - Site = await _db.Sites.SingleOrDefaultAsync(x => x.Guid == siteGuid); + Site = await new SiteBuilder(_db).Set(siteGuid) + .WithSiteCatalogs() + .WithCatalogs() + .WithSiteRmls() + .WithRmls() + .WithOrganization() + .BuildAsync(); } Machines = await _db.Machines.Where(x => x.SiteGuid == Site.Guid).Include(x => x.Organization).ToListAsync(); - var site_rmls = await _db.SitesRmls.Where(x => x.SiteGuid == Site.Guid).ToListAsync(); - var site_catalogs = await _db.SitesCatalogs.Where(x => x.SiteGuid == Site.Guid).ToListAsync(); - var rmls = await _db.Rmls.OrderBy(x => x.Name).ToListAsync(); var catalogs = await _db.ColorCatalogs.OrderBy(x => x.Name).ToListAsync(); - Rmls = new SelectedObjectCollection<Rml>(rmls.ToObservableCollection(), rmls.Where(rml => site_rmls.Exists(siteRml => siteRml.RmlGuid == rml.Guid)).ToObservableCollection()); - Catalogs = new SelectedObjectCollection<ColorCatalog>(catalogs.ToObservableCollection(), catalogs.Where(catalog => site_catalogs.Exists(siteCatalog => siteCatalog.CatalogGuid == catalog.Guid)).ToObservableCollection()); - - SelectedOrganization = Organizations.SingleOrDefault(x => x.Guid == Site.OrganizationGuid); + Rmls = new SelectedObjectCollection<Rml>(rmls.ToObservableCollection(), Site.SitesRmls.Select(x => x.Rml).ToObservableCollection()); + Catalogs = new SelectedObjectCollection<ColorCatalog>(catalogs.ToObservableCollection(), Site.SitesCatalogs.Select(x => x.ColorCatalog).ToObservableCollection()); - _siteBeforeSave = SiteDTO.FromObservable(Site, site_rmls, site_catalogs, SelectedOrganization.Name); + _siteBeforeSave = SiteDTO.FromObservable(Site); } private async void Save() { try { - Site.OrganizationGuid = SelectedOrganization?.Guid; - if (!Site.Validate(_db)) { _notification.ShowError(String.Join("\n", Site.ValidationErrors)); @@ -132,38 +125,29 @@ namespace Tango.MachineStudio.Sites.ViewModels using (_notification.PushTaskItem("Saving site details...")) { - var site_rmls = await _db.SitesRmls.Where(x => x.SiteGuid == Site.Guid).ToListAsync(); - var site_catalogs = await _db.SitesCatalogs.Where(x => x.SiteGuid == Site.Guid).ToListAsync(); - - //_db.SitesRmls.RemoveRange(site_rmls); - //_db.SitesCatalogs.RemoveRange(site_catalogs); - //Remove site rmls. - site_rmls.ToList().Where(x => !Rmls.SynchedSource.ToList().Exists(y => y.Guid == x.RmlGuid)).ToList().ForEach(x => _db.SitesRmls.Remove(x)); - site_catalogs.ToList().Where(x => !Catalogs.SynchedSource.ToList().Exists(y => y.Guid == x.CatalogGuid)).ToList().ForEach(x => _db.SitesCatalogs.Remove(x)); + Site.SitesRmls.ToList().Where(x => !Rmls.SynchedSource.ToList().Exists(y => y.Guid == x.RmlGuid)).ToList().ForEach(x => _db.SitesRmls.Remove(x)); + Site.SitesCatalogs.ToList().Where(x => !Catalogs.SynchedSource.ToList().Exists(y => y.Guid == x.ColorCatalogGuid)).ToList().ForEach(x => _db.SitesCatalogs.Remove(x)); - foreach (var selectedRml in Rmls.SynchedSource.Where(x => !site_rmls.Exists(y => y.RmlGuid == x.Guid))) + foreach (var selectedRml in Rmls.SynchedSource.Where(x => !Site.SitesRmls.ToList().Exists(y => y.RmlGuid == x.Guid))) { _db.SitesRmls.Add(new SitesRml() { SiteGuid = Site.Guid, RmlGuid = selectedRml.Guid }); } - foreach (var selectedCatalog in Catalogs.SynchedSource.Where(x => !site_catalogs.Exists(y => y.CatalogGuid == x.Guid))) + foreach (var selectedCatalog in Catalogs.SynchedSource.Where(x => !Site.SitesCatalogs.ToList().Exists(y => y.ColorCatalogGuid == x.Guid))) { - _db.SitesCatalogs.Add(new SitesCatalog() { SiteGuid = Site.Guid, CatalogGuid = selectedCatalog.Guid }); + _db.SitesCatalogs.Add(new SitesCatalog() { SiteGuid = Site.Guid, ColorCatalogGuid = selectedCatalog.Guid }); } await _db.SaveChangesAsync(); - var final_site_rmls = _db.SitesRmls.ToList(); - var final_site_catalogs = _db.SitesCatalogs.ToList(); - if (_isNew) { _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.SiteCreated, _authentication.CurrentUser, Site.Name, Site, "Site created using Machine Studio."); } else { - SiteDTO siteAfter = SiteDTO.FromObservable(Site, final_site_rmls, final_site_catalogs, SelectedOrganization.Name); + SiteDTO siteAfter = SiteDTO.FromObservable(Site); _actionLogManager.InsertLog(BL.Enumerations.ActionLogType.SiteSaved, _authentication.CurrentUser, _siteBeforeSave.Name, _siteBeforeSave, siteAfter, "Site saved using Machine Studio."); _siteBeforeSave = siteAfter; } |
