public static bool AddUpdateExemption(Exemption exemption) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Find(exemption.ID); //Add Exemption if(e == null) return AddExemption(exemption); e.AssignedUsers.Clear(); context.SaveChanges(); } using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Find(exemption.ID); e.AssignedUsers = exemption.AssignedUsers; context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static UserTimeLog_DTO Add(int userId) { if (!User_DTO.Exists(userId)) return null; try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var userLog = new UserTimeLog_DTO() { FirstLogDate = DateTime.Now, LastLogDate = DateTime.Now, Duration = 0, UserId = userId }; context.UserLogs.Add(userLog); context.SaveChanges(); return GetLast(userId); } } catch (Exception ex) { throw ex; } }
public static bool AddExemption(Exemption exemption) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { foreach (var user in exemption.AssignedUsers) context.Users.Attach(user); context.Exemptions.Add(exemption); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static void RemoveAll() { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { context.Database.ExecuteSqlCommand("TRUNCATE TABLE [UserLogs]"); context.SaveChanges(); } } catch (Exception ex) { throw ex; } }
public static User_DTO UpdateUser(User_DTO user) { Mapper.CreateMap<Models.User_DTO, Models.User_DTO>() .ForMember(dest => dest.Id, opt => opt.Ignore()); using (HomesteadViewerContext context = new HomesteadViewerContext()) { try { var c = Get(user.Id); if (c == null) throw new Exception("User Could Not Be Found"); Mapper.Map<Models.User_DTO, Models.User_DTO>(user, c); context.Users.Attach(c); context.Entry(c).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return Get(user.Id); } catch (Exception ex) { return null; } } }
public static bool UpdateUser(int id, string firstName, string lastName, string phone, string email) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var user = Get(id); if (user == null) throw new Exception("User Could Not Be Found"); user.FirstName = firstName; user.LastName = lastName; user.Phone = phone; user.EmailAddress = email; context.Users.Attach(user); context.Entry(user).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static bool DeleteUser(int id) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var user = Get(id); if (user == null) return false; // foreach (var ae in GetAssignedExemptionsByUserId(user.Id)) // UnassignExemption(ae.OnlineExemptionID); context.Users.Attach(user); context.Users.Remove(user); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static void ReorderGridColumns() { try { using (var context = new HomesteadViewerContext()) { var displayedCols = context.GridColumns.Where(g => g.Displayed).OrderBy(g => g.ColumnNumber).ToList(); for (int i = 0; i < displayedCols.Count(); i++) { using (var newContext = new HomesteadViewerContext()) { var col = displayedCols[i]; col.ColumnNumber = i + 1; newContext.GridColumns.Attach(col); newContext.Entry(col).State = System.Data.Entity.EntityState.Modified; newContext.SaveChanges(); } } var hiddenCols = context.GridColumns.Where(g => !g.Displayed).OrderBy(g => g.PropertyName).ToList(); for (int i = 0; i < hiddenCols.Count(); i++) { using (var newContext = new HomesteadViewerContext()) { var col = hiddenCols[i]; col.ColumnNumber = i + displayedCols.Count() + 1; newContext.GridColumns.Attach(col); newContext.Entry(col).State = System.Data.Entity.EntityState.Modified; newContext.SaveChanges(); } } } } catch (Exception ex) { throw ex; } }
public static Setting_DTO UpdateSetting(Setting_DTO setting) { Mapper.CreateMap<Models.Setting_DTO, Models.Setting_DTO>(); using (HomesteadViewerContext context = new HomesteadViewerContext()) { try { var s = GetSetting(setting.SettingKey); if (s == null) throw new Exception("Setting Does Not Exist"); Mapper.Map(setting, s); s.SettingGroup = (string.IsNullOrEmpty(setting.SettingGroup)) ? "" : setting.SettingGroup; context.Settings.Attach(s); context.Entry(s).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return GetSetting(s.SettingKey); } catch (Exception ex) { ex.Log(); return null; } } }
public static AdministrativeProperties_DTO LogChange(int onlineExemptionID) { try { using (var context = new HomesteadViewerContext()) { var ap = AdministrativeProperties_DTO.Get(onlineExemptionID); if (ap == null) return null; ap.Modified = DateTime.Now; ap.ModifiedBy = UserSession.GetCurrentUser().FullName; context.AdministrativeProperties_Collection.Attach(ap); context.Entry(ap).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return AdministrativeProperties_DTO.Get(onlineExemptionID); } } catch (Exception ex) { throw ex; } }
public static bool DeleteExemption(Exemption exemption) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Find(exemption.ID); e.AssignedUsers.Clear(); context.SaveChanges(); } using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Find(exemption.ID); context.Exemptions.Attach(e); context.Exemptions.Remove(e); context.SaveChanges(); } return true; } catch (Exception ex) { return false; } }
public static bool AssignExemption(Exemption exemption) { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Find(exemption.ID); //Add Exemption if (e == null) return AddExemption(exemption); } using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Include(ex=>ex.AssignedUsers).FirstOrDefault(ex=>ex.ID == exemption.ID); e.AssignedUsers.Clear(); context.Exemptions.Attach(e); // context.Entry(e).Property(x => x.AssignedUsers).IsModified = true; context.SaveChanges(); } using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.Include(ex => ex.AssignedUsers).FirstOrDefault(ex => ex.ID == exemption.ID); foreach (var user in exemption.AssignedUsers) { context.Users.Attach(user); e.AssignedUsers.Add(user); } context.Exemptions.Attach(e); // context.Entry(e).Property(x => x.AssignedUsers).IsModified = true; context.SaveChanges(); } return true; }
public static ErrorLog_DTO Add(Exception exception, string className, string methodName, string additionalInfo = null) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { ErrorLog_DTO ErrorLog = new ErrorLog_DTO() { UserId = exception.UserId(), Message = exception.Message, TimeStamp = exception.TimeStamp(), Classname = exception.ClassName() ?? className, LineNumber = exception.LineNumber(), MethodName = exception.MethodName() ?? methodName, InnerException = (exception.InnerException != null) ? exception.InnerException.Message : "", AdditionalIfno = additionalInfo }; context.ErrorLogs.Add(ErrorLog); context.SaveChanges(); return context.ErrorLogs.FirstOrDefault(e => e.UserId == ErrorLog.UserId && e.TimeStamp == ErrorLog.TimeStamp); } } catch (Exception ex) { throw ex; } }
public static GridColumn_DTO UpdateGridColumn(GridColumn_DTO gridColumn) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var gc = Get(gridColumn.PropertyName); if (gc == null) return null; gc = ReorderGridColumn(gc); Mapper.CreateMap<GridColumn_DTO, GridColumn_DTO>(); Mapper.Map<GridColumn_DTO, GridColumn_DTO>(gridColumn, gc); context.GridColumns.Attach(gc); context.Entry(gc).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); ReorderGridColumns(); return Get(gridColumn.PropertyName); } } catch (Exception ex) { throw ex; } }
public static UserTimeLog_DTO Update(UserTimeLog_DTO userLog) { Mapper.CreateMap<Models.UserTimeLog_DTO, Models.UserTimeLog_DTO>() .ForMember(dest => dest.Id, opt => opt.Ignore()); using (HomesteadViewerContext context = new HomesteadViewerContext()) { try { var c = Get(userLog.Id); if (c == null) throw new Exception("User Could Not Be Found"); Mapper.Map<Models.UserTimeLog_DTO, Models.UserTimeLog_DTO>(userLog, c); TimeSpan span = c.LastLogDate - c.FirstLogDate; c.Duration = (int)span.TotalMilliseconds; context.UserLogs.Attach(c); context.Entry(c).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return Get(userLog.Id); } catch (Exception ex) { throw ex; } } }
public static bool SetExemptionStatus(int quedExemptionId, ExemptionStatus exemptionStatus) { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.FirstOrDefault(ex => ex.QuedExemptionId == quedExemptionId); //Add Exemption if (e == null) { Mapper.CreateMap<Models.QuedExemption, Models.Exemption>() .ForMember(dest => dest.QuedExemptionId, opt => opt.MapFrom(src => src.ID)); var exemption = Mapper.Map<QuedExemption, Exemption>(GetQuedExemptionById(quedExemptionId)); exemption.Status = exemptionStatus; return AddExemption(exemption); } } using (HomesteadViewerContext context = new HomesteadViewerContext()) { var e = context.Exemptions.FirstOrDefault(ex => ex.QuedExemptionId == quedExemptionId); e.Status = exemptionStatus; context.Exemptions.Attach(e); context.Entry(e).Property(x => x.Status).IsModified = true; context.SaveChanges(); } return true; }
public static bool AddUser(string userName, string phone, string email) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { User_DTO user = new User_DTO() { UserName = userName, Phone = phone, EmailAddress = email }; context.Users.Add(user); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static AdministrativeProperties_DTO AddOrUpdate(AdministrativeProperties_DTO administrativeProperties) { try { if (administrativeProperties.AssignedUserID != null) administrativeProperties.AssignedUserID = (administrativeProperties.AssignedUserID <= 0) ? null : administrativeProperties.AssignedUserID; var ap = AdministrativeProperties_DTO.Get(administrativeProperties.OnlineExemptionID); //Creaet New if (ap == null) { using (var context = new HomesteadViewerContext()) { var newAp = new AdministrativeProperties_DTO() { OnlineExemptionID = administrativeProperties.OnlineExemptionID, Comment = administrativeProperties.Comment, FollowUpDate = administrativeProperties.FollowUpDate, Status = administrativeProperties.Status, QuickrefID = administrativeProperties.QuickrefID, AssignedUserID = administrativeProperties.AssignedUserID }; if (newAp.Status != ExemptionStatus.NotWorked) newAp.DateStatusChanged = DateTime.Now; if (administrativeProperties.AssignedUserID != null) newAp.DateAssigned = DateTime.Now; context.AdministrativeProperties_Collection.Add(newAp); context.SaveChanges(); return LogChange(administrativeProperties.OnlineExemptionID); } } //Update using (var context = new HomesteadViewerContext()) { ap.Comment = administrativeProperties.Comment; ap.FollowUpDate = administrativeProperties.FollowUpDate; ap.QuickrefID = administrativeProperties.QuickrefID; if (ap.Status != administrativeProperties.Status) ap.DateStatusChanged = DateTime.Now; ap.Status = administrativeProperties.Status; if (ap.AssignedUserID != administrativeProperties.AssignedUserID) ap.DateAssigned = DateTime.Now; ap.AssignedUserID = administrativeProperties.AssignedUserID; context.AdministrativeProperties_Collection.Attach(ap); context.Entry(ap).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return LogChange(administrativeProperties.OnlineExemptionID); } } catch (Exception ex) { throw ex; } }
public static bool AddUser(User_DTO user) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { context.Users.Add(user); context.SaveChanges(); return true; } } catch (Exception ex) { return false; } }
public static Setting_DTO UpdateSetting(string settingGroup, string settingKey, string settingValue) { try { using (HomesteadViewerContext context = new HomesteadViewerContext()) { var setting = GetSetting(settingKey); if (setting == null) throw new Exception("Setting Does Not Exist."); setting.SettingGroup = (string.IsNullOrEmpty(settingGroup)) ? "" : settingGroup; setting.SettingValue = setting.SettingValue; context.Settings.Attach(setting); context.Entry(setting).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return GetSetting(settingKey); } } catch (Exception ex) { ex.Log(); return null; } }
public static GridColumn_DTO ReorderGridColumn(GridColumn_DTO new_gc) { try { using (var context = new HomesteadViewerContext()) { var orig_gc = Get(new_gc.PropertyName); if (orig_gc == null) return null; if (orig_gc.ColumnNumber < new_gc.ColumnNumber) foreach (var col in GetAll(g => g.ColumnNumber < orig_gc.ColumnNumber && g.ColumnNumber >= new_gc.ColumnNumber)) { using (var newContext = new HomesteadViewerContext()) { col.ColumnNumber--; newContext.GridColumns.Attach(col); newContext.Entry(col).State = System.Data.Entity.EntityState.Modified; newContext.SaveChanges(); } } else if (orig_gc.ColumnNumber > new_gc.ColumnNumber) foreach (var col in GetAll(g => g.ColumnNumber >= new_gc.ColumnNumber && g.ColumnNumber < orig_gc.ColumnNumber)) { using (var newContext = new HomesteadViewerContext()) { col.ColumnNumber++; newContext.GridColumns.Attach(col); newContext.Entry(col).State = System.Data.Entity.EntityState.Modified; newContext.SaveChanges(); } } else { return orig_gc; } orig_gc.ColumnNumber = new_gc.ColumnNumber; context.GridColumns.Attach(orig_gc); context.Entry(orig_gc).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); ReorderGridColumns(); return Get(orig_gc.PropertyName); } } catch (Exception ex) { throw ex; } }