/// <summary>
 /// Delete a single product
 /// </summary>
 /// <param name="dc"></param>
 /// <param name="product"></param>
 public void Delete(DataContext dc, Domain.Product product)
 {
     dc = dc ?? Conn.GetContext();
     var dbProduct = dc.Products.Where(p => p.ProductID == product.ID).SingleOrDefault();
     if (dbProduct == null) return;
     dc.Products.Attach(dbProduct, true);
     dc.Products.DeleteOnSubmit(dbProduct);
     dc.SubmitChanges();
 }
 /// <summary>
 /// Delete a single TaskRepository
 /// </summary>
 /// <param name="dc">DataContext</param>
 /// <param name="task"></param>
 public void Delete(DataContext dc, Domain.Task task)
 {
     dc = dc ?? Conn.GetContext();
     var dbTask = dc.Tasks.Where(t => t.TaskID == task.ID).SingleOrDefault();
     if (dbTask == null) return;
     //dc.Tasks.Attach(dbTask, true);
     dc.Tasks.DeleteOnSubmit(dbTask);
     dc.SubmitChanges();
 }
 /// <summary>
 /// Delete a single TaskType
 /// </summary>
 /// <param name="dc">DataContext</param>
 /// <param name="taskType"></param>
 public void Delete(DataContext dc, Domain.TaskType taskType)
 {
     dc = dc ?? Conn.GetContext();
     var dbTaskType = dc.TaskTypes.Where(t => t.TaskTypeId == taskType.ID).SingleOrDefault();
     if (dbTaskType == null) return;
     dc.TaskTypes.Attach(dbTaskType, true);
     dc.TaskTypes.DeleteOnSubmit(dbTaskType);
     dc.SubmitChanges();
 }
 /// <summary>
 /// Delete a single greenhouse
 /// </summary>
 /// <param name="dc"></param>
 /// <param name="greenhouse"></param>
 public void Delete(DataContext dc, Domain.Greenhouse greenhouse)
 {
     dc = dc ?? Conn.GetContext();
     var dbGreenhouse = dc.Greenhouses.Where(g => g.GreenhouseID == greenhouse.ID).SingleOrDefault();
     if (dbGreenhouse == null) return;
     dc.GreenhouseUsers.DeleteAllOnSubmit(dbGreenhouse.GreenhouseUsers);
     foreach (var section in dbGreenhouse.Sections) { dc.Tasks.DeleteAllOnSubmit(section.Tasks); }
     dc.Sections.DeleteAllOnSubmit(dbGreenhouse.Sections);
     dc.Greenhouses.DeleteOnSubmit(dbGreenhouse);
     dc.SubmitChanges();
 }
 /// <summary>
 /// Delete a single 
 /// </summary>
 /// <param name="dc"></param>
 /// <param name="section"></param>
 public void Delete(DataContext dc, Domain.Section section)
 {
     dc = dc ?? Conn.GetContext();
     var dbSection = dc.Sections.Where(s => s.SectionID == section.ID).SingleOrDefault();
     if (dbSection == null) return;
     foreach (var task in dbSection.Tasks)
     {
         dc.Tasks.DeleteOnSubmit(task);
     }
     dc.Sections.DeleteOnSubmit(dbSection);
     dc.SubmitChanges();
 }
 public void Delete(DataContext dc, Domain.GreenhouseUser greenhouseUser)
 {
     dc = dc ?? Conn.GetContext();
     var dbGreenhouseUser = dc.GreenhouseUsers.Where(g => g.GreenhouseUserId == greenhouseUser.ID).SingleOrDefault();
     if (dbGreenhouseUser == null) return;
     foreach (var section in dbGreenhouseUser.Greenhouse.Sections.Where(gu => gu.UserID == dbGreenhouseUser.UserId))
     {
         dc.Sections.DeleteOnSubmit(section);
         foreach (var task in section.Tasks)
         {
             dc.Tasks.DeleteOnSubmit(task);
         }
     }
     dc.GreenhouseUsers.DeleteOnSubmit(dbGreenhouseUser);
     dc.SubmitChanges();
 }
        public int Save(DataContext dc, Domain.GreenhouseUser greenhouseUser)
        {
            dc = dc ?? Conn.GetContext();
            var dbGreenhouseUser = dc.GreenhouseUsers.Where(g => g.GreenhouseUserId == greenhouseUser.ID).SingleOrDefault();
            var isNew = false;
            if (dbGreenhouseUser == null)
            {
                dbGreenhouseUser = new GreenhouseUser();
                isNew = true;
            }

            dbGreenhouseUser.UserId = greenhouseUser.UserID;
            dbGreenhouseUser.GreenhouseId = greenhouseUser.GreenhouseID;

            if (isNew)
            {
                dc.GreenhouseUsers.InsertOnSubmit(dbGreenhouseUser);
            }
            dc.SubmitChanges();

            greenhouseUser.ID = dbGreenhouseUser.GreenhouseId;

            return greenhouseUser.ID;
        }
        /// <summary>
        /// Save a Product
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="product"></param>
        /// <returns>returns the id of the saved product</returns>
        public int Save(DataContext dc, Domain.Product product)
        {
            dc = dc ?? Conn.GetContext();
            var dbProduct = dc.Products.Where(p => p.ProductID == product.ID).SingleOrDefault();
            var isNew = false;
            if (dbProduct == null)
            {
                dbProduct = new Product();
                isNew = true;
            }

            dbProduct.Name = product.Name;
            dbProduct.Description = product.Description;
            dbProduct.Price = product.Price;
            dbProduct.IsActive = product.IsActive;
            dbProduct.Image = product.Image;
            dbProduct.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbProduct.DateCreated = DateTime.Now;
                dc.Products.InsertOnSubmit(dbProduct);
            }
            dc.SubmitChanges();
            return dbProduct.ProductID;
        }
        /// <summary>
        /// Save a TaskRepository
        /// </summary>
        /// <param name="dc">DataContext</param>
        /// <param name="task"></param>
        /// <returns>returns the id of the saved task</returns>
        public int Save(DataContext dc, Domain.Task task)
        {
            dc = dc ?? Conn.GetContext();
            var dbTask = dc.Tasks.Where(t => t.TaskID == task.ID).SingleOrDefault();
            var isNew = false;
            if (dbTask == null)
            {
                dbTask = new DataAccess.SqlRepository.Task();
                isNew = true;
            }

            dbTask.SectionID = task.SectionID;
            dbTask.StartTime = task.StartTime;
            dbTask.EndTime = task.StartTime.AddMinutes(task.Interval);
            dbTask.TaskTypeID = task.TaskTypeId;
            dbTask.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbTask.DateCreated = DateTime.Now;
                dc.Tasks.InsertOnSubmit(dbTask);
            }
            dc.SubmitChanges();
            return dbTask.TaskID;
        }
        /// <summary>
        /// Save a Preset
        /// </summary>
        /// <param name="dc">DataContext</param>
        /// <param name="preset"></param>
        /// <returns>returns the id of the saved preset</returns>
        public int Save(DataContext dc, Domain.Preset preset)
        {
            dc = dc ?? Conn.GetContext();
            var dbPreset = dc.Presets.Where(p => p.PresetID == preset.ID).SingleOrDefault();
            var isNew = false;
            if (dbPreset == null)
            {
                dbPreset = new Preset();
                isNew = true;
            }

            dbPreset.Name = preset.Name;
            dbPreset.UserID = preset.UserID;
            dbPreset.IdealTemperature = preset.IdealTemperature;
            dbPreset.TemperatureThreshold = preset.TemperatureThreshold;
            dbPreset.IdealLightIntensity = preset.IdealLightIntensity;
            dbPreset.LightIntensityThreshold = preset.LightIntensityThreshold;
            dbPreset.IdealHumidity = preset.IdealHumidity;
            dbPreset.HumidityThreshold = preset.HumidityThreshold;
            dbPreset.IdealWaterLevel = preset.IdealWaterLevel;
            dbPreset.WaterLevelThreshold = preset.WaterLevelThreshold;
            dbPreset.IsGlobal = preset.IsGlobal;
            dbPreset.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbPreset.DateCreated = DateTime.Now;
                dc.Presets.InsertOnSubmit(dbPreset);
            }
            dc.SubmitChanges();
            return dbPreset.PresetID;
        }
        /// <summary>
        /// Save a Greenhouse
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="greenhouse"></param>
        /// <returns>returns the id of the saved greenhouse</returns>
        public int Save(DataContext dc, Domain.Greenhouse greenhouse)
        {
            dc = dc ?? Conn.GetContext();
            var dbGreenhouse = dc.Greenhouses.Where(g => g.GreenhouseID == greenhouse.ID).SingleOrDefault();
            var isNew = false;
            if (dbGreenhouse == null)
            {
                dbGreenhouse = new DataAccess.SqlRepository.Greenhouse();
                isNew = true;
            }

            dbGreenhouse.City = greenhouse.Address.City;
            dbGreenhouse.Country = greenhouse.Address.Country;
            dbGreenhouse.StateOrProvince = greenhouse.Address.StateOrProvince;
            dbGreenhouse.Zip = greenhouse.Address.Zip;
            dbGreenhouse.StreetLine1 = greenhouse.Address.StreetLine1;
            dbGreenhouse.StreetLine2 = greenhouse.Address.StreetLine2;
            dbGreenhouse.IsDefault = greenhouse.Address.IsDefault;
            dbGreenhouse.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbGreenhouse.DateCreated = DateTime.Now;
                dc.Greenhouses.InsertOnSubmit(dbGreenhouse);
            }
            dc.SubmitChanges();

            greenhouse.ID = dbGreenhouse.GreenhouseID;

            if (greenhouse.Sections != null)
                foreach (Section section in greenhouse.Sections)
                {
                    section.Save();
                }

            return greenhouse.ID;
        }
        /// <summary>
        /// Save a Section
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="section"></param>
        /// <returns>returns the id of the saved section</returns>
        public int Save(DataContext dc, Domain.Section section)
        {
            dc = dc ?? Conn.GetContext();
            var dbSection = dc.Sections.Where(s => s.SectionID == section.ID).SingleOrDefault();
            var isNew = false;
            if (dbSection == null)
            {
                dbSection = new DataAccess.SqlRepository.Section();
                isNew = true;
            }

            dbSection.Name = section.Name;
            dbSection.GreenhouseID = section.GreenhouseID;
            dbSection.PresetID = section.PresetID;
            dbSection.UserID = section.UserID;
            dbSection.IsTemeratureActivated = section.IsTemperatureActivated;
            dbSection.IdealTemperature = section.IdealTemperature;
            dbSection.TemperatureThreshold = section.TemperatureThreshold;
            dbSection.IsLightActivated = section.IsLightActivated;
            dbSection.IdealLightIntensity = section.IdealLightIntensity;
            dbSection.LightIntensityThreshold = section.LightIntensityThreshold;
            dbSection.IsHumidityActivated = section.IsHumidityActivated;
            dbSection.IdealHumidity = section.IdealHumidity;
            dbSection.HumidityThreshold = section.HumidityThreshold;
            dbSection.IsWaterLevelActivated = section.IsWaterLevelActivated;
            dbSection.IdealWaterLevel = section.IdealWaterLevel;
            dbSection.WaterLevelThreshold = section.WaterLevelThreshold;
            dbSection.DateUpdated = DateTime.Now;

            if (isNew)
            {
                dbSection.DateCreated = DateTime.Now;
                dc.Sections.InsertOnSubmit(dbSection);
            }
            dc.SubmitChanges();
            return dbSection.SectionID;
        }
        /// <summary>
        /// Save a TaskType
        /// </summary>
        /// <param name="dc">DataContext</param>
        /// <param name="taskType"></param>
        /// <returns>returns the id of the saved taskType</returns>
        public int Save(DataContext dc, Domain.TaskType taskType)
        {
            dc = dc ?? Conn.GetContext();
            var dcTaskType = dc.TaskTypes.Where(t => t.TaskTypeId == taskType.ID).SingleOrDefault();
            var isNew = false;
            if (dcTaskType == null)
            {
                dcTaskType = new DataAccess.SqlRepository.TaskType();
                isNew = true;
            }

            dcTaskType.Name = taskType.Name;

            if (isNew)
            {
                dc.TaskTypes.InsertOnSubmit(dcTaskType);
            }
            dc.SubmitChanges();
            return dcTaskType.TaskTypeId;
        }