fix: теперь нет каскадного удаления записей изменения баланса

This commit is contained in:
mfnefd 2024-11-27 01:16:08 +04:00
parent 6d734bbf79
commit ba8c4a76d8
14 changed files with 235 additions and 15 deletions

View File

@ -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; }
}

View File

@ -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()
};
}

View File

@ -9,5 +9,5 @@ public interface IChangeRecordService
Task<ChangeRecordViewModel> Create(ChangeRecordDto model);
Task<ChangeRecordViewModel> Update(ChangeRecordDto model);
Task<ChangeRecordViewModel> Delete(ChangeRecordSearch search);
Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch search);
Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch? search = null);
}

View File

@ -4,4 +4,5 @@ public class SpendingGroupViewModel
{
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public List<ChangeRecordViewModel> ChangeRecords { get; set; } = new();
}

View File

@ -34,6 +34,20 @@ public class ChangeRecordController : ControllerBase
}
[HttpGet]
public async Task<ActionResult<IEnumerable<ChangeRecordViewModel>>> GetChangeRecords()
{
try
{
var records = await _changeRecordService.GetList();
return Ok(records);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpGet("filter")]
public async Task<ActionResult<IEnumerable<ChangeRecordViewModel>>> GetChangeRecords(
[FromQuery] ChangeRecordSearch search)
{

View File

@ -0,0 +1,139 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<DateTime>("ChangedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid?>("SpendingGroupId")
.HasColumnType("uuid");
b.Property<decimal>("Sum")
.HasColumnType("numeric");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("SpendingGroupId");
b.HasIndex("UserId");
b.ToTable("ChangeRecords");
});
modelBuilder.Entity("Infrastructure.Models.SpendingGroup", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<Guid>("UserId")
.HasColumnType("uuid");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("SpendingGroups");
});
modelBuilder.Entity("Infrastructure.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<decimal>("Balance")
.HasColumnType("numeric");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("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
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastructure.Migrations
{
/// <inheritdoc />
public partial class fixChangeRecord : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId",
table: "ChangeRecords");
migrationBuilder.AlterColumn<Guid>(
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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ChangeRecords_SpendingGroups_SpendingGroupId",
table: "ChangeRecords");
migrationBuilder.AlterColumn<Guid>(
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);
}
}
}

View File

@ -31,7 +31,7 @@ namespace Infrastructure.Migrations
b.Property<DateTime>("ChangedAt")
.HasColumnType("timestamp with time zone");
b.Property<Guid>("SpendingGroupId")
b.Property<Guid?>("SpendingGroupId")
.HasColumnType("uuid");
b.Property<decimal>("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

View File

@ -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)

View File

@ -7,5 +7,5 @@ public class SpendingGroup
public Guid UserId { get; set; }
public User User { get; set; } = null!;
public List<ChangeRecord> ChangeRecords { get; set; } = new();
public List<ChangeRecord>? ChangeRecords { get; set; }
}

View File

@ -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<ChangeRecordDto?> Update(ChangeRecordDto changeRecord)

View File

@ -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()

View File

@ -39,7 +39,7 @@ public class ChangeRecordService : IChangeRecordService
return record.ToView();
}
public async Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch search)
public async Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch? search = null)
{
var records = await _changeRecordRepo.GetList(search);

View File

@ -44,7 +44,7 @@ public class SpendingGroupService : ISpendingGroupService
return group.ToView();
}
public async Task<IEnumerable<SpendingGroupViewModel>> GetList(SpendingGroupSearch search)
public async Task<IEnumerable<SpendingGroupViewModel>> GetList(SpendingGroupSearch? search = null)
{
var groups = await _spendingGroupRepo.GetList(search);
return groups.Select(x => x.ToView()).ToList();