Skip to content

Commit

Permalink
(#411) events: update create event hadnler to remove redundant try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 16, 2024
1 parent 0ca85e9 commit bad7360
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,157 +37,140 @@ public CreateEventHandler(IEventRepository eventRepository, IMessageBroker messa

public async Task HandleAsync(CreateEvent command, CancellationToken cancellationToken)
{
try
{
var options = new JsonSerializerOptions { WriteIndented = true };
var commandJson = JsonSerializer.Serialize(command, options);
Console.WriteLine("Received CreateEvent command: ");
Console.WriteLine(commandJson);
var options = new JsonSerializerOptions { WriteIndented = true };
var commandJson = JsonSerializer.Serialize(command, options);
var identity = _appContext.Identity;

var identity = _appContext.Identity;
// Validate Event ID
if (command.EventId == Guid.Empty || await _eventRepository.ExistsAsync(command.EventId))
{
throw new InvalidEventIdException(command.EventId);
}

// Validate Event ID
if (command.EventId == Guid.Empty || await _eventRepository.ExistsAsync(command.EventId))
{
throw new InvalidEventIdException(command.EventId);
}
// Validate Organizer Type
if (!Enum.TryParse<OrganizerType>(command.OrganizerType, true, out var organizerType))
{
throw new ArgumentException($"Invalid OrganizerType value: {command.OrganizerType}");
}

// Validate Organizer Type
if (!Enum.TryParse<OrganizerType>(command.OrganizerType, true, out var organizerType))
{
throw new ArgumentException($"Invalid OrganizerType value: {command.OrganizerType}");
}
// Validate Visibility
if (!Enum.TryParse<Visibility>(command.Visibility, true, out var visibility))
{
throw new ArgumentException($"Invalid Visibility value: {command.Visibility}");
}

// Validate Visibility
if (!Enum.TryParse<Visibility>(command.Visibility, true, out var visibility))
PaymentMethod? paymentMethod = null;
if (command.Settings != null && command.Settings.RequiresPayment && !string.IsNullOrWhiteSpace(command.Settings.PaymentMethod))
{
if (!Enum.TryParse<PaymentMethod>(command.Settings.PaymentMethod, true, out var parsedPaymentMethod))
{
throw new ArgumentException($"Invalid Visibility value: {command.Visibility}");
throw new ArgumentException($"Invalid PaymentMethod value: {command.Settings.PaymentMethod}");
}
paymentMethod = parsedPaymentMethod;
}

PaymentMethod? paymentMethod = null;
if (command.Settings != null && command.Settings.RequiresPayment && !string.IsNullOrWhiteSpace(command.Settings.PaymentMethod))
{
if (!Enum.TryParse<PaymentMethod>(command.Settings.PaymentMethod, true, out var parsedPaymentMethod))
{
throw new ArgumentException($"Invalid PaymentMethod value: {command.Settings.PaymentMethod}");
}
paymentMethod = parsedPaymentMethod;
}
_eventValidator.ValidateName(command.Name);
_eventValidator.ValidateDescription(command.Description);
var startDate = _eventValidator.ParseDate(command.StartDate, "event_start_date");
var endDate = _eventValidator.ParseDate(command.EndDate, "event_end_date");
var now = _dateTimeProvider.Now;
_eventValidator.ValidateDates(now, startDate, "now", "event_start_date");
_eventValidator.ValidateDates(startDate, endDate, "event_start_date", "event_end_date");

// Create Address object
var address = new Address(command.BuildingName, command.Street, command.BuildingNumber, command.ApartmentNumber, command.City, command.ZipCode, command.Country);

// Validate Capacity and Fee
_eventValidator.ValidateCapacity(command.Capacity);
_eventValidator.ValidateFee(command.Fee);

// Parse and Validate Category
var category = _eventValidator.ParseCategory(command.Category);

// Determine Publish Date and State
var publishDate = now;
var state = State.Published;
if (!string.IsNullOrEmpty(command.PublishDate))
{
publishDate = _eventValidator.ParseDate(command.PublishDate, "event_publish_date");
_eventValidator.ValidateDates(now, publishDate, "now", "event_publish_date");
_eventValidator.ValidateDates(publishDate, startDate, "event_publish_date", "event_start_date");
state = State.ToBePublished;
}

_eventValidator.ValidateName(command.Name);
_eventValidator.ValidateDescription(command.Description);
var startDate = _eventValidator.ParseDate(command.StartDate, "event_start_date");
var endDate = _eventValidator.ParseDate(command.EndDate, "event_end_date");
var now = _dateTimeProvider.Now;
_eventValidator.ValidateDates(now, startDate, "now", "event_start_date");
_eventValidator.ValidateDates(startDate, endDate, "event_start_date", "event_end_date");

// Create Address object
var address = new Address(command.BuildingName, command.Street, command.BuildingNumber, command.ApartmentNumber, command.City, command.ZipCode, command.Country);

// Validate Capacity and Fee
_eventValidator.ValidateCapacity(command.Capacity);
_eventValidator.ValidateFee(command.Fee);

// Parse and Validate Category
var category = _eventValidator.ParseCategory(command.Category);

// Determine Publish Date and State
var publishDate = now;
var state = State.Published;
if (!string.IsNullOrEmpty(command.PublishDate))
// Determine Organizer
Organizer organizer;
if (organizerType == OrganizerType.Organization)
{
if (command.OrganizationId == null)
{
publishDate = _eventValidator.ParseDate(command.PublishDate, "event_publish_date");
_eventValidator.ValidateDates(now, publishDate, "now", "event_publish_date");
_eventValidator.ValidateDates(publishDate, startDate, "event_publish_date", "event_start_date");
state = State.ToBePublished;
throw new ArgumentNullException(nameof(command.OrganizationId), "OrganizationId cannot be null for Organization-type events.");
}

// Determine Organizer
Organizer organizer;
if (organizerType == OrganizerType.Organization)
{
if (command.OrganizationId == null)
{
throw new ArgumentNullException(nameof(command.OrganizationId), "OrganizationId cannot be null for Organization-type events.");
}

var organization = await _organizationsServiceClient.GetAsync(command.OrganizationId.Value);
if (organization == null)
{
throw new OrganizationNotFoundException(command.OrganizationId.Value);
}

// Store both organization ID and the user ID creating the event
organizer = new Organizer(command.OrganizationId.Value, OrganizerType.Organization, userId: command.OrganizerId, organizationId: command.OrganizationId.Value);
}
else
var organization = await _organizationsServiceClient.GetAsync(command.OrganizationId.Value);
if (organization == null)
{
organizer = new Organizer(command.OrganizerId, OrganizerType.User, userId: command.OrganizerId);
throw new OrganizationNotFoundException(command.OrganizationId.Value);
}

var settings = command.Settings != null
? new EventSettings
{
RequiresApproval = command.Settings.RequiresApproval,
IsOnlineEvent = command.Settings.IsOnlineEvent,
IsPrivate = command.Settings.IsPrivate,
RequiresRSVP = command.Settings.RequiresRSVP,
AllowsGuests = command.Settings.AllowsGuests,
ShowAttendeesPublicly = command.Settings.ShowAttendeesPublicly,
SendReminders = command.Settings.SendReminders,
ReminderDaysBefore = command.Settings.ReminderDaysBefore,
EnableChat = command.Settings.EnableChat,
AllowComments = command.Settings.AllowComments,
RequiresPayment = command.Settings.RequiresPayment,
PaymentMethod = paymentMethod ?? PaymentMethod.Offline,
PaymentReceiverDetails = command.Settings.PaymentReceiverDetails,
PaymentGateway = command.Settings.PaymentGateway,
IssueTickets = command.Settings.IssueTickets,
MaxTicketsPerPerson = command.Settings.MaxTicketsPerPerson,
TicketPrice = command.Settings.TicketPrice,
RecordEvent = command.Settings.RecordEvent,
CustomTermsAndConditions = command.Settings.CustomTermsAndConditions,
CustomFields = command.Settings.CustomFields
}
: null;

var @event = Event.Create(
command.EventId,
command.Name,
command.Description,
organizer,
startDate,
endDate,
address,
command.MediaFilesUrl.ToList(),
command.BannerUrl,
command.Capacity,
command.Fee,
category,
state,
publishDate,
now,
visibility,
settings);

await _eventRepository.AddAsync(@event);
await _messageBroker.PublishAsync(new EventCreated(
@event.Id,
@event.Organizer.OrganizerType,
@event.Organizer.Id,
@event.MediaFiles));
}
catch (ArgumentException argEx)
{
Console.WriteLine($"Validation error: {argEx.Message}");
throw;
organizer = new Organizer(command.OrganizationId.Value, OrganizerType.Organization, userId: command.OrganizerId, organizationId: command.OrganizationId.Value);
}
catch (Exception ex)
else
{
Console.WriteLine($"Unhandled exception: {ex.Message}");
throw;
organizer = new Organizer(command.OrganizerId, OrganizerType.User, userId: command.OrganizerId);
}

var settings = command.Settings != null
? new EventSettings
{
RequiresApproval = command.Settings.RequiresApproval,
IsOnlineEvent = command.Settings.IsOnlineEvent,
IsPrivate = command.Settings.IsPrivate,
RequiresRSVP = command.Settings.RequiresRSVP,
AllowsGuests = command.Settings.AllowsGuests,
ShowAttendeesPublicly = command.Settings.ShowAttendeesPublicly,
SendReminders = command.Settings.SendReminders,
ReminderDaysBefore = command.Settings.ReminderDaysBefore,
EnableChat = command.Settings.EnableChat,
AllowComments = command.Settings.AllowComments,
RequiresPayment = command.Settings.RequiresPayment,
PaymentMethod = paymentMethod ?? PaymentMethod.Offline,
PaymentReceiverDetails = command.Settings.PaymentReceiverDetails,
PaymentGateway = command.Settings.PaymentGateway,
IssueTickets = command.Settings.IssueTickets,
MaxTicketsPerPerson = command.Settings.MaxTicketsPerPerson,
TicketPrice = command.Settings.TicketPrice,
RecordEvent = command.Settings.RecordEvent,
CustomTermsAndConditions = command.Settings.CustomTermsAndConditions,
CustomFields = command.Settings.CustomFields
}
: null;

var @event = Event.Create(
command.EventId,
command.Name,
command.Description,
organizer,
startDate,
endDate,
address,
command.MediaFilesUrl.ToList(),
command.BannerUrl,
command.Capacity,
command.Fee,
category,
state,
publishDate,
now,
visibility,
settings);

await _eventRepository.AddAsync(@event);
await _messageBroker.PublishAsync(new EventCreated(
@event.Id,
@event.Organizer.OrganizerType,
@event.Organizer.Id,
@event.MediaFiles));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace MiniSpace.Services.Events.Application.Events
{
public class EventCreated(Guid eventId, OrganizerType organizerType, Guid organizerId, IEnumerable<string> mediaFilesIds) : IEvent
public class EventCreated(Guid eventId, OrganizerType organizerType, Guid organizerId, IEnumerable<string> mediaFilesUrls) : IEvent
{
public Guid EventId { get; set; } = eventId;
public OrganizerType OrganizerType { get; set; } = organizerType;
public Guid OrganizerId { get; set; } = organizerId;
public IEnumerable<string> MediaFilesIds { get; set; } = mediaFilesIds;
public IEnumerable<string> MediaFilesUrls { get; set; } = mediaFilesUrls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public async Task HandleAsync(CommentCreated @event, CancellationToken cancellat
{
WriteIndented = true // Optional: For pretty-printing the JSON
});
Console.WriteLine("Received CommentCreated event:");
Console.WriteLine(eventJson);


var comment = new Comment(
@event.CommentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public async Task HandleAsync(ReactionCreated @event, CancellationToken cancella
{
WriteIndented = true
});
Console.WriteLine("Received ReactionCreated event:");
Console.WriteLine(eventJson);

var reaction = Reaction.Create(
@event.ReactionId,
Expand Down

0 comments on commit bad7360

Please sign in to comment.