0.1.0 #2
@ -7,6 +7,6 @@ public interface IUserRepo
|
|||||||
{
|
{
|
||||||
public Task<UserDto?> Get(UserSearch search);
|
public Task<UserDto?> Get(UserSearch search);
|
||||||
public Task<UserDto> Create(UserDto user);
|
public Task<UserDto> Create(UserDto user);
|
||||||
public Task<UserDto> Update(UserDto user);
|
public Task<UserDto?> Update(UserDto user);
|
||||||
public Task<UserDto> Delete(UserSearch search);
|
public Task<UserDto?> Delete(UserSearch search);
|
||||||
}
|
}
|
15
back/Infrastructure/DatabaseContext.cs
Normal file
15
back/Infrastructure/DatabaseContext.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using Infrastructure.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Infrastructure;
|
||||||
|
|
||||||
|
public class DatabaseContext : DbContext
|
||||||
|
{
|
||||||
|
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||||
|
: base(options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DbSet<User> Users { get; set; } = null!;
|
||||||
|
}
|
20
back/Infrastructure/DbContextFactory.cs
Normal file
20
back/Infrastructure/DbContextFactory.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Infrastructure;
|
||||||
|
|
||||||
|
public class DbContextFactory : IDbContextFactory<DatabaseContext>
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
|
public DbContextFactory(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseContext CreateDbContext()
|
||||||
|
{
|
||||||
|
var scope = _serviceProvider.CreateScope();
|
||||||
|
return scope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,17 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||||
|
</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" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
25
back/Infrastructure/Models/User.cs
Normal file
25
back/Infrastructure/Models/User.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System.Reflection.Metadata.Ecma335;
|
||||||
|
using Contracts.DTO;
|
||||||
|
|
||||||
|
namespace Infrastructure.Models;
|
||||||
|
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Name { get; set; } = null!;
|
||||||
|
public string Password { get; set; } = null!;
|
||||||
|
public decimal Balance { get; set; }
|
||||||
|
|
||||||
|
public void Update(UserDto userDto)
|
||||||
|
{
|
||||||
|
Id = userDto.Id;
|
||||||
|
if (!string.IsNullOrWhiteSpace(userDto.Name))
|
||||||
|
{
|
||||||
|
Name = userDto.Name;
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrWhiteSpace(userDto.Password))
|
||||||
|
{
|
||||||
|
Password = userDto.Password;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
72
back/Infrastructure/Repositories/UserRepo.cs
Normal file
72
back/Infrastructure/Repositories/UserRepo.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using Contracts.DTO;
|
||||||
|
using Contracts.Repositories;
|
||||||
|
using Contracts.SearchModels;
|
||||||
|
using Infrastructure.Support.Mappers;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Infrastructure.Repositories;
|
||||||
|
|
||||||
|
public class UserRepo : IUserRepo
|
||||||
|
{
|
||||||
|
public readonly DbContextFactory _factory;
|
||||||
|
|
||||||
|
public UserRepo(DbContextFactory factory)
|
||||||
|
{
|
||||||
|
_factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserDto> Create(UserDto user)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var createdUser = await context.Users.AddAsync(user.ToModel());
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return createdUser.Entity.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserDto?> Delete(UserSearch search)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var user = await context.Users
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == search.Id
|
||||||
|
|| x.Name == search.Name);
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Users.Remove(user);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return user.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserDto?> Get(UserSearch search)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var user = await context.Users
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == search.Id
|
||||||
|
|| x.Name == search.Name);
|
||||||
|
|
||||||
|
return user?.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<UserDto?> Update(UserDto user)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var existingUser = await context.Users.FirstOrDefaultAsync(x => x.Id == user.Id);
|
||||||
|
|
||||||
|
if (existingUser == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
existingUser.Update(user);
|
||||||
|
context.Users.Update(existingUser);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return existingUser.ToDto();
|
||||||
|
}
|
||||||
|
}
|
25
back/Infrastructure/Support/Mappers/UserMapper.cs
Normal file
25
back/Infrastructure/Support/Mappers/UserMapper.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using Contracts.DTO;
|
||||||
|
using Infrastructure.Models;
|
||||||
|
|
||||||
|
namespace Infrastructure.Support.Mappers;
|
||||||
|
|
||||||
|
public static class UserMapper
|
||||||
|
{
|
||||||
|
public static UserDto ToDto(this User user)
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
Name = user.Name,
|
||||||
|
Balance = user.Balance,
|
||||||
|
Password = user.Password
|
||||||
|
};
|
||||||
|
|
||||||
|
public static User ToModel(this UserDto user)
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
Name = user.Name,
|
||||||
|
Balance = user.Balance,
|
||||||
|
Password = user.Password
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user