Skip to content

Commit

Permalink
Add PostDbContext for database access
Browse files Browse the repository at this point in the history
  • Loading branch information
sentemon committed Oct 24, 2024
1 parent 5625ecb commit a4ccfd2
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<ItemGroup>
<PackageReference Include="HotChocolate.AspNetCore" Version="14.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

Expand Down
9 changes: 8 additions & 1 deletion backend/services/PostService/src/PostService.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.EntityFrameworkCore;
using PostService.Api.GraphQL.Mutation;
using PostService.Api.GraphQL.Query;
using PostService.Application;
Expand All @@ -7,7 +8,7 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddPersistenceServices()
.AddPersistenceServices(builder.Configuration)
.AddInfrastructureServices()
.AddApplicationServices();

Expand All @@ -18,6 +19,12 @@

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<PostDbContext>();
dbContext.Database.Migrate();
}

app.UseHttpsRedirection();

app.MapGraphQL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=mysecretpasswordfordevelopment;Database=postgres"
}
}
Original file line number Diff line number Diff line change
@@ -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<PostDbContext>(options =>
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));

return services;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PostService.Persistence.Migrations
{
/// <inheritdoc />
public partial class InitialMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Comments",
columns: table => new
{
CommentId = table.Column<Guid>(type: "uuid", nullable: false),
PostId = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Content = table.Column<string>(type: "text", nullable: false),
CreatedAt = table.Column<DateTime>(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<Guid>(type: "uuid", nullable: false),
PostId = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
CreatedAt = table.Column<DateTime>(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<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Title = table.Column<string>(type: "text", nullable: false),
Content = table.Column<string>(type: "text", nullable: false),
ImageUrl = table.Column<string>(type: "text", nullable: false),
LikeCount = table.Column<int>(type: "integer", nullable: false),
CommentCount = table.Column<int>(type: "integer", nullable: false),
CreatedAt = table.Column<DateTime>(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<Guid>(type: "uuid", nullable: false),
FirstName = table.Column<string>(type: "text", nullable: false),
LastName = table.Column<string>(type: "text", nullable: false),
Username = table.Column<string>(type: "text", nullable: false),
ImageUrl = table.Column<string>(type: "text", nullable: false),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.UserId);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Comments");

migrationBuilder.DropTable(
name: "Likes");

migrationBuilder.DropTable(
name: "Posts");

migrationBuilder.DropTable(
name: "Users");
}
}
}
Loading

0 comments on commit a4ccfd2

Please sign in to comment.