Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of events controller #2809

Draft
wants to merge 1 commit into
base: event-tweaks-new
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public interface IEventUserAccessService
{
Task<bool> IsUserAdminOfEvent(User user, int eventId);

Task<string> GetEventStatusAsync(User user, int eventId);

Task<bool> CanPostFeedback(Participant user, int eventId);

Task<bool> CanDeleteFeedback(User user, EventFeedback feedback);
Expand Down
7 changes: 5 additions & 2 deletions EPlast/EPlast.BLL/Interfaces/Events/IActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using EPlast.BLL.DTO.Events;
using EPlast.BLL.DTO.EventUser;
using EPlast.DataAccess.Entities;
using EPlast.DataAccess.Entities.Event;
using Microsoft.AspNetCore.Http;

namespace EPlast.BLL.Interfaces.Events
Expand Down Expand Up @@ -53,6 +54,8 @@ public interface IActionManager
/// <param name="user">ClaimsPrincipal of logged in user</param>
Task<IEnumerable<GeneralEventDto>> GetEventsAsync(int categoryId, int eventTypeId, User user);

Task<Event> GetEventAsync(int eventId);

/// <summary>
/// Get detailed information about event by event Id.
/// </summary>
Expand Down Expand Up @@ -105,7 +108,7 @@ public interface IActionManager
/// <param name="eventId">The Id of event</param>
/// <param name="user">User object</param>
/// <param name="feedback">Feedback object</param>
Task<int> LeaveFeedbackAsync(int eventId, EventFeedbackDto feedback, User user);
Task LeaveFeedbackAsync( EventFeedbackDto feedback, Participant participant);

/// <summary>
/// Delete a feedback for the participant's event.
Expand All @@ -114,7 +117,7 @@ public interface IActionManager
/// <param name="eventId">The Id of event</param>
/// <param name="feedbackId">Feedback Id</param>
/// <param name="user">User object</param>
Task<int> DeleteFeedbackAsync(int eventId, int feedbackId, User user);
Task DeleteFeedbackAsync(int feedbackId);

/// <summary>
/// Change event participant status to approved.
Expand Down
4 changes: 4 additions & 0 deletions EPlast/EPlast.BLL/Interfaces/Events/IParticipantManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public interface IParticipantManager
/// <param name="userId">The Id of logged in user</param>
Task<IEnumerable<Participant>> GetParticipantsByUserIdAsync(string userId);

Task<Participant> GetParticipantByEventIdAndUserIdAsync(int eventId, string userId);

Task<EventFeedback> GetEventFeedbackByIdAsync(int feedbackId);

/// <summary>
/// Change present status of the participant's event.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,14 @@ public async Task<bool> CanDeleteFeedback(User user, EventFeedback feedback)
return true;
}

public async Task<string> GetEventStatusAsync(User user, int eventId)
{
var eventDetails = await _actionManager.GetEventInfoAsync(eventId, user);
return eventDetails.Event.EventStatus;
}

