Skip to content

Commit

Permalink
feat: add migrations & adapt Dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
HunorTotBagi committed Feb 12, 2025
1 parent 1b7657d commit 4e813e9
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BuildingBlocks.Domain.Nodes.Node.Dtos;
using BuildingBlocks.Domain.Nodes.Node.ValueObjects;
using BuildingBlocks.Domain.Notes.Note.ValueObjects;
using BuildingBlocks.Domain.Reminders.Reminder.ValueObjects;

namespace BuildingBlocks.Application.Data;
Expand All @@ -9,4 +10,5 @@ public interface INodesService
Task<NodeDto> GetNodeByIdAsync(NodeId nodeId, CancellationToken cancellationToken);
Task<NodeBaseDto> GetNodeBaseByIdAsync(NodeId nodeId, CancellationToken cancellationToken);
Task AddReminder(NodeId nodeId, ReminderId reminderId, CancellationToken cancellationToken);
Task AddNote(NodeId nodeId, NoteId noteId, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using BuildingBlocks.Domain.Notes.Note.Dtos;
using BuildingBlocks.Domain.Reminders.Reminder.Dtos;

namespace BuildingBlocks.Domain.Nodes.Node.Dtos;
Expand All @@ -13,5 +14,7 @@ public class NodeDto(
List<string> categories,
List<string> tags) : NodeBaseDto(id, title, description, timestamp, importance, phase, categories, tags)
{
[JsonPropertyName("reminders")] public List<ReminderBaseDto> Reminders { get; } = [];
[JsonPropertyName("reminders")] public List<ReminderBaseDto> Reminders { get; } = [];

[JsonPropertyName("notes")] public List<NoteBaseDto> Notes { get; } = [];
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BuildingBlocks.Domain.Notes.Note.ValueObjects;
using System.Text.Json.Serialization;
using BuildingBlocks.Domain.Nodes.Node.Dtos;
using BuildingBlocks.Domain.Notes.Note.ValueObjects;

namespace BuildingBlocks.Domain.Notes.Note.Dtos;

Expand All @@ -10,6 +12,8 @@ public class NoteDto(
string owner,
List<NoteId> relatedNotes,
List<string> sharedWith,
bool isPublic) : NoteBaseDto(id, title, content, timestamp, owner, relatedNotes, sharedWith, isPublic)
bool isPublic,
NodeBaseDto node) : NoteBaseDto(id, title, content, timestamp, owner, relatedNotes, sharedWith, isPublic)
{
[JsonPropertyName("note")] public NodeBaseDto Node { get; set; } = node;
}
19 changes: 18 additions & 1 deletion Backend/src/Modules/Nodes/Nodes.Application/Data/NodesService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BuildingBlocks.Application.Data;
using BuildingBlocks.Domain.Nodes.Node.Dtos;
using BuildingBlocks.Domain.Nodes.Node.ValueObjects;
using BuildingBlocks.Domain.Notes.Note.ValueObjects;
using BuildingBlocks.Domain.Reminders.Reminder.ValueObjects;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,6 +25,14 @@ public async Task<NodeDto> GetNodeByIdAsync(NodeId nodeId, CancellationToken can
nodeDto.Reminders.Add(reminder);
}

var notesService = serviceProvider.GetRequiredService<INotesService>();

foreach (var noteId in node.NoteIds)
{
var note = await notesService.GetNoteBaseByIdAsync(noteId, cancellationToken);
nodeDto.Notes.Add(note);
}

return nodeDto;
}

Expand All @@ -41,5 +50,13 @@ public async Task AddReminder(NodeId nodeId, ReminderId reminderId, Cancellation
node.AddReminder(reminderId);

await nodesRepository.UpdateNodeAsync(node, cancellationToken);
}
}

public async Task AddNote(NodeId nodeId, NoteId noteId, CancellationToken cancellationToken)
{
var node = await nodesRepository.GetNodeByIdAsync(nodeId, cancellationToken);
node.AddNote(noteId);

await nodesRepository.UpdateNodeAsync(node, cancellationToken);
}
}

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,30 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Nodes.Infrastructure.Data.Migrations
{
/// <inheritdoc />
public partial class AddNote : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "NoteIds",
schema: "Nodes",
table: "Nodes",
type: "text",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "NoteIds",
schema: "Nodes",
table: "Nodes");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Nodes.Infrastructure.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Nodes.Infrastructure.Data.Migrations
{
[DbContext(typeof(NodesDbContext))]
partial class NodesDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Nodes")
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Nodes.Domain.Models.Node", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");

