diff --git a/back/Contracts/DTOs/ChangeRecordDto.cs b/back/Contracts/DTOs/ChangeRecordDto.cs index f95da9c..21d140c 100644 --- a/back/Contracts/DTOs/ChangeRecordDto.cs +++ b/back/Contracts/DTOs/ChangeRecordDto.cs @@ -4,7 +4,7 @@ public class ChangeRecordDto { public Guid Id { get; set; } public Guid UserId { get; set; } - public Guid SpendingGroupId { get; set; } + public Guid? SpendingGroupId { get; set; } public decimal Sum { get; set; } public DateTime ChangedAt { get; set; } } \ No newline at end of file diff --git a/back/Contracts/Mappers/SpendingGroupMapper.cs b/back/Contracts/Mappers/SpendingGroupMapper.cs index 7eca2d3..92877a5 100644 --- a/back/Contracts/Mappers/SpendingGroupMapper.cs +++ b/back/Contracts/Mappers/SpendingGroupMapper.cs @@ -9,6 +9,7 @@ public static class SpendingGroupMapper => new() { Id = spendingGroup.Id, - Name = spendingGroup.Name + Name = spendingGroup.Name, + ChangeRecords = spendingGroup.ChangeRecords.Select(x => x.ToView()).ToList() }; } \ No newline at end of file diff --git a/back/Contracts/Services/IChangeRecordService.cs b/back/Contracts/Services/IChangeRecordService.cs index 9dd0832..b9939e0 100644 --- a/back/Contracts/Services/IChangeRecordService.cs +++ b/back/Contracts/Services/IChangeRecordService.cs @@ -9,5 +9,5 @@ public interface IChangeRecordService Task Create(ChangeRecordDto model); Task Update(ChangeRecordDto model); Task Delete(ChangeRecordSearch search); - Task> GetList(ChangeRecordSearch search); + Task> GetList(ChangeRecordSearch? search = null); } \ No newline at end of file diff --git a/back/Contracts/ViewModels/SpendingGroupViewModel.cs b/back/Contracts/ViewModels/SpendingGroupViewModel.cs index 12cba2d..c22ae62 100644 --- a/back/Contracts/ViewModels/SpendingGroupViewModel.cs +++ b/back/Contracts/ViewModels/SpendingGroupViewModel.cs @@ -4,4 +4,5 @@ public class SpendingGroupViewModel { public Guid Id { get; set; } public string Name { get; set; } = string.Empty; + public List ChangeRecords { get; set; } = new(); } \ No newline at end of file diff --git a/back/Controllers/Controllers/ChangeRecordController.cs b/back/Controllers/Controllers/ChangeRecordController.cs index 8ae218a..c823a99 100644 --- a/back/Controllers/Controllers/ChangeRecordController.cs +++ b/back/Controllers/Controllers/ChangeRecordController.cs @@ -34,6 +34,20 @@ public class ChangeRecordController : ControllerBase } [HttpGet] + public async Task>> GetChangeRecords() + { + try + { + var records = await _changeRecordService.GetList(); + return Ok(records); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpGet("filter")] public async Task>> GetChangeRecords( [FromQuery] ChangeRecordSearch search) { diff --git a/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.Designer.cs b/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.Designer.cs new file mode 100644 index 0000000..937117a --- /dev/null +++ b/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.Designer.cs @@ -0,0 +1,139 @@ +// +using System; +using Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20241126210947_fixChangeRecord")] + partial class fixChangeRecord + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Infrastructure.Models.ChangeRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ChangedAt") + .HasColumnType("timestamp with time zone"); + + b.Property("SpendingGroupId") + .HasColumnType("uuid"); + + b.Property("Sum") + .HasColumnType("numeric"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("SpendingGroupId"); + + b.HasIndex("UserId"); + + b.ToTable("ChangeRecords"); + }); + + modelBuilder.Entity("Infrastructure.Models.SpendingGroup", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("SpendingGroups"); + }); + + modelBuilder.Entity("Infrastructure.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Balance") + .HasColumnType("numeric"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Infrastructure.Models.ChangeRecord", b => + { + b.HasOne("Infrastructure.Models.SpendingGroup", "SpendingGroup") + .WithMany("ChangeRecords") + .HasForeignKey("SpendingGroupId"); + + b.HasOne("Infrastructure.Models.User", "User") + .WithMany("ChangeRecords") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("SpendingGroup"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Infrastructure.Models.SpendingGroup", b => + { + b.HasOne("Infrastructure.Models.User", "User") + .WithMany("SpendingGroups") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("Infrastructure.Models.SpendingGroup", b => + { + b.Navigation("ChangeRecords"); + }); + + modelBuilder.Entity("Infrastructure.Models.User", b => + { + b.Navigation("ChangeRecords"); + + b.Navigation("SpendingGroups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.cs b/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.cs new file mode 100644 index 0000000..68c6484 --- /dev/null +++ b/back/Infrastructure/Migrations/20241126210947_fixChangeRecord.cs @@ -0,0 +1,60 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Infrastructure.Migrations +{ + /// + public partial class fixChangeRecord : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId", + table: "ChangeRecords"); + + migrationBuilder.AlterColumn( + name: "SpendingGroupId", + table: "ChangeRecords", + type: "uuid", + nullable: true, + oldClrType: typeof(Guid), + oldType: "uuid"); + + migrationBuilder.AddForeignKey( + name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId", + table: "ChangeRecords", + column: "SpendingGroupId", + principalTable: "SpendingGroups", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId", + table: "ChangeRecords"); + + migrationBuilder.AlterColumn( + name: "SpendingGroupId", + table: "ChangeRecords", + type: "uuid", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000"), + oldClrType: typeof(Guid), + oldType: "uuid", + oldNullable: true); + + migrationBuilder.AddForeignKey( + name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId", + table: "ChangeRecords", + column: "SpendingGroupId", + principalTable: "SpendingGroups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs b/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs index 82c6874..2c7a5ef 100644 --- a/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs +++ b/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs @@ -31,7 +31,7 @@ namespace Infrastructure.Migrations b.Property("ChangedAt") .HasColumnType("timestamp with time zone"); - b.Property("SpendingGroupId") + b.Property("SpendingGroupId") .HasColumnType("uuid"); b.Property("Sum") @@ -94,13 +94,11 @@ namespace Infrastructure.Migrations modelBuilder.Entity("Infrastructure.Models.ChangeRecord", b => { b.HasOne("Infrastructure.Models.SpendingGroup", "SpendingGroup") - .WithMany() - .HasForeignKey("SpendingGroupId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + .WithMany("ChangeRecords") + .HasForeignKey("SpendingGroupId"); b.HasOne("Infrastructure.Models.User", "User") - .WithMany() + .WithMany("ChangeRecords") .HasForeignKey("UserId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -121,8 +119,15 @@ namespace Infrastructure.Migrations b.Navigation("User"); }); + modelBuilder.Entity("Infrastructure.Models.SpendingGroup", b => + { + b.Navigation("ChangeRecords"); + }); + modelBuilder.Entity("Infrastructure.Models.User", b => { + b.Navigation("ChangeRecords"); + b.Navigation("SpendingGroups"); }); #pragma warning restore 612, 618 diff --git a/back/Infrastructure/Models/Changerecord.cs b/back/Infrastructure/Models/Changerecord.cs index 64b06c4..60cb92d 100644 --- a/back/Infrastructure/Models/Changerecord.cs +++ b/back/Infrastructure/Models/Changerecord.cs @@ -11,7 +11,7 @@ public class ChangeRecord public Guid UserId { get; set; } public User User { get; set; } = null!; - public Guid SpendingGroupId { get; set; } + public Guid? SpendingGroupId { get; set; } public SpendingGroup? SpendingGroup { get; set; } public void Update(ChangeRecordDto changeRecordDto) diff --git a/back/Infrastructure/Models/SpendingGroup.cs b/back/Infrastructure/Models/SpendingGroup.cs index ec9d48e..889bdb6 100644 --- a/back/Infrastructure/Models/SpendingGroup.cs +++ b/back/Infrastructure/Models/SpendingGroup.cs @@ -7,5 +7,5 @@ public class SpendingGroup public Guid UserId { get; set; } public User User { get; set; } = null!; - public List ChangeRecords { get; set; } = new(); + public List? ChangeRecords { get; set; } } \ No newline at end of file diff --git a/back/Infrastructure/Repositories/ChangeRecordRepo.cs b/back/Infrastructure/Repositories/ChangeRecordRepo.cs index cafeb2d..6da1839 100644 --- a/back/Infrastructure/Repositories/ChangeRecordRepo.cs +++ b/back/Infrastructure/Repositories/ChangeRecordRepo.cs @@ -71,7 +71,7 @@ public class ChangeRecordRepo : IChangeRecordRepo query = query.Where(x => x.ChangedAt >= search.From && x.ChangedAt <= search.To); } } - return query.Select(x => x.ToDto()); + return await query.Select(x => x.ToDto()).ToListAsync(); } public async Task Update(ChangeRecordDto changeRecord) diff --git a/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs b/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs index 1cb0585..d74490f 100644 --- a/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs +++ b/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs @@ -11,7 +11,7 @@ public static class SpendingGroupMapper Id = group.Id, Name = group.Name, UserId = group.UserId, - ChangeRecords = group.ChangeRecords.Select(x => x.ToDto()).ToList() + ChangeRecords = group.ChangeRecords?.Select(x => x.ToDto()).ToList() ?? [] }; public static SpendingGroup ToModel(this SpendingGroupDto group) => new() diff --git a/back/Services/Domain/ChangeRecordService.cs b/back/Services/Domain/ChangeRecordService.cs index 4df6138..45ed858 100644 --- a/back/Services/Domain/ChangeRecordService.cs +++ b/back/Services/Domain/ChangeRecordService.cs @@ -39,7 +39,7 @@ public class ChangeRecordService : IChangeRecordService return record.ToView(); } - public async Task> GetList(ChangeRecordSearch search) + public async Task> GetList(ChangeRecordSearch? search = null) { var records = await _changeRecordRepo.GetList(search); diff --git a/back/Services/Domain/SpendingGroupService.cs b/back/Services/Domain/SpendingGroupService.cs index b0afed1..e4ff348 100644 --- a/back/Services/Domain/SpendingGroupService.cs +++ b/back/Services/Domain/SpendingGroupService.cs @@ -44,7 +44,7 @@ public class SpendingGroupService : ISpendingGroupService return group.ToView(); } - public async Task> GetList(SpendingGroupSearch search) + public async Task> GetList(SpendingGroupSearch? search = null) { var groups = await _spendingGroupRepo.GetList(search); return groups.Select(x => x.ToView()).ToList();