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 +++++++++--- 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 BackEnd/Timeline/Services/Timeline/MultipleTimelineException.cs (limited to 'BackEnd/Timeline/Services') 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(); + } } } -- cgit v1.2.3