Добавил паттерн репозиторий

parent 95309e32
using Microsoft.EntityFrameworkCore;
using Payment.WebApi.Models.DbModels;
namespace Payment.WebApi.Infrastructures.Databases;
public class PaymentDbContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public PaymentDbContext(DbContextOptions<PaymentDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.SeedUser();
modelBuilder.SeedSupplier();
modelBuilder.SeedUserBalance();
}
}
\ No newline at end of file
namespace Payment.WebApi.Models.DbModels;
public class Supplier
{
public int Id { get; set; }
public string Name { get; set; }
}
\ No newline at end of file
namespace Payment.WebApi.Models.DbModels;
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string? MiddleName { get; set; }
public int Age { get; set; }
public List<UserBalance> Balances { get; set; }
public User()
{
Balances = new List<UserBalance>();
}
}
\ No newline at end of file
namespace Payment.WebApi.Models.DbModels;
public class UserBalance
{
public int Id { get; set; }
public decimal Amount { get; set; }
public string Currency { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
\ No newline at end of file
namespace Payment.WebApi.Repositories.Interfaces;
public interface IRepository<T>
{
void Create(T item);
void Update(T item);
void Remove(T item);
void Save();
}
\ No newline at end of file
using Payment.WebApi.Models.DbModels;
namespace Payment.WebApi.Repositories.Interfaces;
public interface ISupplierRepository : IRepository<Supplier>
{
List<Supplier> GetAll();
}
\ No newline at end of file
using Payment.WebApi.Models.DbModels;
namespace Payment.WebApi.Repositories.Interfaces;
public interface IUserRepository : IRepository<User>
{
User? GetFirstOrDefaultById(int id);
}
\ No newline at end of file
using Payment.WebApi.Infrastructures.Databases;
using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Repositories.Interfaces;
namespace Payment.WebApi.Repositories;
public class SupplierRepository : ISupplierRepository
{
private readonly PaymentDbContext _context;
public SupplierRepository(PaymentDbContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public List<Supplier> GetAll()
{
var suppliers = _context.Suppliers.ToList();
return suppliers;
}
public void Create(Supplier item)
{
_context.Suppliers.Add(item);
}
public void Update(Supplier item)
{
_context.Suppliers.Update(item);
}
public void Remove(Supplier item)
{
_context.Suppliers.Remove(item);
}
public void Save()
{
_context.SaveChanges();
}
}
\ No newline at end of file
using Payment.WebApi.Infrastructures.Databases;
using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Repositories.Interfaces;
namespace Payment.WebApi.Repositories;
public class UserRepository : IUserRepository
{
private readonly PaymentDbContext _context;
public UserRepository(PaymentDbContext context) =>
_context = context ?? throw new ArgumentNullException(nameof(context));
public User? GetFirstOrDefaultById(int id) => _context.Users.FirstOrDefault(s => s.Id == id);
public void Create(User item) => _context.Users.Add(item);
public void Update(User item) => _context.Users.Update(item);
public void Remove(User item) => _context.Users.Remove(item);
public void Save() => _context.SaveChanges();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment