-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from sentemon/implement-application-and-infras…
…tructure-logic-file-service Implement application and infrastructure logic file service
- Loading branch information
Showing
81 changed files
with
1,135 additions
and
450 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
backend/src/FileService/FileService.Application/Commands/DeletePost/DeletePostCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using Shared.Application.Abstractions; | ||
|
||
namespace FileService.Application.Commands.DeletePost; | ||
|
||
public record DeletePostCommand(Guid PostId) : ICommand; |
37 changes: 37 additions & 0 deletions
37
...d/src/FileService/FileService.Application/Commands/DeletePost/DeletePostCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using FileService.Domain.Constants; | ||
using FileService.Infrastructure.Interfaces; | ||
using FileService.Persistence; | ||
using Microsoft.EntityFrameworkCore; | ||
using Shared.Application.Abstractions; | ||
using Shared.Application.Common; | ||
|
||
namespace FileService.Application.Commands.DeletePost; | ||
|
||
public class DeletePostCommandHandler : ICommandHandler<DeletePostCommand, string> | ||
{ | ||
private readonly IAzureBlobStorageService _azureBlobStorageService; | ||
private readonly FileDbContext _context; | ||
|
||
public DeletePostCommandHandler(IAzureBlobStorageService azureBlobStorageService, FileDbContext context) | ||
{ | ||
_azureBlobStorageService = azureBlobStorageService; | ||
_context = context; | ||
} | ||
|
||
public async Task<IResult<string, Error>> HandleAsync(DeletePostCommand command) | ||
{ | ||
var file = await _context.Files.FirstOrDefaultAsync(f => f.ForeignEntityId == command.PostId); | ||
|
||
if (file == null) | ||
{ | ||
return Result<string>.Failure(new Error(ResponseMessages.FileNotFound)); | ||
} | ||
|
||
await _azureBlobStorageService.DeleteAsync(file.BlobName, file.BlobContainerName); | ||
|
||
_context.Remove(file); | ||
await _context.SaveChangesAsync(); | ||
|
||
return Result<string>.Success(ResponseMessages.YouSuccessfullyDeletedFile); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/FileService/FileService.Application/Commands/UploadPost/UploadPostCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
using FileService.Application.DTOs; | ||
using Shared.Application.Abstractions; | ||
|
||
namespace FileService.Application.Commands.UploadPost; | ||
|
||
public record UploadPostCommand(UploadPostFileDto UploadPostFileDto, string? UserId) : ICommand; |
72 changes: 72 additions & 0 deletions
72
...d/src/FileService/FileService.Application/Commands/UploadPost/UploadPostCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using FileService.Domain.Constants; | ||
using FileService.Infrastructure.Interfaces; | ||
using FileService.Persistence; | ||
using MassTransit; | ||
using Microsoft.AspNetCore.Http; | ||
using Shared.Application.Common; | ||
using Shared.Application.Abstractions; | ||
using Shared.DTO.Messages; | ||
using File = FileService.Domain.Entities.File; | ||
|
||
namespace FileService.Application.Commands.UploadPost; | ||
|
||
public class UploadPostCommandHandler : ICommandHandler<UploadPostCommand, File> | ||
{ | ||
private readonly IAzureBlobStorageService _azureBlobStorageService; | ||
private readonly FileDbContext _context; | ||
private readonly IPublishEndpoint _publishEndpoint; | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public UploadPostCommandHandler(IAzureBlobStorageService azureBlobStorageService, FileDbContext context, IPublishEndpoint publishEndpoint, IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_azureBlobStorageService = azureBlobStorageService; | ||
_context = context; | ||
_publishEndpoint = publishEndpoint; | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public async Task<IResult<File, Error>> HandleAsync(UploadPostCommand command) | ||
{ | ||
if (command.UserId == null) | ||
{ | ||
return Result<File>.Failure(new Error(ResponseMessages.UserIdIsNull)); | ||
} | ||
|
||
if (command.UploadPostFileDto.FileStream == null) | ||
{ | ||
return Result<File>.Failure(new Error(ResponseMessages.FileStreamIsNull)); | ||
} | ||
|
||
if (command.UploadPostFileDto.ContentType == null) | ||
{ | ||
return Result<File>.Failure(new Error(ResponseMessages.ContentTypeNull)); | ||
} | ||
|
||
var blobName = await _azureBlobStorageService.UploadAsync( | ||
BlobContainerNamesConstants.PostPhotos, | ||
command.UploadPostFileDto.FileStream, | ||
command.UploadPostFileDto.ContentType | ||
); | ||
|
||
var file = File.CreateFile( | ||
blobName, | ||
BlobContainerNamesConstants.PostPhotos, | ||
command.UploadPostFileDto.FileStream.Length, | ||
command.UserId, | ||
command.UploadPostFileDto.PostId | ||
); | ||
|
||
_context.Add(file); | ||
await _context.SaveChangesAsync(); | ||
|
||
var request = _httpContextAccessor.HttpContext?.Request; | ||
var host = $"{request?.Scheme}://{request?.Host}"; | ||
|
||
await _publishEndpoint.Publish(new PostUploadedEventMessage( | ||
file.ForeignEntityId, | ||
$"{host}/file/files/{blobName}" | ||
)); | ||
|
||
return Result<File>.Success(file); | ||
} | ||
} |
Oops, something went wrong.