-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: burst <burstsangels@gmail.com>
- Loading branch information
1 parent
723ebcc
commit e7812c8
Showing
41 changed files
with
806 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base | ||
WORKDIR /app | ||
EXPOSE 80 | ||
EXPOSE 443 | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build | ||
WORKDIR /src | ||
COPY ["Resources.WebApi/Resources.WebApi.csproj", "Resources.WebApi/"] | ||
COPY ["Infrastructure/Infrastructure.csproj", "Infrastructure/"] | ||
RUN dotnet restore "Resources.WebApi/Resources.WebApi.csproj" | ||
COPY . . | ||
WORKDIR "/src/Resources.WebApi" | ||
RUN dotnet build "Resources.WebApi.csproj" -c Release -o /app/build | ||
|
||
FROM build AS publish | ||
RUN dotnet publish "Resources.WebApi.csproj" -c Release -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "Resources.WebApi.dll"] |
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,55 @@ | ||
using AutoMapper; | ||
using Grpc.Core; | ||
using Infrastructure.Exceptions; | ||
using Resources.WebApi.Models.Images; | ||
using Resources.WebApi.Services; | ||
|
||
namespace Resources.WebApi.GrpcServices; | ||
|
||
public class GrpcImagesService : Images.ImagesBase | ||
{ | ||
private readonly ImagesService _imagesService; | ||
private readonly IMapper _mapper; | ||
|
||
public GrpcImagesService( | ||
ImagesService imagesService, | ||
IMapper mapper) | ||
{ | ||
_imagesService = imagesService; | ||
_mapper = mapper; | ||
} | ||
|
||
public override async Task<GetMessageImagesResponse> GetByMessageId( | ||
GetMessageImagesRequest request, | ||
ServerCallContext _) | ||
{ | ||
Guid messageId = Guid.Parse(request.MessageId); | ||
IEnumerable<ImageView> imageViews; | ||
try | ||
{ | ||
imageViews = await _imagesService.GetByMessageIdAsync(messageId); | ||
} | ||
catch (NotFoundException exception) | ||
{ | ||
throw new RpcException(new Status(StatusCode.NotFound, exception.Message)); | ||
} | ||
var response = new GetMessageImagesResponse(); | ||
response.Images.AddRange(_mapper.Map<IEnumerable<ImageResponse>>(imageViews)); | ||
return response; | ||
} | ||
|
||
public override async Task<Empty> AddToMessage(AddToMessageImagesRequest request, ServerCallContext _) | ||
{ | ||
Guid messageId = Guid.Parse(request.MessageId); | ||
var imageCreates = _mapper.Map<IEnumerable<ImageCreate>>(request.Images); | ||
try | ||
{ | ||
await _imagesService.AddToMessageAsync(messageId, imageCreates); | ||
} | ||
catch (BadRequestException exception) | ||
{ | ||
throw new RpcException(new Status(StatusCode.InvalidArgument, exception.Message)); | ||
} | ||
return new Empty(); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Resources.WebApi/Migrations/20230828082440_Migration28082023.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
Resources.WebApi/Migrations/20230828082440_Migration28082023.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,60 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace Resources.WebApi.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class Migration28082023 : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Images", | ||
columns: table => new | ||
{ | ||
Id = table.Column<Guid>(type: "uuid", nullable: false), | ||
Content = table.Column<byte[]>(type: "bytea", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Images", x => x.Id); | ||
}); | ||
|
||
migrationBuilder.CreateTable( | ||
name: "ImagesMessages", | ||
columns: table => new | ||
{ | ||
MessageId = table.Column<Guid>(type: "uuid", nullable: false), | ||
ImageId = table.Column<Guid>(type: "uuid", nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_ImagesMessages", x => new { x.MessageId, x.ImageId }); | ||
table.ForeignKey( | ||
name: "FK_ImagesMessages_Images_ImageId", | ||
column: x => x.ImageId, | ||
principalTable: "Images", | ||
principalColumn: "Id", | ||
onDelete: ReferentialAction.Cascade); | ||
}); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_ImagesMessages_ImageId", | ||
table: "ImagesMessages", | ||
column: "ImageId"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "ImagesMessages"); | ||
|
||
migrationBuilder.DropTable( | ||
name: "Images"); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
Resources.WebApi/Migrations/ApplicationContextModelSnapshot.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,67 @@ | ||
// <auto-generated /> | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; | ||
using Resources.WebApi.Repositories; | ||
|
||
#nullable disable | ||
|
||
namespace Resources.WebApi.Migrations | ||
{ | ||
[DbContext(typeof(ApplicationContext))] | ||
partial class ApplicationContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "7.0.10") | ||
.HasAnnotation("Relational:MaxIdentifierLength", 63); | ||
|
||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); | ||
|
||
modelBuilder.Entity("Resources.WebApi.Models.Images.Image", b => | ||
{ | ||
b.Property<Guid>("Id") | ||
.HasColumnType("uuid"); | ||
b.Property<byte[]>("Content") | ||
.IsRequired() | ||
.HasColumnType("bytea"); | ||
b.HasKey("Id"); | ||
b.ToTable("Images"); | ||
}); | ||
|
||
modelBuilder.Entity("Resources.WebApi.Models.Images.ImageMessage", b => | ||
{ | ||
b.Property<Guid>("MessageId") | ||
.HasColumnType("uuid"); | ||
b.Property<Guid>("ImageId") | ||
.HasColumnType("uuid"); | ||
b.HasKey("MessageId", "ImageId"); | ||
b.HasIndex("ImageId"); | ||
b.ToTable("ImagesMessages"); | ||
}); | ||
|
||
modelBuilder.Entity("Resources.WebApi.Models.Images.ImageMessage", b => | ||
{ | ||
b.HasOne("Resources.WebApi.Models.Images.Image", "Image") | ||
.WithMany() | ||
.HasForeignKey("ImageId") | ||
.OnDelete(DeleteBehavior.Cascade) | ||
.IsRequired(); | ||
b.Navigation("Image"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
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,10 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace Resources.WebApi.Models.Images; | ||
|
||
public class Image | ||
{ | ||
[DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
public Guid Id { get; set; } | ||
public byte[] Content { get; set; } | ||
} |
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,6 @@ | ||
namespace Resources.WebApi.Models.Images; | ||
|
||
public class ImageCreate | ||
{ | ||
public string Base64Content { get; set; } | ||
} |
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,12 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Resources.WebApi.Models.Images; | ||
|
||
[PrimaryKey(nameof(MessageId), nameof(ImageId))] | ||
public class ImageMessage | ||
{ | ||
public Guid MessageId { get; set; } | ||
public Guid ImageId { get; set; } | ||
|
||
public Image Image { get; set; } | ||
} |
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 @@ | ||
namespace Resources.WebApi.Models.Images; | ||
|
||
public class ImageView | ||
{ | ||
public Guid Id { get; set; } | ||
public string Base64Content { get; set; } | ||
} |
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,18 @@ | ||
using AutoMapper; | ||
using Resources.WebApi.Models.Images; | ||
using Resources.WebApi.Services; | ||
|
||
namespace Resources.WebApi.Profiles; | ||
|
||
public class ImageProfile : Profile | ||
{ | ||
public ImageProfile() | ||
{ | ||
CreateMap<Image, ImageView>() | ||
.ForMember( | ||
_ => _.Base64Content, | ||
expression => expression.MapFrom(_ => Convert.ToBase64String(_.Content))); | ||
CreateMap<ImageView, ImageResponse>(); | ||
CreateMap<ImageRequest, ImageCreate>(); | ||
} | ||
} |
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,33 @@ | ||
using Infrastructure.Extensions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Resources.WebApi.GrpcServices; | ||
using Resources.WebApi.Repositories; | ||
using Resources.WebApi.Services; | ||
|
||
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
ConfigureServices(builder); | ||
WebApplication app = builder.Build(); | ||
app.TriggerEntityFrameworkMigrations<ApplicationContext>(); | ||
ConfigureMiddlewares(app); | ||
|
||
static void ConfigureServices(WebApplicationBuilder builder) | ||
{ | ||
builder.Services.AddGrpc(); | ||
|
||
string connectionString = builder.Configuration.GetRequiredConnectionString("Default"); | ||
builder.Services | ||
.AddDbContext<ApplicationContext>(options => options.UseNpgsql(connectionString)) | ||
.AddTransient<IImagesRepository, ImagesRepository>(); | ||
|
||
builder.Services | ||
.AddAutoMapper(typeof(Program)) | ||
.AddTransient<ImagesService>() | ||
.AddGrpc(); | ||
} | ||
|
||
static void ConfigureMiddlewares(WebApplication app) | ||
{ | ||
app.MapGrpcService<GrpcImagesService>(); | ||
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client."); | ||
app.Run(); | ||
} |
Oops, something went wrong.