From f8eecd3d50dec23d23b2fa1b6223b9c99d974214 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Apr 2022 20:45:24 +0800 Subject: ... --- .../Timeline.Tests/IntegratedTests/TimelineTest.cs | 1 - .../Timeline.Tests/Services/SearchServiceTest.cs | 6 +++--- BackEnd/Timeline/Controllers/TimelineController.cs | 2 +- .../Timeline/Services/Timeline/ITimelineService.cs | 4 ++-- .../Timeline/Services/Timeline/TimelineService.cs | 25 +++++++++++----------- 5 files changed, 19 insertions(+), 19 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs index d7832b60..72553248 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -383,7 +383,6 @@ namespace Timeline.Tests.IntegratedTests using (var client = await CreateClientAsUser()) { await client.TestPatchAssertInvalidModelAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "!!!" }); - await client.TestPatchAssertErrorAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "t2" }, errorCode: ErrorCodes.Conflict.Timeline); await client.TestPatchAsync("timelines/t1", new HttpTimelinePatchRequest { Name = "newt" }); diff --git a/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs b/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs index 6e3c0f40..9195ef4f 100644 --- a/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs +++ b/BackEnd/Timeline.Tests/Services/SearchServiceTest.cs @@ -19,10 +19,10 @@ namespace Timeline.Tests.Services [Fact] public async Task TimelineSearch_Should_Work() { - await TimelineService.CreateTimelineAsync("hahaha", UserId); - var t2 = await TimelineService.CreateTimelineAsync("bababa", UserId); + await TimelineService.CreateTimelineAsync(UserId, "hahaha"); + var t2 = await TimelineService.CreateTimelineAsync(UserId, "bababa"); await TimelineService.ChangePropertyAsync(t2.Id, new TimelineChangePropertyParams { Title = "hahaha" }); - await TimelineService.CreateTimelineAsync("bbbbbb", UserId); + await TimelineService.CreateTimelineAsync(UserId, "bbbbbb"); var searchResult = await _service.SearchTimelineAsync("hah"); searchResult.Items.Should().HaveCount(2); 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