Skip to content

Commit 4e3e8c5

Browse files
authored
Merge pull request #1448 from TechnologyEnhancedLearning/Develop/Fixes/TD-6283
TD-6283 optimization updates
2 parents abb1816 + c7ffa43 commit 4e3e8c5

File tree

10 files changed

+39
-21
lines changed

10 files changed

+39
-21
lines changed

OpenAPI/LearningHub.Nhs.OpenApi.Models/Configuration/LearningHubConfig.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public class LearningHubConfig
4747
/// </summary>
4848
public bool UseRedisCache { get; set; } = false;
4949

50+
/// <summary>
51+
/// Gets or sets <see cref="MaxDatabaseRetryAttempts"/>.
52+
/// </summary>
53+
public int MaxDatabaseRetryAttempts { get; set; } = 0;
54+
5055
/// <summary>
5156
/// Gets or sets <see cref="HierarchyEditPublishQueueName"/>.
5257
/// </summary>

OpenAPI/LearningHub.Nhs.OpenApi.Repositories/EntityFramework/ServiceMappings.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,21 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
187187
// External
188188
services.AddSingleton<IEntityTypeMap, UserProfileMap>();
189189

190-
var dbContextOptions = new DbContextOptionsBuilder<LearningHubDbContext>()
191-
.UseSqlServer(configuration.GetConnectionString("LearningHubDbConnection")).Options;
190+
// Configure LearningHubDbContextOptions per scope (per request)
191+
services.AddScoped(sp =>
192+
{
193+
var dbOptions = sp.GetRequiredService<DbContextOptions<LearningHubDbContext>>();
194+
var mappings = sp.GetServices<IEntityTypeMap>();
195+
return new LearningHubDbContextOptions(dbOptions, mappings);
196+
});
192197

193-
services.AddSingleton(dbContextOptions);
194-
services.AddSingleton<LearningHubDbContextOptions>();
198+
// Configure DbContext
199+
var maxDatabaseRetryAttempts = configuration.GetValue<int>("LearningHub:MaxDatabaseRetryAttempts");
200+
services.AddDbContext<LearningHubDbContext>(options =>
201+
options.UseSqlServer(
202+
configuration.GetConnectionString("LearningHubDbConnection"),
203+
sqlOptions => sqlOptions.EnableRetryOnFailure(maxDatabaseRetryAttempts)
204+
));
195205
}
196206
}
197207
}

