From b9dc0062241f4dcf5221808d97a7e4c337a8b6cc Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 2 Feb 2020 14:35:30 +0800 Subject: Add timeline service. --- Timeline/Services/TimelineService.cs | 96 +++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 11 deletions(-) (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index a16237ca..b031297e 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -213,11 +213,12 @@ namespace Timeline.Services /// /// The name of the timeline. /// The id of owner of the timeline. - /// Thrown when or is null. - /// Thrown when timeline name is invalid. Currently it means it is an empty string. + /// The info of the new timeline. + /// Thrown when is null. + /// Thrown when timeline name is invalid. /// Thrown when the timeline already exists. /// Thrown when the owner user does not exist. - Task CreateTimeline(string name, long owner); + Task CreateTimeline(string name, long owner); } public interface IPersonalTimelineService : IBaseTimelineService @@ -245,6 +246,17 @@ namespace Timeline.Services protected IMapper Mapper { get; } + protected TimelineEntity CreateNewEntity(string? name, long owner) + { + return new TimelineEntity + { + Name = name, + OwnerId = owner, + Visibility = TimelineVisibility.Register, + CreateTime = Clock.GetCurrentTime() + }; + } + /// /// Find the timeline id by the name. /// For details, see remarks. @@ -537,6 +549,72 @@ namespace Timeline.Services } } + public class TimelineService : BaseTimelineService, ITimelineService + { + private readonly TimelineNameValidator _timelineNameValidator = new TimelineNameValidator(); + + private void ValidateTimelineName(string name, string paramName) + { + if (!_timelineNameValidator.Validate(name, out var message)) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, ExceptionTimelineNameBadFormat, message), paramName); + } + } + + public TimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock) + : base(loggerFactory, database, userService, mapper, clock) + { + + } + + protected override async Task FindTimelineId(string name) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + + ValidateTimelineName(name, nameof(name)); + + var timelineEntity = await Database.Timelines.Where(t => t.Name == name).Select(t => new { t.Id }).SingleOrDefaultAsync(); + + if (timelineEntity == null) + { + throw new TimelineNotExistException(name); + } + else + { + return timelineEntity.Id; + } + } + + public async Task CreateTimeline(string name, long owner) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + + ValidateTimelineName(name, nameof(name)); + + var user = await UserService.GetUserById(owner); + + var conflict = await Database.Timelines.AnyAsync(t => t.Name == name); + + if (conflict) + throw new ConflictException(ExceptionTimelineNameConflict); + + var newEntity = CreateNewEntity(name, owner); + Database.Timelines.Add(newEntity); + await Database.SaveChangesAsync(); + + return new TimelineInfo + { + Name = name, + Description = "", + Owner = Mapper.Map(user), + Visibility = newEntity.Visibility, + Members = new List() + }; + } + } + public class PersonalTimelineService : BaseTimelineService, IPersonalTimelineService { public PersonalTimelineService(ILoggerFactory loggerFactory, DatabaseContext database, IUserService userService, IMapper mapper, IClock clock) @@ -547,6 +625,9 @@ namespace Timeline.Services protected override async Task FindTimelineId(string name) { + if (name == null) + throw new ArgumentNullException(nameof(name)); + long userId; try { @@ -569,14 +650,7 @@ namespace Timeline.Services } else { - var newTimelineEntity = new TimelineEntity - { - Name = null, - Description = null, - OwnerId = userId, - Visibility = TimelineVisibility.Register, - CreateTime = Clock.GetCurrentTime(), - }; + var newTimelineEntity = CreateNewEntity(null, userId); Database.Timelines.Add(newTimelineEntity); await Database.SaveChangesAsync(); -- cgit v1.2.3