-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteRepository.cs
More file actions
58 lines (54 loc) · 1.79 KB
/
WriteRepository.cs
File metadata and controls
58 lines (54 loc) · 1.79 KB
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
using ECommerceAPI.Application.Repositories;
using ECommerceAPI.Domain.Entities.Common;
using ECommerceAPI.Persistence.Contexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ECommerceAPI.Persistence.Repositories
{
public class WriteRepository<T> : IWriteRepository<T> where T : BaseEntity
{
readonly private ECommerceAPIDbContext _context;
public WriteRepository(ECommerceAPIDbContext context)
{
_context = context;
}
public DbSet<T> Table => _context.Set<T>();
public async Task<bool> AddAsync(T model)
{
EntityEntry<T> entityEntry = await Table.AddAsync(model);
return entityEntry.State == EntityState.Added;
}
public async Task<bool> AddRangeAsync(List<T> datas)
{
await Table.AddRangeAsync(datas);
return true;
}
public bool Remove(T model)
{
EntityEntry<T> entityEntry = Table.Remove(model);
return entityEntry.State == EntityState.Deleted;
}
public bool RemoveRange(List<T> datas)
{
Table.RemoveRange(datas);
return true;
}
public async Task<bool> RemoveAsync(string id)
{
T model = await Table.FirstOrDefaultAsync(data => data.Id == Guid.Parse(id));
return Remove(model);
}
public bool Update(T model)
{
EntityEntry entityEntry = Table.Update(model);
return entityEntry.State == EntityState.Modified;
}
public async Task<int> SaveAsync()
=> await _context.SaveChangesAsync();
}
}