From e21b5f85f0d66f51e23a7c1cbf260f2981a83a49 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 19:17:08 +0800 Subject: ... --- BackEnd/Timeline/Services/DataManager.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'BackEnd/Timeline/Services/DataManager.cs') diff --git a/BackEnd/Timeline/Services/DataManager.cs b/BackEnd/Timeline/Services/DataManager.cs index d447b0d5..f24bb59b 100644 --- a/BackEnd/Timeline/Services/DataManager.cs +++ b/BackEnd/Timeline/Services/DataManager.cs @@ -38,13 +38,12 @@ namespace Timeline.Services public Task FreeEntry(string tag); /// - /// Retrieve the entry with given tag. + /// Retrieve the entry with given tag. If not exist, returns null. /// /// The tag of the entry. - /// The data of the entry. + /// The data of the entry. If not exist, returns null. /// Thrown when is null. - /// Thrown when entry with given tag does not exist. - public Task GetEntry(string tag); + public Task GetEntry(string tag); } public class DataManager : IDataManager @@ -106,17 +105,31 @@ namespace Timeline.Services } } - public async Task GetEntry(string tag) + public async Task GetEntry(string tag) { if (tag == null) throw new ArgumentNullException(nameof(tag)); var entity = await _database.Data.Where(d => d.Tag == tag).Select(d => new { d.Data }).SingleOrDefaultAsync(); - if (entity == null) - throw new InvalidOperationException(Resources.Services.DataManager.ExceptionEntryNotExist); + if (entity is null) + return null; return entity.Data; } } + + public static class DataManagerExtensions + { + /// + /// Try to get an entry and throw if not exist. + /// + public static async Task GetEntryAndCheck(this IDataManager dataManager, string tag, string notExistMessage) + { + var data = await dataManager.GetEntry(tag); + if (data is null) + throw new DatabaseCorruptedException($"Can't get data of tag {tag}. {notExistMessage}"); + return data; + } + } } -- cgit v1.2.3