From 8be166185139b691ec53537115c5902caf3c5eb4 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 24 Feb 2020 16:52:19 +0800 Subject: Add delete timeline. --- Timeline/Controllers/TimelineController.cs | 20 ++++++++++++++++++++ Timeline/Services/TimelineService.cs | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) (limited to 'Timeline') diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 9ada16e0..85ccb5c1 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -206,5 +206,25 @@ namespace Timeline.Controllers return BadRequest(ErrorResponse.TimelineCommon.NameConflict()); } } + + [HttpDelete("timelines/{name}")] + [Authorize] + public async Task> 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()); + } + } } } diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 76acc7d7..f12a7fcc 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -269,6 +269,15 @@ namespace Timeline.Services /// Thrown when the timeline already exists. /// Thrown when the owner user does not exist. Task CreateTimeline(string name, long owner); + + /// + /// Delete a timeline. + /// + /// The name of the timeline. + /// Thrown when is null. + /// Thrown when timeline name is invalid. + /// Thrown when the timeline does not exist. + Task DeleteTimeline(string name); } public interface IPersonalTimelineService : IBaseTimelineService @@ -733,6 +742,22 @@ namespace Timeline.Services Members = new List() }; } + + 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 -- cgit v1.2.3