From e2af7d31ac20037b50d5797b7a9b16ee65cbc0ea Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Apr 2022 20:36:03 +0800 Subject: ... --- .../Timeline/Services/Timeline/ITimelineService.cs | 31 ++++++++++++--- .../Services/Timeline/MultipleTimelineException.cs | 44 ++++++++++++++++++++++ .../Timeline/Services/Timeline/TimelineService.cs | 24 +++++++++--- BackEnd/Timeline/Timeline.csproj | 2 +- BackEnd/Timeline/packages.lock.json | 6 +-- 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 BackEnd/Timeline/Services/Timeline/MultipleTimelineException.cs (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline/Services/Timeline/ITimelineService.cs b/BackEnd/Timeline/Services/Timeline/ITimelineService.cs index 62117903..72bf8a69 100644 --- a/BackEnd/Timeline/Services/Timeline/ITimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/ITimelineService.cs @@ -19,16 +19,37 @@ namespace Timeline.Services.Timeline Task CheckTimelineExistenceAsync(long id); /// - /// Get the timeline id by name. + /// Get the timeline id by name. Deprecated now because different users can have timeline with the same name now. /// /// Timeline name. /// Id of the timeline. /// Thrown when is null. /// Throw when is of bad format. - /// - /// Thrown when timeline with name does not exist. - /// - Task GetTimelineIdByNameAsync(string timelineName); + /// Thrown when timeline with name does not exist. + /// Thrown when multiple timelines have that name. + Task GetTimelineIdByNameAsync(string timelineName); + + /// + /// Get timeline id by owner id and timeline name. + /// + /// The timeline owner id. + /// The timeline name. + /// A task contains timeline id. + /// Thrown when is null. + /// Thrown when is not a valid name. + Task GetTimelineIdAsync(long ownerId, string timelineName); + + /// + /// Get timeline id by owner username and timeline name. + /// + /// The timeline owner id. + /// The timeline name. + /// A task contains timeline id. + /// Thrown when is null or is null. + /// Thrown when is not a valid username or is not a valid timeline name. + /// Thrown when user with given username does not exist. + /// Thrown when timeline with given name does not exist. + Task GetTimelineIdAsync(string ownerUsername, string timelineName); /// /// Get the timeline info. diff --git a/BackEnd/Timeline/Services/Timeline/MultipleTimelineException.cs b/BackEnd/Timeline/Services/Timeline/MultipleTimelineException.cs new file mode 100644 index 00000000..f7f80a31 --- /dev/null +++ b/BackEnd/Timeline/Services/Timeline/MultipleTimelineException.cs @@ -0,0 +1,44 @@ +using System; +namespace Timeline.Services.Timeline +{ + /// + /// Thrown when call and multiple timelines have that same name. + /// + [Serializable] + public class MultipleTimelineException : Exception + { + /// + /// Initializes a new instance of the class + /// + public MultipleTimelineException() + { + } + + /// + /// Initializes a new instance of the class + /// + /// A that describes the exception. + public MultipleTimelineException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the class + /// + /// A that describes the exception. + /// The exception that is the cause of the current exception. + public MultipleTimelineException(string message, System.Exception inner) : base(message, inner) + { + } + + /// + /// Initializes a new instance of the class + /// + /// The contextual information about the source or destination. + /// The object that holds the serialized object data. + protected MultipleTimelineException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) + { + } + } +} + diff --git a/BackEnd/Timeline/Services/Timeline/TimelineService.cs b/BackEnd/Timeline/Services/Timeline/TimelineService.cs index 82713eb1..88c6e2f8 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelineService.cs @@ -135,15 +135,19 @@ namespace Timeline.Services.Timeline } else { - var timelineEntity = await _database.Timelines.Where(t => t.Name == timelineName).Select(t => new { t.Id }).SingleOrDefaultAsync(); + var timelineEntities = await _database.Timelines.Where(t => t.Name == timelineName).Select(t => new { t.Id }).ToListAsync(); - if (timelineEntity == null) + if (timelineEntities.Count == 0) { throw CreateTimelineNotExistException(timelineName); } - else + else if (timelineEntities.Count == 1) { - return timelineEntity.Id; + return timelineEntities[0].Id; + } + else + { + throw new MultipleTimelineException(String.Format("Multiple timelines have name '{}'.", timelineName)); } } } @@ -398,6 +402,16 @@ namespace Timeline.Services.Timeline _database.Timelines.Remove(entity); await _database.SaveChangesAsync(); _logger.LogWarning(Resource.LogTimelineDelete, id); - } + } + + public Task GetTimelineIdAsync(long ownerId, string timelineName) + { + throw new NotImplementedException(); + } + + public Task GetTimelineIdAsync(string ownerUsername, string timelineName) + { + throw new NotImplementedException(); + } } } diff --git a/BackEnd/Timeline/Timeline.csproj b/BackEnd/Timeline/Timeline.csproj index 0bf11e31..26e562e8 100644 --- a/BackEnd/Timeline/Timeline.csproj +++ b/BackEnd/Timeline/Timeline.csproj @@ -36,7 +36,7 @@ - + diff --git a/BackEnd/Timeline/packages.lock.json b/BackEnd/Timeline/packages.lock.json index f3bbe4b3..258d3e8d 100644 --- a/BackEnd/Timeline/packages.lock.json +++ b/BackEnd/Timeline/packages.lock.json @@ -23,9 +23,9 @@ }, "Markdig": { "type": "Direct", - "requested": "[0.28.0, )", - "resolved": "0.28.0", - "contentHash": "gT6Sm14OaKQDwus5wFoI/v+Zxrgd2r70pkfq6dC1mIZaK1rvdEHtfnf3nD/Q/clrPFcvBIxZag79NXFjMkxhmA==" + "requested": "[0.28.1, )", + "resolved": "0.28.1", + "contentHash": "70CneXw2N/1t7v6OfZJqMKLPRB1YWTPddEIcHT/P6IL6X1zsXELIu/DHVt96kr83PIVLznMuXoFK6b9N9KTODg==" }, "Microsoft.AspNetCore.SpaServices.Extensions": { "type": "Direct", -- cgit v1.2.3