Skip to content

Commit

Permalink
Add Activity Delete Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fatihkabakk committed May 8, 2022
1 parent 820c180 commit b126563
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions API/Controllers/ActivitiesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public async Task<IActionResult> EditActivity(Guid id, Activity activity)
activity.Id = id;
return Ok(await Mediator.Send(new Edit.Command { Activity = activity }));
}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteActivity(Guid id)
{
return Ok(await Mediator.Send(new Delete.Command { Id = id }));
}
}
}
37 changes: 37 additions & 0 deletions Application/Activities/Delete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Persistence;

namespace Application.Activities
{
public class Delete
{
public class Command : IRequest
{
public Guid Id { get; set; }
}

public class Handler : IRequestHandler<Command>
{
private readonly DataContext _context;
public Handler(DataContext context)
{
_context = context;
}

public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
{
var activity = await _context.Activities.FindAsync(request.Id);

_context.Remove(activity);
await _context.SaveChangesAsync();

return Unit.Value;
}
}
}
}

0 comments on commit b126563

Please sign in to comment.