private void UpdatePermissions(DbAppContext context) { // adding system Account if not there in the database ImportUtility.InsertSystemUser(context, SystemId); IEnumerable <Permission> permissions = Permission.AllPermissions; Logger.LogDebug("Updating permissions ..."); foreach (Permission permission in permissions) { Logger.LogDebug($"Looking up {permission.Code} ..."); Permission p = context.Permissions.FirstOrDefault(x => x.Code == permission.Code); if (p == null) { Logger.LogDebug($"{permission.Code} does not exist, adding it ..."); permission.AppCreateUserid = SystemId; permission.AppCreateTimestamp = DateTime.UtcNow; permission.AppLastUpdateUserid = SystemId; permission.AppLastUpdateTimestamp = DateTime.UtcNow; context.Permissions.Add(permission); } else { Logger.LogDebug($"Updating the fields for {permission.Code} ..."); p.Description = permission.Description; p.Name = permission.Name; } } }
/// <summary> /// Update user /// </summary> /// <param name="context"></param> /// <param name="userInfo"></param> public static void UpdateSeedUserInfo(this DbAppContext context, User userInfo) { // adding system Account if not there in the database ImportUtility.InsertSystemUser(context, SystemId); User user = context.GetUserByGuid(userInfo.Guid); if (user == null) { userInfo.AppCreateUserid = SystemId; userInfo.AppCreateTimestamp = DateTime.UtcNow; userInfo.AppLastUpdateUserid = SystemId; userInfo.AppLastUpdateTimestamp = DateTime.UtcNow; context.Users.Add(userInfo); } else { user.Active = userInfo.Active; user.Email = userInfo.Email; user.GivenName = userInfo.GivenName; user.Initials = userInfo.Initials; user.SmAuthorizationDirectory = userInfo.SmAuthorizationDirectory; user.SmUserId = userInfo.SmUserId; user.Surname = userInfo.Surname; user.District = userInfo.District; // Sync Roles if (user.UserRoles != null) { foreach (UserRole item in user.UserRoles) { context.Entry(item).State = EntityState.Deleted; } foreach (UserRole item in userInfo.UserRoles) { item.AppCreateUserid = SystemId; item.AppCreateTimestamp = DateTime.UtcNow; item.AppLastUpdateUserid = SystemId; item.AppLastUpdateTimestamp = DateTime.UtcNow; user.UserRoles.Add(item); } } } }
/// <summary> /// Update region /// </summary> /// <param name="context"></param> /// <param name="regionInfo"></param> public static void UpdateSeedRegionInfo(this DbAppContext context, Region regionInfo) { // adding system Account if not there in the database ImportUtility.InsertSystemUser(context, SystemId); Region region = context.GetRegionByMinistryRegionId(regionInfo.MinistryRegionID); if (region == null) { regionInfo.AppCreateUserid = SystemId; regionInfo.AppCreateTimestamp = DateTime.UtcNow; regionInfo.AppLastUpdateUserid = SystemId; regionInfo.AppLastUpdateTimestamp = DateTime.UtcNow; context.Regions.Add(regionInfo); } else { region.Name = regionInfo.Name; region.StartDate = regionInfo.StartDate; } }
/// <summary> /// Update district /// </summary> /// <param name="context"></param> /// <param name="districtInfo"></param> public static void UpdateSeedDistrictInfo(this DbAppContext context, District districtInfo) { // adding system Account if not there in the database ImportUtility.InsertSystemUser(context, SystemId); // Adjust the region. int ministryRegionId = districtInfo.Region.MinistryRegionID; var exists = context.Regions.Any(a => a.MinistryRegionID == ministryRegionId); if (exists) { Region region = context.Regions.First(a => a.MinistryRegionID == ministryRegionId); districtInfo.Region = region; } else { districtInfo.Region = null; } District district = context.GetDistrictByMinistryDistrictId(districtInfo.MinistryDistrictID); if (district == null) { districtInfo.AppCreateUserid = SystemId; districtInfo.AppCreateTimestamp = DateTime.UtcNow; districtInfo.AppLastUpdateUserid = SystemId; districtInfo.AppLastUpdateTimestamp = DateTime.UtcNow; context.Districts.Add(districtInfo); } else { district.Name = districtInfo.Name; district.Region = districtInfo.Region; district.StartDate = districtInfo.StartDate; } }
/// <summary> /// Update service area /// </summary> /// <param name="context"></param> /// <param name="serviceAreaInfo"></param> public static void UpdateSeedServiceAreaInfo(this DbAppContext context, ServiceArea serviceAreaInfo) { // adding system Account if not there in the database ImportUtility.InsertSystemUser(context, SystemId); // Adjust the district. int ministryDistrictId = serviceAreaInfo.District.MinistryDistrictID; var exists = context.Districts.Any(a => a.MinistryDistrictID == ministryDistrictId); if (exists) { District district = context.Districts.First(a => a.MinistryDistrictID == ministryDistrictId); serviceAreaInfo.District = district; } else { serviceAreaInfo.District = null; } ServiceArea serviceArea = context.GetServiceAreaByMinistryServiceAreaId(serviceAreaInfo.MinistryServiceAreaID); if (serviceArea == null) { serviceAreaInfo.AppCreateUserid = SystemId; serviceAreaInfo.AppCreateTimestamp = DateTime.UtcNow; serviceAreaInfo.AppLastUpdateUserid = SystemId; serviceAreaInfo.AppLastUpdateTimestamp = DateTime.UtcNow; context.ServiceAreas.Add(serviceAreaInfo); } else { serviceArea.Name = serviceAreaInfo.Name; serviceArea.StartDate = serviceAreaInfo.StartDate; serviceArea.District = serviceAreaInfo.District; } }