diff --git a/backend/services/PostService/src/PostService.Api/PostService.Api.csproj b/backend/services/PostService/src/PostService.Api/PostService.Api.csproj index a88018b..dc3568c 100644 --- a/backend/services/PostService/src/PostService.Api/PostService.Api.csproj +++ b/backend/services/PostService/src/PostService.Api/PostService.Api.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/backend/services/PostService/src/PostService.Api/Program.cs b/backend/services/PostService/src/PostService.Api/Program.cs index 3934f5f..fbb6754 100644 --- a/backend/services/PostService/src/PostService.Api/Program.cs +++ b/backend/services/PostService/src/PostService.Api/Program.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using PostService.Api.GraphQL.Mutation; using PostService.Api.GraphQL.Query; using PostService.Application; @@ -7,7 +8,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services - .AddPersistenceServices() + .AddPersistenceServices(builder.Configuration) .AddInfrastructureServices() .AddApplicationServices(); @@ -18,6 +19,12 @@ var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var dbContext = scope.ServiceProvider.GetRequiredService(); + dbContext.Database.Migrate(); +} + app.UseHttpsRedirection(); app.MapGraphQL(); diff --git a/backend/services/PostService/src/PostService.Api/appsettings.Development.json b/backend/services/PostService/src/PostService.Api/appsettings.Development.json index 0c208ae..1b138cf 100644 --- a/backend/services/PostService/src/PostService.Api/appsettings.Development.json +++ b/backend/services/PostService/src/PostService.Api/appsettings.Development.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=mysecretpasswordfordevelopment;Database=postgres" } } diff --git a/backend/services/PostService/src/PostService.Persistence/DependencyInjection.cs b/backend/services/PostService/src/PostService.Persistence/DependencyInjection.cs index 294ef06..e420826 100644 --- a/backend/services/PostService/src/PostService.Persistence/DependencyInjection.cs +++ b/backend/services/PostService/src/PostService.Persistence/DependencyInjection.cs @@ -1,11 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace PostService.Persistence; public static class DependencyInjection { - public static IServiceCollection AddPersistenceServices(this IServiceCollection services) + public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration) { + services.AddDbContext(options => + options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))); + return services; } } \ No newline at end of file diff --git a/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.Designer.cs b/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.Designer.cs new file mode 100644 index 0000000..69b042a --- /dev/null +++ b/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.Designer.cs @@ -0,0 +1,139 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PostService.Persistence; + +#nullable disable + +namespace PostService.Persistence.Migrations +{ + [DbContext(typeof(PostDbContext))] + [Migration("20241024191844_InitialMigration")] + partial class InitialMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PostService.Domain.Entities.Comment", b => + { + b.Property("CommentId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("CommentId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.Like", b => + { + b.Property("LikeId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LikeId"); + + b.ToTable("Likes"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.Post", b => + { + b.Property("PostId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CommentCount") + .HasColumnType("integer"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("text"); + + b.Property("LikeCount") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("PostId"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.User", b => + { + b.Property("UserId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Username") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("UserId"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.cs b/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.cs new file mode 100644 index 0000000..bf49536 --- /dev/null +++ b/backend/services/PostService/src/PostService.Persistence/Migrations/20241024191844_InitialMigration.cs @@ -0,0 +1,94 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PostService.Persistence.Migrations +{ + /// + public partial class InitialMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + CommentId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Content = table.Column(type: "text", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.CommentId); + }); + + migrationBuilder.CreateTable( + name: "Likes", + columns: table => new + { + LikeId = table.Column(type: "uuid", nullable: false), + PostId = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Likes", x => x.LikeId); + }); + + migrationBuilder.CreateTable( + name: "Posts", + columns: table => new + { + PostId = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Title = table.Column(type: "text", nullable: false), + Content = table.Column(type: "text", nullable: false), + ImageUrl = table.Column(type: "text", nullable: false), + LikeCount = table.Column(type: "integer", nullable: false), + CommentCount = table.Column(type: "integer", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Posts", x => x.PostId); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "text", nullable: false), + LastName = table.Column(type: "text", nullable: false), + Username = table.Column(type: "text", nullable: false), + ImageUrl = table.Column(type: "text", nullable: false), + CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.UserId); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Comments"); + + migrationBuilder.DropTable( + name: "Likes"); + + migrationBuilder.DropTable( + name: "Posts"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/backend/services/PostService/src/PostService.Persistence/Migrations/PostDbContextModelSnapshot.cs b/backend/services/PostService/src/PostService.Persistence/Migrations/PostDbContextModelSnapshot.cs new file mode 100644 index 0000000..e74743d --- /dev/null +++ b/backend/services/PostService/src/PostService.Persistence/Migrations/PostDbContextModelSnapshot.cs @@ -0,0 +1,136 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PostService.Persistence; + +#nullable disable + +namespace PostService.Persistence.Migrations +{ + [DbContext(typeof(PostDbContext))] + partial class PostDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PostService.Domain.Entities.Comment", b => + { + b.Property("CommentId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("CommentId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.Like", b => + { + b.Property("LikeId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LikeId"); + + b.ToTable("Likes"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.Post", b => + { + b.Property("PostId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CommentCount") + .HasColumnType("integer"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("text"); + + b.Property("LikeCount") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("PostId"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("PostService.Domain.Entities.User", b => + { + b.Property("UserId") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("text"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("text"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Username") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("UserId"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/services/PostService/src/PostService.Persistence/PostDbContext.cs b/backend/services/PostService/src/PostService.Persistence/PostDbContext.cs new file mode 100644 index 0000000..254e7da --- /dev/null +++ b/backend/services/PostService/src/PostService.Persistence/PostDbContext.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using PostService.Domain.Entities; + +namespace PostService.Persistence; + +public class PostDbContext : DbContext +{ + public PostDbContext(DbContextOptions options) : base(options) + { + } + + public DbSet Users { get; set; } + public DbSet Posts { get; set; } + public DbSet Comments { get; set; } + public DbSet Likes { get; set; } +} \ No newline at end of file diff --git a/backend/services/PostService/src/PostService.Persistence/PostService.Persistence.csproj b/backend/services/PostService/src/PostService.Persistence/PostService.Persistence.csproj index cd696e1..33cdc51 100644 --- a/backend/services/PostService/src/PostService.Persistence/PostService.Persistence.csproj +++ b/backend/services/PostService/src/PostService.Persistence/PostService.Persistence.csproj @@ -7,7 +7,14 @@ + + + + + + +