aboutsummaryrefslogtreecommitdiff
path: root/API/Database/DbRepository.cs
diff options
context:
space:
mode:
Diffstat (limited to 'API/Database/DbRepository.cs')
-rw-r--r--API/Database/DbRepository.cs65
1 files changed, 64 insertions, 1 deletions
diff --git a/API/Database/DbRepository.cs b/API/Database/DbRepository.cs
index a166682..5540104 100644
--- a/API/Database/DbRepository.cs
+++ b/API/Database/DbRepository.cs
@@ -1,4 +1,67 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Camera_Shop.Database;
+using Microsoft.EntityFrameworkCore;
+
namespace API.Database
{
-
+ public class DbRepository<TEntity> : IRepository<TEntity>
+ where TEntity : class
+ {
+ private readonly DbContext _context;
+ public DbRepository(DbContext context)
+ {
+ _context = context;
+ }
+
+ //Create
+ public async Task AddAsync(TEntity entity)
+ {
+ await this._context
+ .Set<TEntity>()
+ .AddAsync(entity);
+
+ await this._context.SaveChangesAsync();
+ }
+
+ //Read
+ public async Task<TEntity> FindByIdAsync(object id)
+ {
+ return await this._context
+ .Set<TEntity>()
+ .FindAsync(id);
+ }
+
+ public async Task<IAsyncEnumerable<TEntity>> Query(int count)
+ {
+ return this._context
+ .Set<TEntity>()
+ .Take(count)
+ .AsAsyncEnumerable();
+ }
+
+ //Update
+ public async Task EditAsync(object id, TEntity newEntity)
+ {
+ //Set the Id property to the given id
+ TEntity entity = await FindByIdAsync(id);
+
+ this._context.Entry(entity)
+ .CurrentValues
+ .SetValues(newEntity);
+
+ await this._context.SaveChangesAsync();
+ }
+
+ //Delete
+ public async Task DeleteAsync(object id)
+ {
+ TEntity entity = await FindByIdAsync(id);
+
+ this._context.Set<TEntity>().Remove(entity);
+
+ await this._context.SaveChangesAsync();
+ }
+ }
} \ No newline at end of file