From 415a91f08422a9a18958552ec21a25f336ef81c4 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 31 Jan 2021 15:42:52 +0800 Subject: ... --- BackEnd/Timeline/Services/TimelineService.cs | 99 ++++++---------------------- 1 file changed, 21 insertions(+), 78 deletions(-) (limited to 'BackEnd/Timeline/Services/TimelineService.cs') diff --git a/BackEnd/Timeline/Services/TimelineService.cs b/BackEnd/Timeline/Services/TimelineService.cs index 1d1bb320..f4141752 100644 --- a/BackEnd/Timeline/Services/TimelineService.cs +++ b/BackEnd/Timeline/Services/TimelineService.cs @@ -49,6 +49,7 @@ namespace Timeline.Services public class TimelineChangePropertyParams { + public string? Name { get; set; } public string? Title { get; set; } public string? Description { get; set; } public TimelineVisibility? Visibility { get; set; } @@ -59,22 +60,6 @@ namespace Timeline.Services /// public interface ITimelineService : IBasicTimelineService { - /// - /// Get the timeline last modified time (not include name change). - /// - /// The id of the timeline. - /// The timeline modified time. - /// Thrown when timeline does not exist. - Task GetTimelineLastModifiedTime(long id); - - /// - /// Get the timeline unique id. - /// - /// The id of the timeline. - /// The timeline unique id. - /// Thrown when timeline does not exist. - Task GetTimelineUniqueId(long id); - /// /// Get the timeline info. /// @@ -90,6 +75,7 @@ namespace Timeline.Services /// The new properties. Null member means not to change. /// Thrown when is null. /// Thrown when timeline with given id does not exist. + /// Thrown when a timeline with new name already exists. Task ChangeProperty(long id, TimelineChangePropertyParams newProperties); /// @@ -180,20 +166,6 @@ namespace Timeline.Services /// The id of the timeline to delete. /// Thrown when the timeline does not exist. Task DeleteTimeline(long id); - - /// - /// Change name of a timeline. - /// - /// The timeline id. - /// The new timeline name. - /// Thrown when is null. - /// Thrown when is of invalid format. - /// Thrown when timeline does not exist. - /// Thrown when a timeline with new name already exists. - /// - /// You can only change name of general timeline. - /// - Task ChangeTimelineName(long id, string newTimelineName); } public class TimelineService : BasicTimelineService, ITimelineService @@ -222,26 +194,6 @@ namespace Timeline.Services } } - public async Task GetTimelineLastModifiedTime(long id) - { - var entity = await _database.Timelines.Where(t => t.Id == id).Select(t => new { t.LastModified }).SingleOrDefaultAsync(); - - if (entity is null) - throw new TimelineNotExistException(id); - - return entity.LastModified; - } - - public async Task GetTimelineUniqueId(long id) - { - var entity = await _database.Timelines.Where(t => t.Id == id).Select(t => new { t.UniqueId }).SingleOrDefaultAsync(); - - if (entity is null) - throw new TimelineNotExistException(id); - - return entity.UniqueId; - } - public async Task GetTimeline(long id) { var entity = await _database.Timelines.Where(t => t.Id == id).SingleOrDefaultAsync(); @@ -257,12 +209,29 @@ namespace Timeline.Services if (newProperties is null) throw new ArgumentNullException(nameof(newProperties)); + if (newProperties.Name is not null) + ValidateTimelineName(newProperties.Name, nameof(newProperties)); + var entity = await _database.Timelines.Where(t => t.Id == id).SingleOrDefaultAsync(); if (entity is null) throw new TimelineNotExistException(id); var changed = false; + var nameChanged = false; + + if (newProperties.Name is not null) + { + var conflict = await _database.Timelines.AnyAsync(t => t.Name == newProperties.Name); + + if (conflict) + throw new EntityAlreadyExistException(EntityNames.Timeline, null, ExceptionTimelineNameConflict); + + entity.Name = newProperties.Name; + + changed = true; + nameChanged = true; + } if (newProperties.Title != null) { @@ -286,6 +255,8 @@ namespace Timeline.Services { var currentTime = _clock.GetCurrentTime(); entity.LastModified = currentTime; + if (nameChanged) + entity.NameLastModified = currentTime; } await _database.SaveChangesAsync(); @@ -447,34 +418,6 @@ namespace Timeline.Services _database.Timelines.Remove(entity); await _database.SaveChangesAsync(); } - - public async Task ChangeTimelineName(long id, string newTimelineName) - { - if (newTimelineName == null) - throw new ArgumentNullException(nameof(newTimelineName)); - - ValidateTimelineName(newTimelineName, nameof(newTimelineName)); - - var entity = await _database.Timelines.Where(t => t.Id == id).SingleOrDefaultAsync(); - - if (entity is null) - throw new TimelineNotExistException(id); - - if (entity.Name == newTimelineName) return; - - var conflict = await _database.Timelines.AnyAsync(t => t.Name == newTimelineName); - - if (conflict) - throw new EntityAlreadyExistException(EntityNames.Timeline, null, ExceptionTimelineNameConflict); - - var now = _clock.GetCurrentTime(); - - entity.Name = newTimelineName; - entity.NameLastModified = now; - entity.LastModified = now; - - await _database.SaveChangesAsync(); - } } public static class TimelineServiceExtensions -- cgit v1.2.3