OpenAPI/LearningHub.Nhs.OpenApi.Repositories/Repositories/Activity/ScormActivityRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,13 @@ public async Task<bool> CheckUserScormActivitySuspendDataToBeCleared(int lastSco
220220

221221
private async Task UpdateScormActivityAsync(int userId, ScormActivity updatedScormActivity)
222222
{
223-
var existingScormActivity = DbContext.ScormActivity.Where(s => s.Id == updatedScormActivity.Id)
223+
var existingScormActivity = this.DbContext.ScormActivity.Where(s => s.Id == updatedScormActivity.Id)
224224
.Include(sao => sao.ScormActivityObjective)
225225
.Include(sao => sao.ScormActivityInteraction)
226226
.ThenInclude(sai => sai.ScormActivityInteractionCorrectResponse)
227227
.Include(sai => sai.ScormActivityInteraction)
228228
.ThenInclude(sai => sai.ScormActivityInteractionObjective)
229+
.AsSplitQuery()
229230
.SingleOrDefault();
230231

231232
updatedScormActivity.ResourceActivityId = existingScormActivity.ResourceActivityId;

OpenAPI/LearningHub.Nhs.OpenApi.Repositories/Repositories/UserProfileRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public UserProfileRepository(LearningHubDbContext context, ITimezoneOffsetManage
2626
/// </summary>
2727
/// <param name="id">The id.</param>
2828
/// <returns>The userProfile.</returns>
29-
public Task<UserProfile> GetByIdAsync(int id)
29+
public async Task<UserProfile> GetByIdAsync(int id)
3030
{
31-
return DbContext.UserProfile.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id);
31+
return await DbContext.UserProfile.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id);
3232
}
3333

3434
/// <summary>

OpenAPI/LearningHub.Nhs.OpenApi.Services/Services/CatalogueService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public async Task<CatalogueViewModel> GetCatalogueAsync(string reference, int us
543543
await this.RecordNodeActivity(userId, catalogue);
544544

545545
var catalogueVM = this.mapper.Map<CatalogueViewModel>(catalogue);
546-
var bookmark = this.bookmarkRepository.GetAll().Where(b => b.NodeId == catalogue.NodeId && b.UserId == userId).FirstOrDefault();
546+
var bookmark = await this.bookmarkRepository.GetAll().Where(b => b.NodeId == catalogue.NodeId && b.UserId == userId).FirstOrDefaultAsync();
547547
catalogueVM.BookmarkId = bookmark?.Id;
548548
catalogueVM.IsBookmarked = !bookmark?.Deleted ?? false;
549549
catalogueVM.Providers = await this.providerService.GetByCatalogueVersionIdAsync(catalogueVM.Id);

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/BookmarkController.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ public BookmarkController(IBookmarkService bookmarkService)
3737
[Route("GetAllByParent/{parentId?}")]
3838
public async Task<IActionResult> GetAllByParent(int? parentId, bool? all = false)
3939
{
40-
if (this.CurrentUserId.GetValueOrDefault() != null)
40+
var userId = this.CurrentUserId;
41+
42+
if (userId.HasValue && userId.Value != 4)
4143
{
42-
var bookmarks = await this.bookmarkService.GetAllByParent(this.CurrentUserId.GetValueOrDefault(), parentId, all);
44+
var bookmarks = await this.bookmarkService.GetAllByParent(userId.Value, parentId, all);
4345
return this.Ok(bookmarks);
4446
}
45-
else
46-
{
47-
return this.Ok(await this.bookmarkService.GetAllByParent(this.TokenWithoutBearer));
48-
}
47+
48+
var fallbackBookmarks = await this.bookmarkService.GetAllByParent(this.TokenWithoutBearer);
49+
return this.Ok(fallbackBookmarks);
50+
4951
}
5052

5153
/// <summary>

OpenAPI/LearningHub.Nhs.OpenApi/Controllers/OpenApiControllerBase.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/// </summary>
1212
public abstract class OpenApiControllerBase : ControllerBase, IDisposable
1313
{
14+
private const int PortalAdminId = 4;
15+
1416
/// <summary>
1517
/// Gets the current user's ID.
1618
/// </summary>
@@ -28,14 +30,14 @@ public int? CurrentUserId
2830
}
2931
else
3032
{
31-
// If parsing fails, return null - for apikey this will be the name
32-
return null;
33+
// If parsing fails, return PortalAdminId as default userId - for apikey this will be the name
34+
return PortalAdminId;
3335
}
3436
}
3537
else
3638
{
3739
// When authorizing by ApiKey we do not have a user for example
38-
return null;
40+
return PortalAdminId;
3941
}
4042
}
4143
}

OpenAPI/LearningHub.Nhs.OpenApi/Startup.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ public void ConfigureServices(IServiceCollection services)
8484

8585
services.AddRepositories(this.Configuration);
8686
services.AddServices();
87-
88-
services.AddDbContext<LearningHubDbContext>(
89-
options =>
90-
options.UseSqlServer(this.Configuration.GetConnectionString("LearningHub")));
9187
services.AddApplicationInsightsTelemetry();
9288
services.AddControllers(options =>
9389
{

OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"SupportPages": "https://support.learninghub.nhs.uk/",
7777
"SupportForm": "https://support.learninghub.nhs.uk/support/tickets/new",
7878
"UseRedisCache": true,
79+
"MaxDatabaseRetryAttempts": 3,
7980
"ResourcePublishQueueRouteName": "",
8081
"HierarchyEditPublishQueueName": "",
8182
"ContentManagementQueueName": "",

WebAPI/LearningHub.Nhs.Repository/Activity/ScormActivityRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ private async Task UpdateScormActivityAsync(int userId, ScormActivity updatedSco
226226
.ThenInclude(sai => sai.ScormActivityInteractionCorrectResponse)
227227
.Include(sai => sai.ScormActivityInteraction)
228228
.ThenInclude(sai => sai.ScormActivityInteractionObjective)
229+
.AsSplitQuery()
229230
.SingleOrDefault();
230231

231232
updatedScormActivity.ResourceActivityId = existingScormActivity.ResourceActivityId;

0 commit comments

Comments
 (0)