-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-repository
134 lines (110 loc) · 4.32 KB
/
generic-repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Youthifi.BusinessLogicLayer.Interface;
using Youthifi.DataAccessLayer.Models;
namespace Youthifi.BusinessLogicLayer.Services
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
//private readonly DbSet<T> dbSet;
//private readonly YouthifiContext context;
//public GenericRepository(YouthifiContext context)
//{
// this.context = context;
// dbSet = context.Set<T>();
//}
public async Task<IEnumerable<T>> GetAll(
Expression<Func<T, bool>> filter = null,
int pageNumber = 1,
int pageSize = 8,
bool isPaged = true,
Expression<Func<T, T>> select = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "")
{
var context = new YouthifiContext();
IQueryable<T> query = context.Set<T>();
foreach (var inlcudeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
query = query.Include(inlcudeProperty);
if (filter != null)
query = query.Where(filter);
if (orderBy != null)
query = orderBy(query);
if (select != null)
query = query.Select(select);
if (isPaged == true)
return await query.AsNoTracking().Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToListAsync<T>();
return await query.AsNoTracking().ToListAsync<T>();
}
public async Task<T> GetFirst(Expression<Func<T, bool>> filter)
{
var context = new YouthifiContext();
IQueryable<T> query = context.Set<T>();
return await query.FirstOrDefaultAsync(filter);
}
public async Task<T> GetById(string id)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
return await dbSet.FindAsync(id);
}
public async Task Add(T obj)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
await dbSet.AddAsync(obj);
await context.SaveChangesAsync();
}
public async Task Update(T obj)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
dbSet.Update(obj);
await context.SaveChangesAsync();
}
public async Task Update(IEnumerable<T> obj)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
dbSet.UpdateRange(obj);
await context.SaveChangesAsync();
}
public async Task Delete(string id)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
T existing = await dbSet.FindAsync(id);
dbSet.Remove(existing);
await context.SaveChangesAsync();
}
public async Task<int> GetCount(Expression<Func<T, bool>> filter = null)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
IQueryable<T> query = dbSet;
if (filter != null)
query = query.Where(filter);
return await query.CountAsync();
}
public async Task DeleteAll()
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
var medias = await dbSet.AsNoTracking().ToListAsync<T>();
dbSet.RemoveRange(medias);
await context.SaveChangesAsync();
}
public async Task Add(IEnumerable<T> obj)
{
var context = new YouthifiContext();
var dbSet = context.Set<T>();
await dbSet.AddRangeAsync(obj);
await context.SaveChangesAsync();
}
}
}
services.AddDbContext<YouthifiContext>(m => m.UseMySQL(Configuration.GetConnectionString("YouthifiConnection")), ServiceLifetime.Transient);