Skip to content

Commit

Permalink
add: Delete a Spixer.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronvasconcelos committed Feb 25, 2024
1 parent 6f22e33 commit 84d82c8
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Spix.Api/Controllers/SpixerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Spix.Application.Spixers.Create;
using Spix.Application.Spixers.Delete;
using Spix.Application.Spixers.Like;
using Spix.Application.Spixers.Unlike;

Expand All @@ -22,6 +23,10 @@ public SpixerController(IMediator mediator)
[Route("[action]")]
public async Task<IActionResult> CreateSpixer([FromBody] CreateSpixerCommand command)
{
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var response = await _mediator.Send(command);
return Ok(response);
}
Expand All @@ -30,6 +35,11 @@ public async Task<IActionResult> CreateSpixer([FromBody] CreateSpixerCommand com
[Route("[action]")]
public async Task<IActionResult> LikeSpixer([FromBody] LikeASpixerCommand command)
{

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var response = await _mediator.Send(command);
return Ok(response);
}
Expand All @@ -38,6 +48,26 @@ public async Task<IActionResult> LikeSpixer([FromBody] LikeASpixerCommand comman
[Route("[action]")]
public async Task<IActionResult> UnlikeSpixer([FromBody] UnlikeASpixerCommand command)
{

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var response = await _mediator.Send(command);
return Ok(response);
}

[HttpDelete]
[Route("[action]")]
public async Task<IActionResult> DeleteSpixer([FromBody] Guid guid)
{

if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}

var command = new DeleteSpixerCommand(Guid.NewGuid(), guid);
var response = await _mediator.Send(command);
return Ok(response);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Spix.Application/Spixers/Delete/DeleteSpixerCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Spix.Application.Core;

namespace Spix.Application.Spixers.Delete;

public class DeleteSpixerCommand : ICommandBase<DeleteSpixerResponse>
{
public Guid UserId { get; set; }
public Guid SpixerId { get; set; }

public DeleteSpixerCommand(Guid userId, Guid spixerId)
{
UserId = userId;
SpixerId = spixerId;
}
}
37 changes: 37 additions & 0 deletions src/Spix.Application/Spixers/Delete/DeleteSpixerCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Spix.Application.Core;
using Spix.Domain.Spixers;

namespace Spix.Application.Spixers.Delete;

public class DeleteSpixerCommandHandler : ICommandHandlerBase<DeleteSpixerCommand, DeleteSpixerResponse>
{
private readonly ISpixerRepository _spixerRepository;

public DeleteSpixerCommandHandler(ISpixerRepository spixerRepository)
{
_spixerRepository = spixerRepository;
}

public async Task<ResultBase<DeleteSpixerResponse>> Handle(DeleteSpixerCommand request, CancellationToken cancellationToken)
{

var spixer = await _spixerRepository.GetByIdAsync(request.SpixerId);
if (spixer == null)
{
return ResultBaseFactory.Failure<DeleteSpixerResponse>("Spixer not found");
}

if (spixer.UserId != request.UserId)
{
return ResultBaseFactory.Failure<DeleteSpixerResponse>("User not authorized to delete this spixer");
}

spixer.Delete();
await _spixerRepository.UpdateAsync(spixer);
if (!await _spixerRepository.UnitOfWork.CommitAsync(cancellationToken))
{
return ResultBaseFactory.Failure<DeleteSpixerResponse>("Error deleting spixer");
}
return ResultBaseFactory.Successful(new DeleteSpixerResponse { DeletedAt = DateTime.UtcNow }, "Success");
}
}
6 changes: 6 additions & 0 deletions src/Spix.Application/Spixers/Delete/DeleteSpixerResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Spix.Application.Spixers.Delete;

public class DeleteSpixerResponse
{
public DateTime DeletedAt { get; set; }
}
4 changes: 4 additions & 0 deletions src/Spix.Domain/Spixers/Spixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void Unlike(SpixerLike like)
}


public void Delete()
{
Active = false;
}
public Spixer(string content, Guid userId)
{
SetContent(content);
Expand Down
File renamed without changes.

0 comments on commit 84d82c8

Please sign in to comment.