Skip to content

Commit

Permalink
Merge pull request #61 from crupest/dev
Browse files Browse the repository at this point in the history
Add delete timeline feature.
  • Loading branch information
crupest authored Feb 24, 2020
2 parents fd09024 + fe44cef commit 227e901
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Timeline.Tests/IntegratedTests/TimelineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,62 @@ public async Task TimelineCreate_Should_Work()
}
}

[Fact]
public async Task TimelineDelete_Should_Work()
{
await CreateTestTimelines();

{
using var client = await CreateDefaultClient();
var res = await client.DeleteAsync("timelines/t1");
res.Should().HaveStatusCode(HttpStatusCode.Unauthorized);
}

{
using var client = await CreateClientAs(2);
var res = await client.DeleteAsync("timelines/t1");
res.Should().HaveStatusCode(HttpStatusCode.Forbidden);
}

{
using var client = await CreateClientAsAdministrator();

{
var res = await client.DeleteAsync("timelines/!!!");
res.Should().BeInvalidModel();
}

{
var res = await client.DeleteAsync("timelines/t2");
res.Should().BeDelete(true);
}

{
var res = await client.DeleteAsync("timelines/t2");
res.Should().BeDelete(false);
}
}

{
using var client = await CreateClientAs(1);

{
var res = await client.DeleteAsync("timelines/!!!");
res.Should().BeInvalidModel();
}

{
var res = await client.DeleteAsync("timelines/t1");
res.Should().BeDelete(true);
}

{
var res = await client.DeleteAsync("timelines/t1");
res.Should().HaveStatusCode(HttpStatusCode.NotFound);
}
}
}

[Fact]
public async Task InvalidModel_BadName()
{
Expand Down
20 changes: 20 additions & 0 deletions Timeline/Controllers/TimelineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,25 @@ public async Task<ActionResult<TimelineInfo>> TimelineCreate([FromBody] Timeline
return BadRequest(ErrorResponse.TimelineCommon.NameConflict());
}
}

[HttpDelete("timelines/{name}")]
[Authorize]
public async Task<ActionResult<TimelineInfo>> TimelineDelete([FromRoute][TimelineName] string name)
{
if (!this.IsAdministrator() && !(await _service.HasManagePermission(name, this.GetUserId())))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}

try
{
await _service.DeleteTimeline(name);
return Ok(CommonDeleteResponse.Delete());
}
catch (TimelineNotExistException)
{
return Ok(CommonDeleteResponse.NotExist());
}
}
}
}
25 changes: 25 additions & 0 deletions Timeline/Services/TimelineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ public interface ITimelineService : IBaseTimelineService
/// <exception cref="ConflictException">Thrown when the timeline already exists.</exception>
/// <exception cref="UserNotExistException">Thrown when the owner user does not exist.</exception>
Task<TimelineInfo> CreateTimeline(string name, long owner);

/// <summary>
/// Delete a timeline.
/// </summary>
/// <param name="name">The name of the timeline.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when timeline name is invalid.</exception>
/// <exception cref="TimelineNotExistException">Thrown when the timeline does not exist.</exception>
Task DeleteTimeline(string name);
}

public interface IPersonalTimelineService : IBaseTimelineService
Expand Down Expand Up @@ -733,6 +742,22 @@ public async Task<TimelineInfo> CreateTimeline(string name, long owner)
Members = new List<UserInfo>()
};
}

public async Task DeleteTimeline(string name)
{
if (name == null)
throw new ArgumentNullException(nameof(name));

ValidateTimelineName(name, nameof(name));

var entity = await Database.Timelines.Where(t => t.Name == name).SingleOrDefaultAsync();

if (entity == null)
throw new TimelineNotExistException(name);

Database.Timelines.Remove(entity);
await Database.SaveChangesAsync();
}
}

public class PersonalTimelineService : BaseTimelineService, IPersonalTimelineService
Expand Down

0 comments on commit 227e901

Please sign in to comment.