-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make the user data storable (#25)
- Loading branch information
Showing
20 changed files
with
343 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
24 changes: 24 additions & 0 deletions
24
src/BB84.SAU.Application/Interfaces/Infrastructure/Persistence/IUserDataService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using BB84.SAU.Domain.Models; | ||
|
||
namespace BB84.SAU.Application.Interfaces.Infrastructure.Persistence; | ||
|
||
/// <summary> | ||
/// The user data service interface. | ||
/// </summary> | ||
public interface IUserDataService | ||
{ | ||
/// <summary> | ||
/// Loads the persisted state of the user data. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token to cancel the request.</param> | ||
/// <returns><see cref="Task{TResult}"/></returns> | ||
Task<UserDataModel> LoadUserDataAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Saves the current user data state. | ||
/// </summary> | ||
/// <param name="userData">The user data to persist.</param> | ||
/// <param name="cancellationToken">The cancellation token to cancel the request.</param> | ||
/// <returns><see cref="Task{TResult}"/></returns> | ||
Task<bool> SaveUserDataAsync(UserDataModel userData, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/BB84.SAU.Infrastructure/Persistence/UserDataService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
using BB84.Extensions.Serialization; | ||
using BB84.SAU.Application.Interfaces.Infrastructure.Persistence; | ||
using BB84.SAU.Application.Interfaces.Infrastructure.Services; | ||
using BB84.SAU.Domain.Models; | ||
using BB84.SAU.Infrastructure.Interfaces.Provider; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BB84.SAU.Infrastructure.Persistence; | ||
|
||
/// <summary> | ||
/// The user data service class. | ||
/// </summary> | ||
/// <param name="loggerService">The logger service instance to use.</param> | ||
/// <param name="fileProvider">The file provider instance to use.</param> | ||
internal sealed class UserDataService(ILoggerService<UserDataService> loggerService, IFileProvider fileProvider) : IUserDataService | ||
{ | ||
private static readonly Action<ILogger, Exception?> LogException = | ||
LoggerMessage.Define(LogLevel.Error, 0, "Exception occured."); | ||
|
||
private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web) | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | ||
}; | ||
private static readonly string AppName = "BB84.SAU"; | ||
private static readonly string DataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); | ||
private static readonly string DataFile = "UserData.json"; | ||
|
||
public async Task<UserDataModel> LoadUserDataAsync(CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
string filePath = Path.Combine(DataFolder, AppName, DataFile); | ||
|
||
if (!fileProvider.Exists(filePath)) | ||
return new(); | ||
|
||
string fileContent = await fileProvider.ReadAllTextAsync(filePath, cancellationToken); | ||
|
||
UserDataModel userData = fileContent.FromJson<UserDataModel>(SerializerOptions); | ||
|
||
return userData; | ||
} | ||
catch (Exception ex) | ||
{ | ||
loggerService.Log(LogException, ex); | ||
return new(); | ||
} | ||
} | ||
|
||
public async Task<bool> SaveUserDataAsync(UserDataModel userData, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
_ = Directory.CreateDirectory(Path.Combine(DataFolder, AppName)); | ||
|
||
string filePath = Path.Combine(DataFolder, AppName, DataFile); | ||
|
||
string fileContent = userData.ToJson(SerializerOptions); | ||
|
||
await fileProvider.WriteAllTextAsync(filePath, fileContent, cancellationToken); | ||
|
||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
loggerService.Log(LogException, ex); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.