public async Task<Dictionary<string, bool>> RedefineAccessesAsync(Dictionary<string, bool> userAccesses, User user, int? eventId = null)
{
if (eventId == null) return userAccesses;

bool access = await IsUserAdminOfEvent(user, (int)eventId);
var roles = await _userManager.GetRolesAsync(user);
var eventStatus = await GetEventStatusAsync(user, (int)eventId);
var eventStatus = (await _actionManager.GetEventInfoAsync( (int)eventId, user)).Event.EventStatus;

userAccesses["SubscribeOnEvent"] = !access;

Expand Down
50 changes: 17 additions & 33 deletions EPlast/EPlast.BLL/Services/Events/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class ActionManager : IActionManager
private readonly IParticipantManager _participantManager;
private readonly IEventWrapper _eventWrapper;
private readonly INotificationService _notificationService;
private readonly IEventUserAccessService _eventUserAccessService;

public ActionManager(UserManager<User> userManager, IRepositoryWrapper repoWrapper, IMapper mapper,
IParticipantStatusManager participantStatusManager, IParticipantManager participantManager,
IEventWrapper eventWrapper, INotificationService notificationService, IEventUserAccessService eventUserAccessService)
IEventWrapper eventWrapper, INotificationService notificationService)
{
_userManager = userManager;
_repoWrapper = repoWrapper;
Expand All @@ -40,7 +40,6 @@ public ActionManager(UserManager<User> userManager, IRepositoryWrapper repoWrapp
_participantManager = participantManager;
_eventWrapper = eventWrapper;
_notificationService = notificationService;
_eventUserAccessService = eventUserAccessService;
}

/// <inheritdoc />
Expand Down Expand Up @@ -94,6 +93,12 @@ public async Task<IEnumerable<GeneralEventDto>> GetEventsAsync(int categoryId, i
return await GetEventDtosAsync(events, user);
}

public async Task<Event> GetEventAsync(int eventId)
{
var eventEntity = await _repoWrapper.Event.GetFirstOrDefaultAsync(e => e.ID == eventId);
return eventEntity;
}

/// <inheritdoc />
public async Task<EventDto> GetEventInfoAsync(int id, User user)
{
Expand Down Expand Up @@ -234,18 +239,8 @@ public async Task ChangeUsersPresentStatusAsync(int participantId)
await _participantManager.ChangeUserPresentStatusAsync(participantId);
}

public async Task<int> LeaveFeedbackAsync(int eventId, EventFeedbackDto feedback, User user)
public async Task LeaveFeedbackAsync(EventFeedbackDto feedback, Participant participant)
{
var eventEntity = await _repoWrapper.Event.GetFirstOrDefaultAsync(e => e.ID == eventId);

if (eventEntity == null) return StatusCodes.Status404NotFound;

var participant =
await _repoWrapper.Participant.GetFirstOrDefaultAsync(e => e.EventId == eventId && e.UserId == user.Id);

var canPostFeedback = await _eventUserAccessService.CanPostFeedback(participant, eventId);
if (!canPostFeedback) return StatusCodes.Status403Forbidden;

var existingFeedback = await _repoWrapper.EventFeedback.GetFirstOrDefaultAsync(f => f.ParticipantId == participant.ID);

if (existingFeedback != null)
Expand All @@ -255,7 +250,7 @@ public async Task<int> LeaveFeedbackAsync(int eventId, EventFeedbackDto feedback

_repoWrapper.EventFeedback.Update(existingFeedback);
await _repoWrapper.SaveAsync();
return StatusCodes.Status200OK;
return;
}

var createdFeedback = _mapper.Map<EventFeedbackDto, EventFeedback>(feedback);
Expand All @@ -264,28 +259,17 @@ public async Task<int> LeaveFeedbackAsync(int eventId, EventFeedbackDto feedback

await _repoWrapper.EventFeedback.CreateAsync(createdFeedback);
await _repoWrapper.SaveAsync();

return StatusCodes.Status200OK;
}

public async Task<int> DeleteFeedbackAsync(int eventId, int feedbackId, User user)
public async Task DeleteFeedbackAsync(int feedbackId)
{
var eventEntity = await _repoWrapper.Event.GetFirstOrDefaultAsync(e => e.ID == eventId);
if (eventEntity == null) return StatusCodes.Status404NotFound;

var feedback = await _repoWrapper.EventFeedback
.GetFirstOrDefaultAsync(e => e.Id == feedbackId && e.Participant.EventId == eventId,
include: e => e.Include(f => f.Participant));

if (feedback == null) return StatusCodes.Status404NotFound;

bool canDelete = await _eventUserAccessService.CanDeleteFeedback(user, feedback);
if (!canDelete) return StatusCodes.Status403Forbidden;

_repoWrapper.EventFeedback.Delete(feedback);
await _repoWrapper.SaveAsync();
var eventFeedback = await _repoWrapper.EventFeedback.GetFirstOrDefaultAsync(f => f.Id == feedbackId);
if (eventFeedback != null)
{
_repoWrapper.EventFeedback.Delete(eventFeedback);
await _repoWrapper.SaveAsync();

return StatusCodes.Status200OK;
}
}

/// <inheritdoc />
Expand Down
14 changes: 14 additions & 0 deletions EPlast/EPlast.BLL/Services/Events/ParticipantManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ public async Task<IEnumerable<Participant>> GetParticipantsByUserIdAsync(string
return participants;
}

public async Task<Participant> GetParticipantByEventIdAndUserIdAsync(int eventId, string userId)
{
var participant =
await _repoWrapper.Participant.GetFirstOrDefaultAsync(e => e.EventId == eventId && e.UserId == userId);
return participant;
}

public async Task<EventFeedback> GetEventFeedbackByIdAsync(int feedbackId)
{
var feedback =
await _repoWrapper.EventFeedback.GetFirstOrDefaultAsync(f=>f.Id==feedbackId);
return feedback;
}

public async Task ChangeUserPresentStatusAsync(int perticipantId)
{
var participant = await _repoWrapper.Participant
Expand Down
Loading