fix: корректно работает круд групп расходов

This commit is contained in:
mfnefd 2024-11-26 20:23:59 +04:00
parent 43b00bfc2f
commit f0bdffeb86
12 changed files with 190 additions and 14 deletions

View File

@ -7,6 +7,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup> </ItemGroup>

View File

@ -9,5 +9,7 @@ public static class AddDomainServicesExtension
{ {
services.AddTransient<IAuthService, AuthService>(); services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IUserService, UserService>(); services.AddTransient<IUserService, UserService>();
services.AddTransient<ISpendingGroupService, SpendingGroupService>();
} }
} }

View File

@ -10,5 +10,6 @@ public static class AddReposExtension
public static void AddRepos(this IServiceCollection services) public static void AddRepos(this IServiceCollection services)
{ {
services.AddTransient<IUserRepo, UserRepo>(); services.AddTransient<IUserRepo, UserRepo>();
services.AddTransient<ISpendingGroupRepo, SpendingGroupRepo>();
} }
} }

View File

@ -18,8 +18,9 @@ public static class DatabaseSetupExtension
try try
{ {
using var scope = app.ApplicationServices.CreateScope(); using var scope = app.ApplicationServices.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<DatabaseContext>(); var context = scope.ServiceProvider.GetRequiredService<IDbContextFactory<DatabaseContext>>();
context.Database.Migrate(); using var db = context.CreateDbContext();
db.Database.Migrate();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -9,7 +9,6 @@ public class DatabaseContext : DbContext
public DatabaseContext(DbContextOptions<DatabaseContext> options) public DatabaseContext(DbContextOptions<DatabaseContext> options)
: base(options) : base(options)
{ {
Database.EnsureCreated();
} }
public DbSet<User> Users { get; set; } = null!; public DbSet<User> Users { get; set; } = null!;

View File

@ -5,10 +5,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.1" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.1" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,88 @@
// <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("20241126140310_SpendingGroup")]
partial class SpendingGroup
{
/// <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.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.SpendingGroup", b =>
{
b.HasOne("Infrastructure.Models.User", "User")
.WithMany("SpendingGroups")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("Infrastructure.Models.User", b =>
{
b.Navigation("SpendingGroups");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,46 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastructure.Migrations
{
/// <inheritdoc />
public partial class SpendingGroup : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SpendingGroups",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "text", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SpendingGroups", x => x.Id);
table.ForeignKey(
name: "FK_SpendingGroups_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_SpendingGroups_UserId",
table: "SpendingGroups",
column: "UserId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SpendingGroups");
}
}
}

View File

@ -22,6 +22,26 @@ namespace Infrastructure.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
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 => modelBuilder.Entity("Infrastructure.Models.User", b =>
{ {
b.Property<Guid>("Id") b.Property<Guid>("Id")
@ -43,6 +63,22 @@ namespace Infrastructure.Migrations
b.ToTable("Users"); b.ToTable("Users");
}); });
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.User", b =>
{
b.Navigation("SpendingGroups");
});
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }
} }

View File

@ -9,6 +9,7 @@ public class User
public string Name { get; set; } = null!; public string Name { get; set; } = null!;
public string Password { get; set; } = null!; public string Password { get; set; } = null!;
public decimal Balance { get; set; } public decimal Balance { get; set; }
public List<SpendingGroup>? SpendingGroups { get; set; }
public void Update(UserDto userDto) public void Update(UserDto userDto)
{ {

View File

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

View File

@ -20,6 +20,10 @@ public class UserService : IUserService
public async Task<UserViewModel> Delete(UserSearch search) public async Task<UserViewModel> Delete(UserSearch search)
{ {
var user = await _userRepo.Delete(search); var user = await _userRepo.Delete(search);
if (user == null)
{
throw new UserNotFoundException($"Пользователь для удаления не найден");
}
return user.ToView(); return user.ToView();
} }
@ -35,13 +39,11 @@ public class UserService : IUserService
public async Task<UserViewModel> UpdateUserData(UserDto user) public async Task<UserViewModel> UpdateUserData(UserDto user)
{ {
var existingUser = await _userRepo.Get(new UserSearch() { Name = user.Name });
if (existingUser == null)
{
throw new UserNotFoundException($"Пользователь {user.Name} не найден");
}
var updatedUser = await _userRepo.Update(user); var updatedUser = await _userRepo.Update(user);
if (updatedUser == null)
{
throw new EntityNotFoundException("При обновлении не получилось найти пользователя с id = " + user.Id);
}
return updatedUser.ToView(); return updatedUser.ToView();
} }
} }