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

#342 #343 mediafile service, organization serivce events handlers update #345

Merged
merged 67 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
2c8acc3
Merge pull request #186 from SaintAngeLs/dev
SaintAngeLs May 26, 2024
37e6be1
Merge pull request #238 from SaintAngeLs/dev
SaintAngeLs Jun 2, 2024
049eaa0
Merge pull request #268 from SaintAngeLs/dev
SaintAngeLs Jun 2, 2024
738d77a
Merge pull request #272 from SaintAngeLs/dev
SaintAngeLs Jun 18, 2024
1d4c5d0
Merge pull request #275 from SaintAngeLs/dev
SaintAngeLs Jun 18, 2024
da4960a
Merge pull request #277 from SaintAngeLs/dev
SaintAngeLs Jun 18, 2024
8633299
Merge pull request #280 from SaintAngeLs/dev
SaintAngeLs Jun 22, 2024
1c0ddef
Merge pull request #309 from SaintAngeLs/dev
SaintAngeLs Jul 21, 2024
19cfbea
Merge pull request #313 from SaintAngeLs/dev
SaintAngeLs Jul 21, 2024
ec32ec4
Merge pull request #316 from SaintAngeLs/dev
SaintAngeLs Jul 22, 2024
b6d47dd
Merge pull request #320 from SaintAngeLs/dev
SaintAngeLs Jul 22, 2024
0606f0e
Merge pull request #322 from SaintAngeLs/dev
SaintAngeLs Jul 22, 2024
f653d92
Merge pull request #324 from SaintAngeLs/dev
SaintAngeLs Jul 22, 2024
aec992d
Merge pull request #327 from SaintAngeLs/dev
SaintAngeLs Jul 22, 2024
51e4e3c
Bump SixLabors.ImageSharp
dependabot[bot] Jul 22, 2024
762e278
Merge pull request #328 from SaintAngeLs/dependabot/nuget/MiniSpace.S…
SaintAngeLs Jul 22, 2024
62efb45
(#330) udpate schemas
SaintAngeLs Aug 2, 2024
9296c58
Merge remote-tracking branches 'origin' and 'remotes/origin/dev' into…
SaintAngeLs Aug 2, 2024
4372752
(#342) add edit organisation view
SaintAngeLs Aug 3, 2024
3f10bf0
(#342) add external event organization image uploaded
SaintAngeLs Aug 3, 2024
e893b6f
(#342) update external events subscribers
SaintAngeLs Aug 3, 2024
3c5afa5
(#342) update organizations core
SaintAngeLs Aug 3, 2024
af95616
(#342) implement external evnet handler for organization image uploaded
SaintAngeLs Aug 3, 2024
64b9fff
(#342) add nullable organization id to uploade media file command
SaintAngeLs Aug 3, 2024
5d1391a
(#342) add organization id to medifiles service handlers
SaintAngeLs Aug 3, 2024
6ae40ad
(#342) add upload organization image to mediafiles service via blazor…
SaintAngeLs Aug 3, 2024
c6d63fa
(#342) update upload image method via organization view to mediafiles…
SaintAngeLs Aug 3, 2024
9220ace
(#342) blazor application update the mediafiles service post image up…
SaintAngeLs Aug 3, 2024
68ba656
(#342) fix: spasing styles issue
SaintAngeLs Aug 3, 2024
03a5a0d
(#342) update media files service mediafileDeleted handler
SaintAngeLs Aug 3, 2024
e19b3d2
(#342) update fileSourceInfo entity
SaintAngeLs Aug 3, 2024
de36605
(#342) web fix: cropper file update
SaintAngeLs Aug 3, 2024
d035380
(#342) update file source info document
SaintAngeLs Aug 3, 2024
b662efd
(#342) update IFileSOurceInfoRepostory
SaintAngeLs Aug 3, 2024
3c973d2
(#342) mediafile service infrastructure update repository implementat…
SaintAngeLs Aug 3, 2024
f0b953e
(#342) organizations: add mediafiles deleted external event
SaintAngeLs Aug 3, 2024
be8792b
(#342) mediafiles: udpate delete media file handler
SaintAngeLs Aug 3, 2024
a018767
(#342) organizations: update mondo enxtensions
SaintAngeLs Aug 3, 2024
a3505c4
(#342) oranizations: add main envent handler for image deleted handler
SaintAngeLs Aug 3, 2024
bb1a9db
(#342) organizations: add remove image abstract method
SaintAngeLs Aug 3, 2024
4ab398a
(#342) organizations: add remove image repository entity method
SaintAngeLs Aug 3, 2024
571a69b
(#342) update delete mediaFile command handler
SaintAngeLs Aug 3, 2024
1f7d72b
(#342) orzanizations: add organizationId? to mongo object
SaintAngeLs Aug 3, 2024
c3c7f0f
(#342) mediafiles: add organizationId to sourceInfo
SaintAngeLs Aug 3, 2024
d17af20
(#342) oranizations: debug mediaFileDeleted external evnet handler
SaintAngeLs Aug 3, 2024
94b835f
(#342) web fix: profile compoment update
SaintAngeLs Aug 3, 2024
9a2f472
(#342) organizations: update getOrganisationWithGalleryAndUsers handl…
SaintAngeLs Aug 3, 2024
a3ee789
(#342) web organizations: update organization settings razor compoment
SaintAngeLs Aug 3, 2024
ea929e8
(#342) web organizations: add all available settings frm dto to razor…
SaintAngeLs Aug 3, 2024
088cf61
(#342) oranizations: update organization create command handler
SaintAngeLs Aug 3, 2024
f95d918
(#342) organizations: udpate core user and role permissions
SaintAngeLs Aug 3, 2024
ed12beb
(#342) organizations: update infrastructure extensions to register or…
SaintAngeLs Aug 3, 2024
d4260aa
(#342) organizations: roles repository implementation
SaintAngeLs Aug 3, 2024
24a3f51
(#342) organizations: register transient roles repository
SaintAngeLs Aug 3, 2024
0764747
(#342) organizations: udpate organization roles repository interface
SaintAngeLs Aug 3, 2024
05c2f9a
(#342) oranizations: udpate create role handler
SaintAngeLs Aug 3, 2024
4aeeea7
(#342) oranizations: udpate assign role to memeber command handler fo…
SaintAngeLs Aug 3, 2024
9eedcaf
(#342) organizations: update organization roles repository interface
SaintAngeLs Aug 3, 2024
2c1e65b
(#342) organizations: add role entry document
SaintAngeLs Aug 3, 2024
6a3b949
(#342) organizations: udpate implementation of role core class
SaintAngeLs Aug 3, 2024
f2be659
(#342) web organizations: update organization service and galeeries
SaintAngeLs Aug 3, 2024
16963ee
(#342) oranizations: udpate updateOrganizationSettingsHandler
SaintAngeLs Aug 3, 2024
ec0d435
(#342) organizations: udpate user core class
SaintAngeLs Aug 3, 2024
6fdf53d
(#342) organizations: udpate user core class
SaintAngeLs Aug 3, 2024
0b5d609
(#342) oranizations: add permissions check to UpdateOrganizaion comma…
SaintAngeLs Aug 3, 2024
46414c5
(#342) oranizations: add permissions check for create organization role
SaintAngeLs Aug 3, 2024
a95f1ac
(#342) oranizations: create suborganization add permissions check
SaintAngeLs Aug 3, 2024
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
@@ -1,4 +1,5 @@
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Convey.CQRS.Commands;
Expand Down Expand Up @@ -27,30 +28,45 @@ public DeleteMediaFileHandler(IFileSourceInfoRepository fileSourceInfoRepository

public async Task HandleAsync(DeleteMediaFile command, CancellationToken cancellationToken)
{
// Serialize the command to JSON and write to the console
var commandJson = JsonSerializer.Serialize(command, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("Received DeleteMediaFile command: " + commandJson);

// Decode the URL before using it
var decodedUrl = Uri.UnescapeDataString(command.MediaFileUrl);

Console.WriteLine($"DeleteMediaFileHandler: {decodedUrl}");

// Retrieve the file source information
var fileSourceInfo = await _fileSourceInfoRepository.GetAsync(decodedUrl);
if (fileSourceInfo is null)
{
throw new MediaFileNotFoundException(decodedUrl);
}

// Log the fileSourceInfo details to ensure it contains the correct data
SaintAngeLs marked this conversation as resolved.
Show resolved Hide resolved
var fileSourceInfoJson = JsonSerializer.Serialize(fileSourceInfo, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("Retrieved FileSourceInfo: " + fileSourceInfoJson);

// Check for unauthorized access
var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != fileSourceInfo.UploaderId)
if (identity.IsAuthenticated && identity.Id != fileSourceInfo.UploaderId &&
!fileSourceInfo.OrganizationId.HasValue)
{
throw new UnauthorizedMediaFileAccessException(fileSourceInfo.Id, identity.Id, fileSourceInfo.UploaderId);
}

// Delete the files from S3
await _s3Service.DeleteFileAsync(fileSourceInfo.OriginalFileUrl);
await _s3Service.DeleteFileAsync(fileSourceInfo.FileUrl);
await _fileSourceInfoRepository.DeleteAsync(decodedUrl);

// Publish the MediaFileDeleted event
await _messageBroker.PublishAsync(new MediaFileDeleted(
decodedUrl,
fileSourceInfo.SourceId,
fileSourceInfo.SourceType.ToString(),
fileSourceInfo.UploaderId));
fileSourceInfo.UploaderId,
fileSourceInfo.OrganizationId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ public class UploadMediaFile : ICommand
public Guid MediaFileId { get; set; }
public Guid SourceId { get; set; }
public string SourceType { get; set; }
public Guid? OrganizationId { get; set; }
public Guid UploaderId { get; set; }
public string FileName { get; set; }
public string FileContentType { get; set; }
public byte[] FileData { get; set; }
public byte[] FileData { get; set; }

public UploadMediaFile(Guid mediaFileId, Guid sourceId, string sourceType, Guid uploaderId,
string fileName, string fileContentType, byte[] fileData)
public UploadMediaFile(Guid mediaFileId, Guid sourceId, string sourceType, Guid? organizationId,
Guid uploaderId, string fileName, string fileContentType, byte[] fileData)
{
MediaFileId = mediaFileId == Guid.Empty ? Guid.NewGuid() : mediaFileId;
SourceId = sourceId;
SourceType = sourceType;
OrganizationId = organizationId;
UploaderId = uploaderId;
FileName = fileName;
FileContentType = fileContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Convey.CQRS.Events;
using System;

namespace MiniSpace.Services.MediaFiles.Application.Events
{
Expand All @@ -8,13 +9,16 @@ public class MediaFileDeleted : IEvent
public Guid SourceId { get; }
public string Source { get; }
public Guid UploaderId { get; }
public Guid? OrganizationId { get; }

public MediaFileDeleted(string mediaFileUrl, Guid sourceId, string source, Guid uploaderId)
public MediaFileDeleted(string mediaFileUrl, Guid sourceId, string source,
Guid uploaderId, Guid? organizationId)
{
MediaFileUrl = mediaFileUrl;
SourceId = sourceId;
Source = source;
UploaderId = uploaderId;
OrganizationId = organizationId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Convey.CQRS.Queries" Version="1.1.448" />
<PackageReference Include="Convey.MessageBrokers" Version="1.1.448" />
<PackageReference Include="MongoDB.Bson" Version="2.25.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class FileSourceInfo : AggregateRoot
public string OriginalFileContentType { get; set; }
public string FileUrl { get; set; }
public string FileName { get; set; }

public Guid? OrganizationId { get; set; }

public FileSourceInfo(Guid id, Guid sourceId, ContextType sourceType, Guid uploaderId, State state,
DateTime createdAt, string originalFileUrl, string originalFileContentType, string fileUrl, string fileName)
DateTime createdAt, string originalFileUrl, string originalFileContentType, string fileUrl, string fileName,
Guid? organizationId = null)
{
Id = id;
SourceId = sourceId;
Expand All @@ -27,13 +29,15 @@ public FileSourceInfo(Guid id, Guid sourceId, ContextType sourceType, Guid uploa
OriginalFileContentType = originalFileContentType;
FileUrl = fileUrl;
FileName = fileName;
OrganizationId = organizationId;
}



public void Associate()
{
State = State.Associated;
}

public void Unassociate()
{
State = State.Unassociated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public interface IFileSourceInfoRepository
Task<IEnumerable<FileSourceInfo>> FindByUploaderIdAndSourceTypeAsync(Guid uploaderId, ContextType sourceType);
Task<IEnumerable<FileSourceInfo>> GetAllAsync(string url);
Task DeleteAllAsync(string url);
Task<IEnumerable<FileSourceInfo>> FindByOrganizationIdAsync(Guid organizationId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static FileSourceInfo AsEntity(this FileSourceInfoDocument document)
document.OriginalFileUrl,
document.OriginalFileContentType,
document.FileUrl,
document.FileName
document.FileName,
document.OrganizationId
);

public static FileSourceInfoDocument AsDocument(this FileSourceInfo entity)
Expand All @@ -32,7 +33,9 @@ public static FileSourceInfoDocument AsDocument(this FileSourceInfo entity)
OriginalFileUrl = entity.OriginalFileUrl,
OriginalFileContentType = entity.OriginalFileContentType,
FileUrl = entity.FileUrl,
FileName = entity.FileName
FileName = entity.FileName,
OrganizationId = entity.OrganizationId
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class FileSourceInfoDocument : IIdentifiable<Guid>
public string OriginalFileContentType { get; set; }
public string FileUrl { get; set; }
public string FileName { get; set; }
public Guid? OrganizationId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,12 @@ public async Task<IEnumerable<FileSourceInfo>> FindByUploaderIdAndSourceTypeAsyn
var fileSourceInfos = await _repository.FindAsync(s => s.UploaderId == uploaderId && s.SourceType == sourceType);
return fileSourceInfos?.Select(s => s.AsEntity());
}

// Optional: Implement method to find files by OrganizationId
public async Task<IEnumerable<FileSourceInfo>> FindByOrganizationIdAsync(Guid organizationId)
{
var fileSourceInfos = await _repository.FindAsync(s => s.OrganizationId == organizationId);
return fileSourceInfos?.Select(s => s.AsEntity());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace MiniSpace.Services.MediaFiles.Infrastructure.Services
Expand Down Expand Up @@ -41,6 +42,21 @@ public MediaFilesService(IFileSourceInfoRepository fileSourceInfoRepository, IFi

public async Task<FileUploadResponseDto> UploadAsync(UploadMediaFile command)
{
var commandWithoutFileData = new UploadMediaFile(
command.MediaFileId,
command.SourceId,
command.SourceType,
command.OrganizationId,
command.UploaderId,
command.FileName,
command.FileContentType,
null // Exclude the FileData
);

// Serialize the modified command to JSON and write to the console
var commandJson = JsonSerializer.Serialize(commandWithoutFileData, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("Received UploadMediaFile command (excluding FileData): " + commandJson);

var identity = _appContext.Identity;
if (identity.IsAuthenticated && identity.Id != command.UploaderId)
{
Expand All @@ -52,6 +68,7 @@ public async Task<FileUploadResponseDto> UploadAsync(UploadMediaFile command)
throw new InvalidContextTypeException(command.SourceType);
}

// Handle previous files if necessary
if (sourceType == ContextType.StudentProfileImage ||
sourceType == ContextType.StudentBannerImage ||
sourceType == ContextType.OrganizationProfileImage ||
Expand Down Expand Up @@ -102,28 +119,25 @@ public async Task<FileUploadResponseDto> UploadAsync(UploadMediaFile command)
var uploadDate = _dateTimeProvider.Now;
var fileSourceInfo = new FileSourceInfo(command.MediaFileId, command.SourceId, sourceType,
command.UploaderId, State.Associated, uploadDate, originalUrl,
command.FileContentType, processedUrl, originalFileName);
command.FileContentType, processedUrl, originalFileName, command.OrganizationId);

await _fileSourceInfoRepository.AddAsync(fileSourceInfo);
await _messageBroker.PublishAsync(new MediaFileUploaded(command.MediaFileId, originalFileName));

// Handle specific events based on the source type
if (sourceType == ContextType.StudentProfileImage ||
sourceType == ContextType.StudentBannerImage ||
sourceType == ContextType.StudentGalleryImage)
// Handle specific events based on the source type and organization
if (command.OrganizationId.HasValue)
{
var imageType = sourceType.ToString();
var studentImageUploadedEvent = new StudentImageUploaded(command.UploaderId, processedUrl, imageType, uploadDate);
await _messageBroker.PublishAsync(studentImageUploadedEvent);
var organizationImageUploadedEvent = new OrganizationImageUploaded(command.OrganizationId.Value, processedUrl, imageType, uploadDate);
await _messageBroker.PublishAsync(organizationImageUploadedEvent);
}

if (sourceType == ContextType.OrganizationProfileImage ||
sourceType == ContextType.OrganizationBannerImage ||
sourceType == ContextType.OrganizationGalleryImage)
else if (sourceType == ContextType.StudentProfileImage ||
sourceType == ContextType.StudentBannerImage ||
sourceType == ContextType.StudentGalleryImage)
{
var imageType = sourceType.ToString();
var organizationImageUploadedEvent = new OrganizationImageUploaded(command.UploaderId, processedUrl, imageType, uploadDate);
await _messageBroker.PublishAsync(organizationImageUploadedEvent);
var studentImageUploadedEvent = new StudentImageUploaded(command.UploaderId, processedUrl, imageType, uploadDate);
await _messageBroker.PublishAsync(studentImageUploadedEvent);
}

return new FileUploadResponseDto(fileSourceInfo.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public async Task HandleAsync(AssignRoleToMember command, CancellationToken canc
throw new MemberNotFoundException(command.MemberId);
}

// Fetch the role by its name
var existingRole = await _organizationRolesRepository.GetRoleByNameAsync(command.Role);
// Fetch the role by its name using the correct method signature
var existingRole = await _organizationRolesRepository.GetRoleByNameAsync(command.OrganizationId, command.Role);
if (existingRole == null)
{
throw new RoleNotFoundException(command.Role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MiniSpace.Services.Organizations.Core.Entities;
using MiniSpace.Services.Organizations.Core.Repositories;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -13,12 +14,24 @@ namespace MiniSpace.Services.Organizations.Application.Commands.Handlers
public class CreateOrganizationHandler : ICommandHandler<CreateOrganization>
{
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationMembersRepository _organizationMembersRepository;
private readonly IOrganizationGalleryRepository _organizationGalleryRepository;
private readonly IOrganizationRolesRepository _organizationRolesRepository;
private readonly IAppContext _appContext;
private readonly IMessageBroker _messageBroker;

public CreateOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext, IMessageBroker messageBroker)
public CreateOrganizationHandler(
IOrganizationRepository organizationRepository,
IOrganizationMembersRepository organizationMembersRepository,
IOrganizationGalleryRepository organizationGalleryRepository,
IOrganizationRolesRepository organizationRolesRepository,
IAppContext appContext,
IMessageBroker messageBroker)
{
_organizationRepository = organizationRepository;
_organizationMembersRepository = organizationMembersRepository;
_organizationGalleryRepository = organizationGalleryRepository;
_organizationRolesRepository = organizationRolesRepository;
_appContext = appContext;
_messageBroker = messageBroker;
}
Expand Down Expand Up @@ -48,14 +61,6 @@ public async Task HandleAsync(CreateOrganization command, CancellationToken canc
);

await _organizationRepository.AddAsync(organization);
await _messageBroker.PublishAsync(new OrganizationCreated(
organization.Id,
organization.Name,
organization.Description,
organization.Id, // Root ID is the organization's own ID
null, // No parent ID
command.OwnerId,
DateTime.UtcNow));
}
else
{
Expand Down Expand Up @@ -85,15 +90,35 @@ await _messageBroker.PublishAsync(new OrganizationCreated(

parent.AddSubOrganization(organization);
await _organizationRepository.UpdateAsync(root);
await _messageBroker.PublishAsync(new OrganizationCreated(
organization.Id,
organization.Name,
organization.Description,
command.RootId.Value,
command.ParentId.Value,
command.OwnerId,
DateTime.UtcNow));
}

var defaultRoles = organization.Roles.ToList();
foreach (var role in defaultRoles)
{
await _organizationRolesRepository.AddRoleAsync(organization.Id, role);
}

// Initialize an empty gallery for the organization
await _organizationGalleryRepository.AddImageAsync(organization.Id, new GalleryImage(Guid.NewGuid(), "Default Image URL", DateTime.UtcNow));

// Add the creator as a member with the "Creator" role
var creatorRole = defaultRoles.SingleOrDefault(r => r.Name == "Creator");
if (creatorRole == null)
{
throw new RoleNotFoundException("Creator");
}

var creatorMember = new User(identity.Id, creatorRole);
await _organizationMembersRepository.AddMemberAsync(organization.Id, creatorMember);

await _messageBroker.PublishAsync(new OrganizationCreated(
organization.Id,
organization.Name,
organization.Description,
command.RootId ?? organization.Id, // Root ID is the organization's own ID or the provided root ID
command.ParentId,
command.OwnerId,
DateTime.UtcNow));
}
}
}
Loading
Loading