Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public class LearningHubConfig
/// </summary>
public bool UseRedisCache { get; set; } = false;

/// <summary>
/// Gets or sets <see cref="MaxDatabaseRetryAttempts"/>.
/// </summary>
public int MaxDatabaseRetryAttempts { get; set; } = 0;

/// <summary>
/// Gets or sets <see cref="HierarchyEditPublishQueueName"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,21 @@ public static void AddLearningHubMappings(this IServiceCollection services, ICon
// External
services.AddSingleton<IEntityTypeMap, UserProfileMap>();

var dbContextOptions = new DbContextOptionsBuilder<LearningHubDbContext>()
.UseSqlServer(configuration.GetConnectionString("LearningHubDbConnection")).Options;
// Configure LearningHubDbContextOptions per scope (per request)
services.AddScoped(sp =>
{
var dbOptions = sp.GetRequiredService<DbContextOptions<LearningHubDbContext>>();
var mappings = sp.GetServices<IEntityTypeMap>();
return new LearningHubDbContextOptions(dbOptions, mappings);
});

services.AddSingleton(dbContextOptions);
services.AddSingleton<LearningHubDbContextOptions>();
// Configure DbContext
var maxDatabaseRetryAttempts = configuration.GetValue<int>("LearningHub:MaxDatabaseRetryAttempts");
services.AddDbContext<LearningHubDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("LearningHubDbConnection"),
sqlOptions => sqlOptions.EnableRetryOnFailure(maxDatabaseRetryAttempts)
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ public async Task<bool> CheckUserScormActivitySuspendDataToBeCleared(int lastSco

private async Task UpdateScormActivityAsync(int userId, ScormActivity updatedScormActivity)
{
var existingScormActivity = DbContext.ScormActivity.Where(s => s.Id == updatedScormActivity.Id)
var existingScormActivity = this.DbContext.ScormActivity.Where(s => s.Id == updatedScormActivity.Id)
.Include(sao => sao.ScormActivityObjective)
.Include(sao => sao.ScormActivityInteraction)
.ThenInclude(sai => sai.ScormActivityInteractionCorrectResponse)
.Include(sai => sai.ScormActivityInteraction)
.ThenInclude(sai => sai.ScormActivityInteractionObjective)
.AsSplitQuery()
.SingleOrDefault();

updatedScormActivity.ResourceActivityId = existingScormActivity.ResourceActivityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public UserProfileRepository(LearningHubDbContext context, ITimezoneOffsetManage
/// </summary>
/// <param name="id">The id.</param>
/// <returns>The userProfile.</returns>
public Task<UserProfile> GetByIdAsync(int id)
public async Task<UserProfile> GetByIdAsync(int id)
{
return DbContext.UserProfile.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id);
return await DbContext.UserProfile.AsNoTracking().SingleOrDefaultAsync(x => x.Id == id);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public async Task<CatalogueViewModel> GetCatalogueAsync(string reference, int us
await this.RecordNodeActivity(userId, catalogue);

var catalogueVM = this.mapper.Map<CatalogueViewModel>(catalogue);
var bookmark = this.bookmarkRepository.GetAll().Where(b => b.NodeId == catalogue.NodeId && b.UserId == userId).FirstOrDefault();
var bookmark = await this.bookmarkRepository.GetAll().Where(b => b.NodeId == catalogue.NodeId && b.UserId == userId).FirstOrDefaultAsync();
catalogueVM.BookmarkId = bookmark?.Id;
catalogueVM.IsBookmarked = !bookmark?.Deleted ?? false;
catalogueVM.Providers = await this.providerService.GetByCatalogueVersionIdAsync(catalogueVM.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ public BookmarkController(IBookmarkService bookmarkService)
[Route("GetAllByParent/{parentId?}")]
public async Task<IActionResult> GetAllByParent(int? parentId, bool? all = false)
{
if (this.CurrentUserId.GetValueOrDefault() != null)
var userId = this.CurrentUserId;

if (userId.HasValue && userId.Value != 4)
{
var bookmarks = await this.bookmarkService.GetAllByParent(this.CurrentUserId.GetValueOrDefault(), parentId, all);
var bookmarks = await this.bookmarkService.GetAllByParent(userId.Value, parentId, all);
return this.Ok(bookmarks);
}
else
{
return this.Ok(await this.bookmarkService.GetAllByParent(this.TokenWithoutBearer));
}

var fallbackBookmarks = await this.bookmarkService.GetAllByParent(this.TokenWithoutBearer);
return this.Ok(fallbackBookmarks);

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/// </summary>
public abstract class OpenApiControllerBase : ControllerBase, IDisposable
{
private const int PortalAdminId = 4;

/// <summary>
/// Gets the current user's ID.
/// </summary>
Expand All @@ -28,14 +30,14 @@ public int? CurrentUserId
}
else
{
// If parsing fails, return null - for apikey this will be the name
return null;
// If parsing fails, return PortalAdminId as default userId - for apikey this will be the name
return PortalAdminId;
}
}
else
{
// When authorizing by ApiKey we do not have a user for example
return null;
return PortalAdminId;
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions OpenAPI/LearningHub.Nhs.OpenApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public void ConfigureServices(IServiceCollection services)

services.AddRepositories(this.Configuration);
services.AddServices();

services.AddDbContext<LearningHubDbContext>(
options =>
options.UseSqlServer(this.Configuration.GetConnectionString("LearningHub")));
services.AddApplicationInsightsTelemetry();
services.AddControllers(options =>
{
Expand Down
1 change: 1 addition & 0 deletions OpenAPI/LearningHub.Nhs.OpenApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"SupportPages": "https://support.learninghub.nhs.uk/",
"SupportForm": "https://support.learninghub.nhs.uk/support/tickets/new",
"UseRedisCache": true,
"MaxDatabaseRetryAttempts": 3,
"ResourcePublishQueueRouteName": "",
"HierarchyEditPublishQueueName": "",
"ContentManagementQueueName": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private async Task UpdateScormActivityAsync(int userId, ScormActivity updatedSco
.ThenInclude(sai => sai.ScormActivityInteractionCorrectResponse)
.Include(sai => sai.ScormActivityInteraction)
.ThenInclude(sai => sai.ScormActivityInteractionObjective)
.AsSplitQuery()
.SingleOrDefault();

updatedScormActivity.ResourceActivityId = existingScormActivity.ResourceActivityId;
Expand Down
Loading