idk something about efcore
This commit is contained in:
parent
751b08e805
commit
6732613466
8 changed files with 103 additions and 0 deletions
7
SurveyBackend/SurveyBackend.API/DTOs/UserLoginDTO.cs
Normal file
7
SurveyBackend/SurveyBackend.API/DTOs/UserLoginDTO.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace SurveyBackend.DTOs;
|
||||||
|
|
||||||
|
public record UserLoginDTO
|
||||||
|
{
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
10
SurveyBackend/SurveyBackend.API/DTOs/UserRegistrationDTO.cs
Normal file
10
SurveyBackend/SurveyBackend.API/DTOs/UserRegistrationDTO.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace SurveyBackend.DTOs;
|
||||||
|
|
||||||
|
public record UserRegistrationDTO
|
||||||
|
{
|
||||||
|
public string Email { get; set; }
|
||||||
|
public string Username { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,8 @@ namespace SurveyBackend.Core.Models;
|
||||||
public class User
|
public class User
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
public string Email { get; set; }
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
namespace SurveyBackend.Core.Repositories;
|
||||||
|
|
||||||
|
public interface IGenericRepository<T> where T : class
|
||||||
|
{
|
||||||
|
Task<T?> GetByIdAsync(int id);
|
||||||
|
Task<IEnumerable<T>> GetAllAsync();
|
||||||
|
Task AddAsync(T entity);
|
||||||
|
Task UpdateAsync(T entity);
|
||||||
|
Task DeleteAsync(T entity);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
using SurveyBackend.Core.Models;
|
||||||
|
|
||||||
|
namespace SurveyBackend.Core.Repositories;
|
||||||
|
|
||||||
|
public interface IUserRepository : IGenericRepository<User>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SurveyBackend.Core.Models;
|
||||||
|
|
||||||
|
namespace SurveyBackend.Infrastructure.Data;
|
||||||
|
|
||||||
|
public class DataContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
public DbSet<Group> Groups { get; set; }
|
||||||
|
|
||||||
|
public DataContext(DbContextOptions<DataContext> options) : base(options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using SurveyBackend.Core.Models;
|
||||||
|
using SurveyBackend.Core.Repositories;
|
||||||
|
using SurveyBackend.Infrastructure.Data;
|
||||||
|
|
||||||
|
namespace SurveyBackend.Infrastructure.Repositories;
|
||||||
|
|
||||||
|
public class UserRepository : IUserRepository
|
||||||
|
{
|
||||||
|
private readonly DataContext _context;
|
||||||
|
|
||||||
|
public UserRepository(DataContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<User?> GetByIdAsync(int id)
|
||||||
|
{
|
||||||
|
return await _context.Users.FindAsync(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<User>> GetAllAsync()
|
||||||
|
{
|
||||||
|
return await _context.Users.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task AddAsync(User entity)
|
||||||
|
{
|
||||||
|
await _context.Users.AddAsync(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateAsync(User entity)
|
||||||
|
{
|
||||||
|
_context.Users.Update(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DeleteAsync(User entity)
|
||||||
|
{
|
||||||
|
_context.Users.Remove(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,4 +6,12 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue