Skip to content

Commit

Permalink
Make LocalTest maintain a copy of applicationmetadata (#8286)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarne authored May 3, 2022
1 parent 7a71ebd commit c697e5d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src/Configuration/LocalPlatformSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class LocalPlatformSettings

public string InstanceCollectionFolder { get; set; } = "instances/";

public string ApplicationsDataFolder { get; set; } = "applications/";

public string DataCollectionFolder { get; set; } = "data/";

public string EventsCollectionFolder { get; set; } = "events/";
Expand Down
7 changes: 7 additions & 0 deletions src/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using AltinnCore.Authentication.Constants;
using Altinn.Platform.Profile.Models;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Platform.Storage.Repository;

using LocalTest.Configuration;
using LocalTest.Models;
Expand All @@ -35,19 +36,22 @@ public class HomeController : Controller
private readonly LocalPlatformSettings _localPlatformSettings;
private readonly IUserProfiles _userProfileService;
private readonly IAuthentication _authenticationService;
private readonly IApplicationRepository _applicationRepository;
private readonly ILocalApp _localApp;

public HomeController(
IOptions<GeneralSettings> generalSettings,
IOptions<LocalPlatformSettings> localPlatformSettings,
IUserProfiles userProfileService,
IAuthentication authenticationService,
IApplicationRepository applicationRepository,
ILocalApp localApp)
{
_generalSettings = generalSettings.Value;
_localPlatformSettings = localPlatformSettings.Value;
_userProfileService = userProfileService;
_authenticationService = authenticationService;
_applicationRepository = applicationRepository;
_localApp = localApp;
}

Expand Down Expand Up @@ -125,6 +129,9 @@ public async Task<ActionResult> LogInTestUser(StartAppModel startAppModel)

Application app = await _localApp.GetApplicationMetadata(startAppModel.AppPathSelection);

// Ensure that the documentstorage in LocalTestingStorageBasePath is updated with the most recent app data
await _applicationRepository.Update(app);

return Redirect($"{_generalSettings.GetBaseUrl}/{app.Id}/");
}

Expand Down
57 changes: 39 additions & 18 deletions src/Services/Storage/Implementation/ApplicationRepository.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Platform.Storage.Repository;
using LocalTest.Configuration;
using LocalTest.Services.Localtest.Interface;
using LocalTest.Services.LocalApp.Interface;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace LocalTest.Services.Storage.Implementation
{
public class ApplicationRepository : IApplicationRepository
{
private readonly ILocalApp _localApp;
private readonly LocalPlatformSettings _localPlatformSettings;

public ApplicationRepository( ILocalApp localApp)
public ApplicationRepository(IOptions<LocalPlatformSettings> localPlatformSettings)
{
_localApp = localApp;
_localPlatformSettings = localPlatformSettings.Value;
}

public Task<Application> Create(Application item)
Expand All @@ -35,38 +33,61 @@ public Task<bool> Delete(string appId, string org)

public async Task<Application> FindOne(string appId, string org)
{
var application = await _localApp.GetApplicationMetadata(appId);
if (application == null)
var filename = GetApplicationsDirectory() + appId + ".json";
if (File.Exists(filename))
{
throw new Exception($"applicationmetadata for '{appId} not found'");
var application = JsonSerializer.Deserialize<Application>(await File.ReadAllTextAsync(filename));
if (application is not null)
{
return application;
}
}
return application;

throw new Exception($"applicationmetadata for '{appId} not found'");
}

public Task<Dictionary<string, Dictionary<string, string>>> GetAppTitles(List<string> appIds)
{
throw new NotImplementedException();
}

public Task<List<Application>> FindByOrg(string org)
public async Task<List<Application>> FindByOrg(string org)
{
throw new NotImplementedException();
var apps = new List<Application>();
foreach (var app in Directory.GetFiles(GetApplicationsDirectory() + org))
{
apps.Add(JsonSerializer.Deserialize<Application>(await File.ReadAllTextAsync(app)));
}

return apps;
}

public Task<Application> Update(Application item)
public async Task<Application> Update(Application item)
{
throw new NotImplementedException();
Directory.CreateDirectory(GetApplicationsDirectory()+item.Id.Split('/')[0]);
await File.WriteAllTextAsync(GetApplicationsDirectory() + item.Id + ".json", JsonSerializer.Serialize(item, new() { WriteIndented = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull }));
return item;
}

public Task<List<Application>> FindAll()
public async Task<List<Application>> FindAll()
{
throw new NotImplementedException();
var apps = new List<Application>();
foreach (var org in (new DirectoryInfo(GetApplicationsDirectory()).GetDirectories()))
{
apps.AddRange(await FindByOrg(org.Name));
}

return apps;
}

public Task<Dictionary<string, string>> GetAllAppTitles()
{
throw new NotImplementedException();
}

public string GetApplicationsDirectory()
{
return _localPlatformSettings.LocalTestingStorageBasePath + _localPlatformSettings.DocumentDbFolder + _localPlatformSettings.ApplicationsDataFolder;
}
}
}

0 comments on commit c697e5d

Please sign in to comment.