Skip to content
Snippets Groups Projects
Commit 5068abdb authored by Václav Bílý's avatar Václav Bílý :cowboy:
Browse files

Fix initial migrations, Comments

parent 5328ce5a
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
image: webapi
build:
context: .
dockerfile: src/Web/Dockerfile
dockerfile: src/WebApi/Dockerfile
ports:
- "8080:8080"
- "4443:443"
......@@ -11,7 +11,7 @@
- postgres
environment:
- ASPNETCORE_HTTP_PORTS=8080
- ConnectionStrings__DefaultConnection=Host=localhost;Port=5432;Database=social-net-soc-db;Username=postgres;Password=pass
- ConnectionStrings__DefaultConnection=Host=postgres;Port=5432;Database=social-net-soc-db;Username=postgres;Password=pass
postgres:
image: postgres:16.0
......
......@@ -5,6 +5,8 @@ namespace SocialNetwork.Social.Application.Common.Interfaces;
public interface IApplicationDbContext
{
DbSet<Post> Posts { get; }
DbSet<Comment> Comments { get; }
Task<int> SaveChangesAsync(CancellationToken cancellationToken);
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
......@@ -9,6 +9,8 @@ public class ApplicationDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Post> Posts => Set<Post>();
public DbSet<Comment> Comments => Set<Comment>();
protected override void OnModelCreating(ModelBuilder builder)
{
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
......
......@@ -14,7 +14,7 @@ public static class InitialiserExtensions
var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
// await initialiser.InitialiseAsync();
await initialiser.InitialiseAsync();
await initialiser.SeedAsync();
}
......@@ -36,7 +36,6 @@ public class ApplicationDbContextInitialiser(ILogger<ApplicationDbContextInitial
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while initialising the database");
throw;
}
}
......@@ -49,7 +48,6 @@ public class ApplicationDbContextInitialiser(ILogger<ApplicationDbContextInitial
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while seeding the database");
throw;
}
}
......
......@@ -12,7 +12,7 @@ using SocialNetwork.Social.Infrastructure.Data;
namespace SocialNetwork.Social.Infrastructure.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20240113105109_Initial")]
[Migration("20240113141228_Initial")]
partial class Initial
{
/// <inheritdoc />
......@@ -55,7 +55,7 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
b.HasIndex("PostId");
b.ToTable("Comment");
b.ToTable("Comments");
});
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Post", b =>
......@@ -95,13 +95,11 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Comment", b =>
{
b.HasOne("SocialNetwork.Social.Domain.Entities.Feed.Post", "Post")
b.HasOne("SocialNetwork.Social.Domain.Entities.Feed.Post", null)
.WithMany("Comments")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Post");
});
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Post", b =>
......
......@@ -30,7 +30,7 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
});
migrationBuilder.CreateTable(
name: "Comment",
name: "Comments",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
......@@ -43,9 +43,9 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_Comment", x => x.Id);
table.PrimaryKey("PK_Comments", x => x.Id);
table.ForeignKey(
name: "FK_Comment_Posts_PostId",
name: "FK_Comments_Posts_PostId",
column: x => x.PostId,
principalTable: "Posts",
principalColumn: "Id",
......@@ -53,8 +53,8 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
});
migrationBuilder.CreateIndex(
name: "IX_Comment_PostId",
table: "Comment",
name: "IX_Comments_PostId",
table: "Comments",
column: "PostId");
}
......@@ -62,7 +62,7 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Comment");
name: "Comments");
migrationBuilder.DropTable(
name: "Posts");
......
......@@ -52,7 +52,7 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
b.HasIndex("PostId");
b.ToTable("Comment");
b.ToTable("Comments");
});
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Post", b =>
......@@ -92,13 +92,11 @@ namespace SocialNetwork.Social.Infrastructure.Migrations
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Comment", b =>
{
b.HasOne("SocialNetwork.Social.Domain.Entities.Feed.Post", "Post")
b.HasOne("SocialNetwork.Social.Domain.Entities.Feed.Post", null)
.WithMany("Comments")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Post");
});
modelBuilder.Entity("SocialNetwork.Social.Domain.Entities.Feed.Post", b =>
......
......@@ -20,11 +20,15 @@ public class PostEndpoints
}
private async Task<IResult> GetAllPosts(IApplicationDbContext dbContext) =>
Results.Ok(await dbContext.Posts.ToListAsync());
Results.Ok(await dbContext.Posts
.Include(post => post.Comments) // normally we wouldn't include comments here, but for demo purposes it's fine
.ToListAsync());
private async Task<IResult> GetPostById(Guid postId, IApplicationDbContext dbContext)
{
var post = await dbContext.Posts.FindAsync(postId);
var post = await dbContext.Posts
.Include(post => post.Comments)
.FirstOrDefaultAsync(post => post.Id == postId);
if (post is null) return Results.NotFound();
return Results.Ok(post);
......@@ -33,12 +37,18 @@ public class PostEndpoints
private async Task<IResult> CommentPost(Guid postId, Comment comment, IApplicationDbContext dbContext)
{
var post = await dbContext.Posts.FindAsync(postId);
if (post is null) return Results.NotFound();
if (post is null) return Results.NotFound("Post not found");
if (post.Comments.Any(c => c.Id == comment.Id)) return Results.Conflict("Comment already exists");
comment.PostId = postId;
await dbContext.Comments.AddAsync(comment);
post.Comments.Add(comment);
await dbContext.SaveChangesAsync(default);
return Results.CreatedAtRoute("api/posts/{id}", new {id = postId}, comment);
await dbContext.SaveChangesAsync();
dbContext.Posts.Update(post);
await dbContext.SaveChangesAsync();
return Results.Created($"api/posts/{postId}", comment);
}
private async Task<IResult> CreatePost(Post post, IApplicationDbContext dbContext)
......@@ -47,7 +57,7 @@ public class PostEndpoints
if (doesPostExist) return Results.Conflict();
var entry = dbContext.Posts.Add(post);
await dbContext.SaveChangesAsync(default);
await dbContext.SaveChangesAsync();
var persistedPost = entry.Entity;
return Results.Created($"/api/posts/{persistedPost.Id}", persistedPost);
......@@ -60,7 +70,7 @@ public class PostEndpoints
post.LikesCount++;
// TODO add who liked the post
await dbContext.SaveChangesAsync(default);
await dbContext.SaveChangesAsync();
return Results.Ok();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment