From 1e808c1de47cd3853bb7912976cc7af04164aa5f Mon Sep 17 00:00:00 2001 From: FranchescoSoto Date: Mon, 13 Nov 2023 17:18:41 -0500 Subject: [PATCH] feat: add delete features --- .../DeleteParticipantCommandHandler.cs | 43 +++++++++++++++++++ .../Service/ParticipantCommandService.cs | 21 +++++++++ .../commands/DeleteArtEventCommandHandler.cs | 40 +++++++++++++++++ ...sterParticipantToArtEventCommandHandler.cs | 3 +- .../artevents/service/ArtEventService.cs | 4 ++ .../Domain/Model/Aggregates/Participant.cs | 1 + .../Model/Commads/DeleteArtEventCommand.cs | 9 ++++ .../Model/Commads/DeleteParticipantCommand.cs | 9 ++++ .../ArtEvent/IArtEventCommandService.cs | 1 + .../Participant/IParticipantCommandService.cs | 10 +++++ .../Interfaces/rest/ArtEventController.cs | 9 ++++ .../Interfaces/rest/ParticipantController.cs | 17 ++++++-- PERUSTARS/PERUSTARS/Startup.cs | 3 +- 13 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/DeleteParticipantCommandHandler.cs create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/Service/ParticipantCommandService.cs create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/DeleteArtEventCommandHandler.cs create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteArtEventCommand.cs create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteParticipantCommand.cs create mode 100644 PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/Participant/IParticipantCommandService.cs diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/DeleteParticipantCommandHandler.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/DeleteParticipantCommandHandler.cs new file mode 100644 index 0000000..744d900 --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/DeleteParticipantCommandHandler.cs @@ -0,0 +1,43 @@ +using MediatR; +using PERUSTARS.AtEventManagement.Domain.Model.Aggregates; +using PERUSTARS.AtEventManagement.Domain.Model.Commads; +using PERUSTARS.AtEventManagement.Domain.Model.Repositories; +using PERUSTARS.Shared.Domain.Repositories; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace PERUSTARS.AtEventManagement.Application.Participant.Command +{ + public class DeleteParticipantCommandHandler:IRequestHandler + { + private readonly IParticipantRepository _participantRepository; + private readonly IUnitOfWork _unitOfWork; + public DeleteParticipantCommandHandler(IParticipantRepository participantRepository, IUnitOfWork unitOfWork) + { + _participantRepository = participantRepository; + _unitOfWork = unitOfWork; + } + + public async Task Handle(DeleteParticipantCommand request, CancellationToken cancellationToken) + { + try + { + PERUSTARS.AtEventManagement.Domain.Model.Aggregates.Participant p = _participantRepository.FindByIdAsync(request.id).Result; + if (p != null) + { + _participantRepository.Remove(p); + await _unitOfWork.CompleteAsync(); + return "Participant deleted"; + } + else { + return "Participant with the given Id doesn't exist"; + } + } + catch (Exception e) { + throw new ApplicationException($"An error occurred while deleting the participant: {e.Message}"); + } + + } + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/Service/ParticipantCommandService.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/Service/ParticipantCommandService.cs new file mode 100644 index 0000000..5abede1 --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Application/Participant/Command/Service/ParticipantCommandService.cs @@ -0,0 +1,21 @@ +using MediatR; +using PERUSTARS.AtEventManagement.Domain.Model.Commads; +using PERUSTARS.AtEventManagement.Domain.Services.Participant; +using System.Threading.Tasks; + +namespace PERUSTARS.AtEventManagement.Application.Participant.Command.Service +{ + public class ParticipantCommandService:IParticipantCommandService + { + private readonly IMediator _mediator; + public ParticipantCommandService(IMediator mediator) + { + _mediator = mediator; + } + + public async Task deleteParticipant(DeleteParticipantCommand deleteParticipantCommand) + { + return await _mediator.Send(deleteParticipantCommand); + } + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/DeleteArtEventCommandHandler.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/DeleteArtEventCommandHandler.cs new file mode 100644 index 0000000..3d77b3e --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/DeleteArtEventCommandHandler.cs @@ -0,0 +1,40 @@ +using MediatR; +using PERUSTARS.AtEventManagement.Domain.Model.Aggregates; +using PERUSTARS.AtEventManagement.Domain.Model.Commads; +using PERUSTARS.AtEventManagement.Domain.Model.Repositories; +using PERUSTARS.Shared.Domain.Repositories; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace PERUSTARS.AtEventManagement.Application.artevents.commands +{ + public class DeleteArtEventCommandHandler : IRequestHandler + { + private readonly IArtEventRepository _artEventRepository; + private readonly IUnitOfWork _unitOfWork; + public DeleteArtEventCommandHandler(IArtEventRepository artEventRepository, IUnitOfWork unitOfWork) + { + _artEventRepository = artEventRepository; + _unitOfWork = unitOfWork; + } + + public async Task Handle(DeleteArtEventCommand request, CancellationToken cancellationToken) + { + try + { + ArtEvent artEvent= _artEventRepository.FindByIdAsync(request.id).Result; + if (artEvent != null) { + _artEventRepository.Remove(artEvent); + await _unitOfWork.CompleteAsync(); + + } + } + catch(Exception e) { + throw new ApplicationException($"An error occurred while deleting the art event: {e.Message}"); + + } + return "Art event deleted"; + } + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/RegisterParticipantToArtEventCommandHandler.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/RegisterParticipantToArtEventCommandHandler.cs index 030dc59..861c49f 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/RegisterParticipantToArtEventCommandHandler.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/commands/RegisterParticipantToArtEventCommandHandler.cs @@ -8,6 +8,7 @@ using PERUSTARS.Shared.Domain.Repositories; using System.Threading; using System.Threading.Tasks; +using System; namespace PERUSTARS.AtEventManagement.Application.artevents.commands { @@ -35,7 +36,7 @@ public async Task Handle(RegisterParticipantToArtEventCommand request, C id: 0, userName: "A", registerDateTime: new System.DateTime(), - checkInDateTime: null, + checkInDateTime: DateTime.UtcNow, hobbyistId: request.hobbyistId, artEventId: request.artEventId, hobbyist: hobbyist, diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/service/ArtEventService.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/service/ArtEventService.cs index d850772..d967402 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/service/ArtEventService.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Application/artevents/service/ArtEventService.cs @@ -50,5 +50,9 @@ public async Task startArtEventCommand(StartArtEventCommand startArtEven { return await _mediator.Send(startArtEventCommand); } + + public async Task deleteArtEvent(DeleteArtEventCommand deleteArtEventCommand) { + return await _mediator.Send(deleteArtEventCommand); + } } } diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Aggregates/Participant.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Aggregates/Participant.cs index c068999..571abb7 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Aggregates/Participant.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Aggregates/Participant.cs @@ -24,6 +24,7 @@ public Participant(long id, string userName, DateTime registerDateTime, DateTime UserName = userName; RegisterDateTime = registerDateTime; CheckInDateTime = checkInDateTime; + ParticipantRegistrationDateTime = DateTime.UtcNow; HobbyistId = hobbyistId; ArtEventId = artEventId; Hobyst = hobbyist; diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteArtEventCommand.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteArtEventCommand.cs new file mode 100644 index 0000000..4a4959e --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteArtEventCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace PERUSTARS.AtEventManagement.Domain.Model.Commads +{ + public class DeleteArtEventCommand:IRequest + { + public int id { get; set; } + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteParticipantCommand.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteParticipantCommand.cs new file mode 100644 index 0000000..94170b0 --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Model/Commads/DeleteParticipantCommand.cs @@ -0,0 +1,9 @@ +using MediatR; + +namespace PERUSTARS.AtEventManagement.Domain.Model.Commads +{ + public class DeleteParticipantCommand:IRequest + { + public int id { get; set; } + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/ArtEvent/IArtEventCommandService.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/ArtEvent/IArtEventCommandService.cs index 4eeb286..6166d16 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/ArtEvent/IArtEventCommandService.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/ArtEvent/IArtEventCommandService.cs @@ -12,5 +12,6 @@ public interface IArtEventCommandService Task finishArtEvent(FinishArtEventCommand finishArtEventCommand); Task rescheduleArtEvent(RescheduleArtEventCommand rescheduleArtEventCommand); Task startArtEventCommand(StartArtEventCommand startArtEventCommand); + Task deleteArtEvent(DeleteArtEventCommand deleteArtEventCommand); } } diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/Participant/IParticipantCommandService.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/Participant/IParticipantCommandService.cs new file mode 100644 index 0000000..148d203 --- /dev/null +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Domain/Services/Participant/IParticipantCommandService.cs @@ -0,0 +1,10 @@ +using PERUSTARS.AtEventManagement.Domain.Model.Commads; +using System.Threading.Tasks; + +namespace PERUSTARS.AtEventManagement.Domain.Services.Participant +{ + public interface IParticipantCommandService + { + Task deleteParticipant(DeleteParticipantCommand deleteParticipantCommand); + } +} diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ArtEventController.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ArtEventController.cs index 1fb9e61..4a3a0c5 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ArtEventController.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ArtEventController.cs @@ -77,5 +77,14 @@ public async Task createParticipant([FromBody] RegisterParticipan string response= await _artEventCommandService.registerParticipantToArtEvent(registerParticipantToArtEvent); return Ok(response); } + + [HttpDelete("id")] + public async Task deleteArtEvent(int id) { + DeleteArtEventCommand deleteArtEventCommand = new DeleteArtEventCommand(); + deleteArtEventCommand.id=id; + string response = await _artEventCommandService.deleteArtEvent(deleteArtEventCommand); + return Ok(response); + } + } } diff --git a/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ParticipantController.cs b/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ParticipantController.cs index 4674b0d..c20fabc 100644 --- a/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ParticipantController.cs +++ b/PERUSTARS/PERUSTARS/AtEventManagement/Interfaces/rest/ParticipantController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Metadata; using PERUSTARS.AtEventManagement.Domain.Model.Aggregates; +using PERUSTARS.AtEventManagement.Domain.Model.Commads; using PERUSTARS.AtEventManagement.Domain.Services.Participant; using System.Collections; using System.Collections.Generic; @@ -14,10 +15,12 @@ public class ParticipantController : ControllerBase { private readonly IMapper _mapper; private readonly IParticipantQueryService _participantQueryService; - public ParticipantController(IMapper mapper, IParticipantQueryService participantQueryService) + private readonly IParticipantCommandService _participantCommandService; + public ParticipantController(IMapper mapper, IParticipantQueryService participantQueryService, IParticipantCommandService participantCommandService) { _mapper = mapper; _participantQueryService = participantQueryService; + _participantCommandService=participantCommandService; } [HttpGet] @@ -27,17 +30,25 @@ public async Task getParticipants() return Ok(participants); } - [HttpGet("/hobbyist/{id}")] + [HttpGet("hobbyist/{id}")] public async Task getParticipantsByHobbyistId(int id) { IEnumerable participants = _participantQueryService.getParticipantByHobbyistId(id); return Ok(participants); } - [HttpGet("/artevent/{id}")] + [HttpGet("artevent/{id}")] public async Task getParticipantsByArtEventId(int id) { IEnumerable participants = _participantQueryService.getParticipantByEventId(id); return Ok(participants); } + + [HttpDelete("{id}")] + public async Task deleteParticipant(int id) { + DeleteParticipantCommand d = new DeleteParticipantCommand(); + d.id = id; + string response = _participantCommandService.deleteParticipant(d).Result; + return Ok(response); + } } } diff --git a/PERUSTARS/PERUSTARS/Startup.cs b/PERUSTARS/PERUSTARS/Startup.cs index bf39cfe..431b38b 100644 --- a/PERUSTARS/PERUSTARS/Startup.cs +++ b/PERUSTARS/PERUSTARS/Startup.cs @@ -48,6 +48,7 @@ using PERUSTARS.DataAnalytics.Domain.Services; using PERUSTARS.DataAnalytics.Application.Commands.Services; using Microsoft.Extensions.Logging; +using PERUSTARS.AtEventManagement.Application.Participant.Command.Service; namespace PERUSTARS { @@ -125,7 +126,7 @@ public void ConfigureServices(IServiceCollection services) services.AddScoped(); services.AddScoped(); services.AddScoped(); - + services.AddScoped(); services.AddScoped(); services.AddScoped();