Skip to content

Commit 328fa19

Browse files
authoredApr 2, 2024··
Merge pull request #43 from DiyorMarket/create-repositories-for-infrastructure-project
Created all repositories and created EntityNotFoundExeption
2 parents 176c420 + e9e9490 commit 328fa19

12 files changed

+331
-8
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace Inflow.Domain.Exeptions
1+
namespace Inflow.Domain.Exeptions
82
{
9-
internal class EntityNotFoundException
3+
public class EntityNotFoundException : Exception
104
{
5+
public EntityNotFoundException()
6+
{
7+
}
8+
9+
public EntityNotFoundException(string message)
10+
: base(message)
11+
{
12+
}
13+
14+
public EntityNotFoundException(string message, Exception innerException)
15+
: base(message, innerException)
16+
{
17+
}
1118
}
1219
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Inflow.Domain.Entities;
2+
using Microsoft.EntityFrameworkCore;
3+
using System.Reflection;
4+
5+
namespace Inflow.Infrastructure
6+
{
7+
public class InflowDbContext : DbContext
8+
{
9+
public virtual DbSet<Category> Categories { get; set; }
10+
public virtual DbSet<Product> Products { get; set; }
11+
public virtual DbSet<Customer> Customers { get; set; }
12+
public virtual DbSet<Sale> Sales { get; set; }
13+
public virtual DbSet<SaleItem> SaleItems { get; set; }
14+
public virtual DbSet<Supplier> Suppliers { get; set; }
15+
public virtual DbSet<Supply> Supplies { get; set; }
16+
public virtual DbSet<SupplyItem> SupplyItems { get; set; }
17+
public virtual DbSet<User> Users { get; set; }
18+
19+
public InflowDbContext(DbContextOptions<InflowDbContext> options)
20+
: base(options)
21+
{
22+
//Database.Migrate();
23+
}
24+
25+
protected override void OnModelCreating(ModelBuilder modelBuilder)
26+
{
27+
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
28+
base.OnModelCreating(modelBuilder);
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class CategoryRepository : RepositoryBase<Category>, ICategoryRepository
8+
{
9+
public CategoryRepository(InflowDbContext context) : base(context)
10+
{
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using Inflow.Domain.Interfaces.Repositories;
2+
using Inflow.Infrastructure;
3+
4+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
5+
{
6+
public class CommonRepository : ICommonRepository
7+
{
8+
private readonly InflowDbContext _context;
9+
10+
private ICategoryRepository _category;
11+
public ICategoryRepository Category
12+
{
13+
get
14+
{
15+
_category ??= new CategoryRepository(_context);
16+
17+
return _category;
18+
}
19+
}
20+
21+
private IProductRepository _product;
22+
public IProductRepository Product
23+
{
24+
get
25+
{
26+
_product ??= new ProductRepository(_context);
27+
28+
return _product;
29+
}
30+
}
31+
32+
private ICustomerRepository _customer;
33+
public ICustomerRepository Customer
34+
{
35+
get
36+
{
37+
_customer ??= new CustomerRepository(_context);
38+
39+
return _customer;
40+
}
41+
}
42+
43+
private ISaleRepository _sale;
44+
public ISaleRepository Sale
45+
{
46+
get
47+
{
48+
_sale ??= new SaleRepository(_context);
49+
50+
return _sale;
51+
}
52+
}
53+
54+
private ISaleItemRepository _saleItem;
55+
public ISaleItemRepository SaleItem
56+
{
57+
get
58+
{
59+
_saleItem ??= new SaleItemRepository(_context);
60+
61+
return _saleItem;
62+
}
63+
}
64+
65+
private ISupplierRepository _supplier;
66+
public ISupplierRepository Supplier
67+
{
68+
get
69+
{
70+
_supplier ??= new SupplierRepository(_context);
71+
72+
return _supplier;
73+
}
74+
}
75+
76+
private ISupplyRepository _supply;
77+
public ISupplyRepository Supply
78+
{
79+
get
80+
{
81+
_supply ??= new SupplyRepository(_context);
82+
83+
return _supply;
84+
}
85+
}
86+
87+
private ISupplyItemRepository _supplyItem;
88+
public ISupplyItemRepository SupplyItem
89+
{
90+
get
91+
{
92+
_supplyItem ??= new SupplyItemRepository(_context);
93+
94+
return _supplyItem;
95+
}
96+
}
97+
public CommonRepository(InflowDbContext context)
98+
{
99+
_context = context;
100+
101+
_category = new CategoryRepository(context);
102+
_product = new ProductRepository(context);
103+
_customer = new CustomerRepository(context);
104+
_sale = new SaleRepository(context);
105+
_saleItem = new SaleItemRepository(context);
106+
_supplier = new SupplierRepository(context);
107+
_supply = new SupplyRepository(context);
108+
_supplyItem = new SupplyItemRepository(context);
109+
}
110+
111+
public int SaveChanges()
112+
{
113+
return _context.SaveChanges();
114+
}
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class CustomerRepository : RepositoryBase<Customer>, ICustomerRepository
8+
{
9+
public CustomerRepository(InflowDbContext context) : base(context)
10+
{
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class ProductRepository : RepositoryBase<Product>, IProductRepository
8+
{
9+
public ProductRepository(InflowDbContext context)
10+
: base(context)
11+
{
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Exeptions;
3+
using Inflow.Domain.Interfaces.Repositories;
4+
using Inflow.Infrastructure;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
8+
{
9+
public class RepositoryBase<T> : IRepositoryBase<T> where T : EntityBase
10+
{
11+
protected readonly InflowDbContext _context;
12+
13+
public RepositoryBase(InflowDbContext context)
14+
{
15+
_context = context;
16+
}
17+
18+
public T Create(T entity)
19+
{
20+
var createdEntity = _context.Set<T>().Add(entity);
21+
22+
return createdEntity.Entity;
23+
}
24+
25+
public void Delete(int id)
26+
{
27+
var entity = FindById(id);
28+
29+
_context.Set<T>().Remove(entity);
30+
}
31+
32+
public IEnumerable<T> FindAll()
33+
{
34+
var entities = _context.Set<T>()
35+
.AsNoTracking()
36+
.ToList();
37+
38+
return entities;
39+
}
40+
41+
public T FindById(int id)
42+
{
43+
var entity = _context.Set<T>()
44+
.Find(id);
45+
46+
if (entity is null)
47+
{
48+
throw new EntityNotFoundException(
49+
$"Entity {typeof(T)} with id: {id} not found.");
50+
}
51+
52+
return entity;
53+
}
54+
55+
public void Update(T entity)
56+
{
57+
_context.Set<T>().Update(entity);
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class SaleItemRepository : RepositoryBase<SaleItem>, ISaleItemRepository
8+
{
9+
public SaleItemRepository(InflowDbContext context)
10+
: base(context)
11+
{
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class SaleRepository : RepositoryBase<Sale>, ISaleRepository
8+
{
9+
public SaleRepository(InflowDbContext context)
10+
: base(context)
11+
{
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class SupplierRepository : RepositoryBase<Supplier>, ISupplierRepository
8+
{
9+
public SupplierRepository(InflowDbContext context) : base(context)
10+
{
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class SupplyItemRepository : RepositoryBase<SupplyItem>, ISupplyItemRepository
8+
{
9+
public SupplyItemRepository(InflowDbContext context)
10+
: base(context)
11+
{
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Inflow.Domain.Entities;
2+
using Inflow.Domain.Interfaces.Repositories;
3+
using Inflow.Infrastructure;
4+
5+
namespace DiyorMarket.Infrastructure.Persistence.Repositories
6+
{
7+
public class SupplyRepository : RepositoryBase<Supply>, ISupplyRepository
8+
{
9+
public SupplyRepository(InflowDbContext context)
10+
: base(context)
11+
{
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)
Please sign in to comment.