Skip to content

Commit

Permalink
Merge pull request #141 from SaintAngeLs/mediafiles_service
Browse files Browse the repository at this point in the history
Mediafiles service
  • Loading branch information
eggwhat authored May 19, 2024
2 parents 9656552 + 4698c51 commit f9d98de
Show file tree
Hide file tree
Showing 186 changed files with 3,736 additions and 150 deletions.
35 changes: 33 additions & 2 deletions MiniSpace.APIGateway/src/MiniSpace.APIGateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,44 @@ modules:
downstream: posts-service/posts/{postId}
auth: true


services:
posts-service:
localUrl: localhost:5013
url: posts-service




mediafiles:
path: /media-files
routes:
- upstream: /
method: POST
use: downstream
downstream: mediafiles-service/media-files
auth: true

- upstream: /{mediaFileId}
method: GET
use: downstream
downstream: mediafiles-service/media-files/{mediaFileId}

- upstream: /{mediaFileId}/original
method: GET
use: downstream
downstream: mediafiles-service/media-files/{mediaFileId}/original

- upstream: /{mediaFileId}
method: DELETE
use: downstream
downstream: mediafiles-service/media-files/{mediaFileId}
auth: true

services:
mediafiles-service:
localUrl: localhost:5014
url: mediafiles-service



organizations:
path: /organizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static async Task Main(string[] args)
.UseDispatcherEndpoints(endpoints => endpoints
.Get<GetEvent, EventDto>("events/{eventId}")
.Put<UpdateEvent>("events/{eventId}")
.Post<AddEvent>("events",
.Post<CreateEvent>("events",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"events/{cmd.EventId}"))
.Delete<DeleteEvent>("events/{eventId}")
.Post<SignUpToEvent>("events/{eventId}/sign-up")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Convey.CQRS.Commands;
using MiniSpace.Services.Events.Core.Entities;

namespace MiniSpace.Services.Events.Application.Commands
{
public class AddEvent : ICommand
public class CreateEvent : ICommand
{
public Guid EventId { get; }
public string Name { get; }
Expand All @@ -19,18 +21,19 @@ public class AddEvent : ICommand
public string ApartmentNumber { get; }
public string City { get; }
public string ZipCode { get; }
public IEnumerable<Guid> MediaFiles { get; }
public string Description { get; }
public int Capacity { get; }
public decimal Fee { get; }
public string Category { get; }
public string PublishDate { get; }

public AddEvent(Guid eventId, string name, Guid organizerId, Guid organizationId, Guid rootOrganizationId,
string startDate, string endDate, string buildingName, string street, string buildingNumber,
string apartmentNumber, string city, string zipCode, string description, int capacity, decimal fee,
string category, string publishDate)
public CreateEvent(Guid eventId, string name, Guid organizerId, Guid organizationId, Guid rootOrganizationId,
string startDate, string endDate, string buildingName, string street, string buildingNumber,
string apartmentNumber, string city, string zipCode, IEnumerable<Guid> mediaFiles, string description,
int capacity, decimal fee, string category, string publishDate)
{
EventId = eventId == Guid.Empty ? Guid.NewGuid() : eventId;
EventId = eventId;
Name = name;
OrganizerId = organizerId;
OrganizationId = organizationId;
Expand All @@ -43,6 +46,7 @@ public AddEvent(Guid eventId, string name, Guid organizerId, Guid organizationId
ApartmentNumber = apartmentNumber;
City = city;
ZipCode = zipCode;
MediaFiles = mediaFiles;
Description = description;
Capacity = capacity;
Fee = fee;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace MiniSpace.Services.Events.Application.Commands.Handlers
{
public class AddEventHandler: ICommandHandler<AddEvent>
public class CreateEventHandler: ICommandHandler<CreateEvent>
{
private readonly IEventRepository _eventRepository;
private readonly IMessageBroker _messageBroker;
Expand All @@ -21,7 +21,7 @@ public class AddEventHandler: ICommandHandler<AddEvent>
private readonly IEventValidator _eventValidator;
private readonly IAppContext _appContext;

public AddEventHandler(IEventRepository eventRepository, IMessageBroker messageBroker,
public CreateEventHandler(IEventRepository eventRepository, IMessageBroker messageBroker,
IOrganizationsServiceClient organizationsServiceClient, IDateTimeProvider dateTimeProvider,
IEventValidator eventValidator, IAppContext appContext)
{
Expand All @@ -33,13 +33,18 @@ public AddEventHandler(IEventRepository eventRepository, IMessageBroker messageB
_appContext = appContext;
}

public async Task HandleAsync(AddEvent command, CancellationToken cancellationToken)
public async Task HandleAsync(CreateEvent command, CancellationToken cancellationToken)
{
var identity = _appContext.Identity;
if (!identity.IsOrganizer)
throw new AuthorizedUserIsNotAnOrganizerException(identity.Id);
if(identity.Id != command.OrganizerId)
throw new OrganizerCannotAddEventForAnotherOrganizerException(identity.Id, command.OrganizerId);

if (command.EventId == Guid.Empty || await _eventRepository.ExistsAsync(command.EventId))
{
throw new InvalidEventIdException(command.EventId);
}

_eventValidator.ValidateName(command.Name);
_eventValidator.ValidateDescription(command.Description);
Expand All @@ -50,6 +55,7 @@ public async Task HandleAsync(AddEvent command, CancellationToken cancellationTo
_eventValidator.ValidateDates(startDate, endDate, "event_start_date", "event_end_date");
var address = new Address(command.BuildingName, command.Street, command.BuildingNumber,
command.ApartmentNumber, command.City, command.ZipCode);
_eventValidator.ValidateMediaFiles(command.MediaFiles.ToList());
_eventValidator.ValidateCapacity(command.Capacity);
_eventValidator.ValidateFee(command.Fee);
var category = _eventValidator.ParseCategory(command.Category);
Expand Down Expand Up @@ -77,10 +83,10 @@ public async Task HandleAsync(AddEvent command, CancellationToken cancellationTo

var organizer = new Organizer(command.OrganizerId, identity.Name, identity.Email, command.OrganizationId, organization.Name);
var @event = Event.Create(command.EventId, command.Name, command.Description, startDate, endDate,
address, command.Capacity, command.Fee, category, state, publishDate, organizer, now);
address, command.MediaFiles, command.Capacity, command.Fee, category, state, publishDate, organizer, now);

await _eventRepository.AddAsync(@event);
await _messageBroker.PublishAsync(new EventCreated(@event.Id, @event.Organizer.Id));
await _messageBroker.PublishAsync(new EventCreated(@event.Id, @event.Organizer.Id, @event.MediaFiles));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Commands;
using MiniSpace.Services.Events.Application.Events;
Expand Down Expand Up @@ -54,6 +55,7 @@ public async Task HandleAsync(UpdateEvent command, CancellationToken cancellatio

var address = @event.Location.Update(command.BuildingName, command.Street, command.BuildingNumber,
command.ApartmentNumber, command.City, command.ZipCode);
_eventValidator.ValidateMediaFiles(command.MediaFiles.ToList());
var capacity = command.Capacity == 0 ? @event.Capacity : command.Capacity;
_eventValidator.ValidateUpdatedCapacity(capacity, @event.Capacity);
var fee = command.Fee == 0 ? @event.Fee : command.Fee;
Expand All @@ -71,7 +73,8 @@ public async Task HandleAsync(UpdateEvent command, CancellationToken cancellatio

@event.Update(name, description, startDate, endDate, address, capacity, fee, category, state, publishDate, now);
await _eventRepository.UpdateAsync(@event);
await _messageBroker.PublishAsync(new EventUpdated(@event.Id, _dateTimeProvider.Now, identity.Id));
await _messageBroker.PublishAsync(new EventUpdated(@event.Id, _dateTimeProvider.Now,
identity.Id, @event.MediaFiles));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Convey.CQRS.Commands;

namespace MiniSpace.Services.Events.Application.Commands
Expand All @@ -16,6 +18,7 @@ public class UpdateEvent : ICommand
public string ApartmentNumber { get; }
public string City { get; }
public string ZipCode { get; }
public IEnumerable<Guid> MediaFiles { get; }
public string Description { get; }
public int Capacity { get; }
public decimal Fee { get; }
Expand All @@ -24,7 +27,8 @@ public class UpdateEvent : ICommand

public UpdateEvent(Guid eventId, string name, Guid organizerId, string startDate, string endDate,
string buildingName, string street, string buildingNumber, string apartmentNumber, string city,
string zipCode, string description, int capacity, decimal fee, string category, string publishDate)
string zipCode, IEnumerable<Guid> mediaFiles, string description, int capacity, decimal fee,
string category, string publishDate)
{
EventId = eventId;
Name = name;
Expand All @@ -37,6 +41,7 @@ public UpdateEvent(Guid eventId, string name, Guid organizerId, string startDate
ApartmentNumber = apartmentNumber;
City = city;
ZipCode = zipCode;
MediaFiles = mediaFiles;
Description = description;
Capacity = capacity;
Fee = fee;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class EventDto
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public AddressDto Location { get; set; }
//public string Image { get; set; }
public IEnumerable<Guid> MediaFiles { get; set; }
public int InterestedStudents { get; set; }
public int SignedUpStudents { get; set; }
public int Capacity { get; set; }
Expand Down Expand Up @@ -43,6 +43,7 @@ public EventDto(Event @event, Guid studentId)
StartDate = @event.StartDate;
EndDate = @event.EndDate;
Location = new AddressDto(@event.Location);
MediaFiles = @event.MediaFiles;
InterestedStudents = @event.InterestedStudents.Count();
SignedUpStudents = @event.SignedUpStudents.Count();
Capacity = @event.Capacity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using Convey.CQRS.Events;

namespace MiniSpace.Services.Events.Application.Events
{
public class EventCreated(Guid eventId, Guid organizerId) : IEvent
public class EventCreated(Guid eventId, Guid organizerId, IEnumerable<Guid> mediaFilesIds) : IEvent
{
public Guid EventId { get; set; } = eventId;
public Guid OrganizerId { get; set; } = organizerId;
public IEnumerable<Guid> MediaFilesIds { get; set; } = mediaFilesIds;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Convey.CQRS.Events;

namespace MiniSpace.Services.Events.Application.Events
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Convey.CQRS.Events;

namespace MiniSpace.Services.Events.Application.Events
{
public class EventUpdated(Guid eventId, DateTime updatedAt, Guid updatedBy) : IEvent
public class EventUpdated(Guid eventId, DateTime updatedAt, Guid updatedBy, IEnumerable<Guid> mediaFilesIds) : IEvent
{
public Guid EventId { get; set; } = eventId;
public DateTime UpdatedAt { get; set; } = updatedAt;
public Guid UpdatedBy { get; set; } = updatedBy;
public IEnumerable<Guid> MediaFilesIds { get; set; } = mediaFilesIds;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Events;
using MiniSpace.Services.Events.Core.Repositories;

namespace MiniSpace.Services.Events.Application.Events.External.Handlers
{
public class MediaFileDeletedHandler: IEventHandler<MediaFileDeleted>
{
private readonly IEventRepository _eventRepository;

public MediaFileDeletedHandler(IEventRepository eventRepository)
{
_eventRepository = eventRepository;
}

public async Task HandleAsync(MediaFileDeleted @event, CancellationToken cancellationToken)
{
if(@event.Source.ToLowerInvariant() != "event")
{
return;
}

var foundEvent = await _eventRepository.GetAsync(@event.SourceId);
if(foundEvent != null)
{
foundEvent.RemoveMediaFile(@event.MediaFileId);
await _eventRepository.UpdateAsync(foundEvent);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Convey.CQRS.Events;
using Convey.MessageBrokers;

namespace MiniSpace.Services.Events.Application.Events.External
{
[Message("mediafiles")]
public class MediaFileDeleted: IEvent
{
public Guid MediaFileId { get; }
public Guid SourceId { get; }
public string Source { get; }

public MediaFileDeleted(Guid mediaFileId, Guid sourceId, string source)
{
MediaFileId = mediaFileId;
SourceId = sourceId;
Source = source;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace MiniSpace.Services.Events.Application.Events.Rejected
{
public class AddEventRejected(Guid organizerId, string reason, string code) : IRejectedEvent
public class CreateEventRejected(Guid organizerId, string reason, string code) : IRejectedEvent
{
public Guid OrganizerId { get; } = organizerId;
public string Reason { get; } = reason;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace MiniSpace.Services.Events.Application.Exceptions
{
public class InvalidEventIdException : AppException
{
public override string Code { get; } = "invalid_event_id";
public Guid EventId { get; }

public InvalidEventIdException(Guid eventId) : base($"Invalid event id: {eventId}.")
{
EventId = eventId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace MiniSpace.Services.Events.Application.Exceptions
{
public class InvalidNumberOfEventMediaFilesException : AppException
{
public override string Code { get; } = "invalid_number_of_event_media_files";
public int MediaFilesNumber { get; }
public InvalidNumberOfEventMediaFilesException(int mediaFilesNumber)
: base($"Invalid media files number: {mediaFilesNumber}. It must be less or equal 5.")
{
MediaFilesNumber = mediaFilesNumber;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using MiniSpace.Services.Events.Core.Entities;

namespace MiniSpace.Services.Events.Application.Services
Expand All @@ -13,6 +14,7 @@ public interface IEventValidator
(int pageNumber, int pageSize) PageFilter(int pageNumber, int pageSize);
void ValidateName(string name);
void ValidateDescription(string description);
void ValidateMediaFiles(List<Guid> mediaFiles);
void ValidateCapacity(int capacity);
void ValidateFee(decimal fee);
void ValidateUpdatedCapacity(int currentCapacity, int newCapacity);
Expand Down
Loading

0 comments on commit f9d98de

Please sign in to comment.