b.Property<DateTime?>("CreatedAt")
.HasColumnType("timestamp with time zone");

b.Property<string>("CreatedBy")
.HasColumnType("text");

b.Property<string>("Description")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("character varying(500)");

b.Property<int>("Importance")
.HasColumnType("integer");

b.Property<DateTime?>("LastModifiedAt")
.HasColumnType("timestamp with time zone");

b.Property<string>("LastModifiedBy")
.HasColumnType("text");

b.Property<string>("Phase")
.IsRequired()
.HasColumnType("text");

b.Property<string>("SerializedReminderIds")
.HasColumnType("text")
.HasColumnName("ReminderIds");

b.Property<DateTime>("Timestamp")
.HasColumnType("timestamp with time zone");

b.Property<string>("Title")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");

b.HasKey("Id");

b.ToTable("Nodes", "Nodes");
});
#pragma warning restore 612, 618
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Nodes.Infrastructure.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;

#nullable disable

namespace Nodes.Infrastructure.Data.Migrations
{
[DbContext(typeof(NodesDbContext))]
partial class NodesDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("Nodes")
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("Nodes.Domain.Models.Node", b =>
{
b.Property<Guid>("Id")
.HasColumnType("uuid");

b.Property<DateTime?>("CreatedAt")
.HasColumnType("timestamp with time zone");

b.Property<string>("CreatedBy")
.HasColumnType("text");

b.Property<string>("Description")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("character varying(500)");

b.Property<int>("Importance")
.HasColumnType("integer");

b.Property<DateTime?>("LastModifiedAt")
.HasColumnType("timestamp with time zone");

b.Property<string>("LastModifiedBy")
.HasColumnType("text");

b.Property<string>("NoteIds")
.HasColumnType("text")
.HasColumnName("NoteIds");

b.Property<string>("Phase")
.IsRequired()
.HasColumnType("text");

b.Property<string>("ReminderIds")
.HasColumnType("text")
.HasColumnName("ReminderIds");

b.Property<DateTime>("Timestamp")
.HasColumnType("timestamp with time zone");

b.Property<string>("Title")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");

b.HasKey("Id");

b.ToTable("Nodes", "Nodes");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using BuildingBlocks.Domain.Nodes.Node.ValueObjects;
using BuildingBlocks.Domain.Notes.Note.ValueObjects;
using Notes.Application.Entities.Notes.Commands.CreateNote;

Expand Down Expand Up @@ -33,6 +34,7 @@ public record CreateNoteRequest
public string Owner { get; set; }
public List<string> SharedWith { get; set; }
public bool IsPublic { get; set; }
public NodeId NodeId { get; set; }
}

public record CreateNoteResponse(NoteId Id);
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using BuildingBlocks.Domain.Notes.Note.Dtos;
using BuildingBlocks.Domain.Notes.Note.ValueObjects;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Notes.Application.Data.Abstractions;
using Notes.Application.Entities.Notes.Extensions;

namespace Notes.Application.Data;

Expand All @@ -12,9 +12,12 @@ public class NotesService(INotesRepository notesRepository, IServiceProvider ser
public async Task<NoteDto> GetNoteByIdAsync(NoteId noteId, CancellationToken cancellationToken)
{
var note = await notesRepository.GetNoteByIdAsync(noteId, cancellationToken);
var noteDto = note.ToNoteDto();
var noteDto = note.Adapt<NoteDto>();

return noteDto;
var node = await serviceProvider.GetRequiredService<INodesService>().GetNodeBaseByIdAsync(note.NodeId, cancellationToken);
noteDto.Node = node;

return noteDto;
}

public async Task<NoteBaseDto> GetNoteBaseByIdAsync(NoteId noteId, CancellationToken cancellationToken)
Expand Down
Loading

0 comments on commit 4e813e9

Please sign in to comment.