From f8eecd3d50dec23d23b2fa1b6223b9c99d974214 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Apr 2022 20:45:24 +0800 Subject: ... --- BackEnd/Timeline/Controllers/TimelineController.cs | 2 +- .../Timeline/Services/Timeline/ITimelineService.cs | 4 ++-- .../Timeline/Services/Timeline/TimelineService.cs | 25 +++++++++++----------- 3 files changed, 16 insertions(+), 15 deletions(-) (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index 42b8f210..a6749706 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -218,7 +218,7 @@ namespace Timeline.Controllers { var userId = GetAuthUserId(); - var timeline = await _service.CreateTimelineAsync(body.Name, userId); + var timeline = await _service.CreateTimelineAsync(userId, body.Name); var result = await Map(timeline); return result; } diff --git a/BackEnd/Timeline/Services/Timeline/ITimelineService.cs b/BackEnd/Timeline/Services/Timeline/ITimelineService.cs index 72bf8a69..847caf0d 100644 --- a/BackEnd/Timeline/Services/Timeline/ITimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/ITimelineService.cs @@ -142,14 +142,14 @@ namespace Timeline.Services.Timeline /// /// Create a timeline. /// - /// The name of the timeline. /// The id of owner of the timeline. + /// The name of the timeline. /// 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 CreateTimelineAsync(string timelineName, long ownerId); + Task CreateTimelineAsync(long ownerId, string timelineName); /// /// Delete a timeline. diff --git a/BackEnd/Timeline/Services/Timeline/TimelineService.cs b/BackEnd/Timeline/Services/Timeline/TimelineService.cs index 88c6e2f8..bd838c7d 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelineService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelineService.cs @@ -34,11 +34,12 @@ namespace Timeline.Services.Timeline _clock = clock; } - private static EntityAlreadyExistException CreateTimelineConflictException(string name) + private static EntityAlreadyExistException CreateTimelineConflictException(long ownerId, string timelineName) { return new EntityAlreadyExistException(EntityTypes.Timeline, new Dictionary { - ["name"] = name + [nameof(ownerId)] = ownerId, + [nameof(timelineName)] = timelineName }); } @@ -190,10 +191,10 @@ namespace Timeline.Services.Timeline if (newProperties.Name is not null) { - var conflict = await _database.Timelines.AnyAsync(t => t.Name == newProperties.Name); + var conflict = await _database.Timelines.AnyAsync(t => t.OwnerId == entity.OwnerId && t.Name == newProperties.Name); if (conflict) - throw CreateTimelineConflictException(newProperties.Name); + throw CreateTimelineConflictException(entity.OwnerId, newProperties.Name); entity.Name = newProperties.Name; @@ -371,23 +372,23 @@ namespace Timeline.Services.Timeline return entities; } - public async Task CreateTimelineAsync(string name, long owner) + public async Task CreateTimelineAsync(long ownerId, string timelineName) { - if (name == null) - throw new ArgumentNullException(nameof(name)); + if (timelineName == null) + throw new ArgumentNullException(nameof(timelineName)); - CheckTimelineName(name, nameof(name)); + CheckTimelineName(timelineName, nameof(timelineName)); - var conflict = await _database.Timelines.AnyAsync(t => t.Name == name); + var conflict = await _database.Timelines.AnyAsync(t => t.OwnerId == ownerId && t.Name == timelineName); if (conflict) - throw CreateTimelineConflictException(name); + throw CreateTimelineConflictException(ownerId, timelineName); - var entity = CreateNewTimelineEntity(name, owner); + var entity = CreateNewTimelineEntity(timelineName, ownerId); _database.Timelines.Add(entity); await _database.SaveChangesAsync(); - _logger.LogInformation(Resource.LogTimelineCreate, name, entity.Id); + _logger.LogInformation(Resource.LogTimelineCreate, timelineName, entity.Id); return entity; } -- cgit v1.2.3