diff options
| author | transtrike <transtrike@gmail.com> | 2020-12-08 20:38:05 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2020-12-08 20:38:05 +0200 |
| commit | 2bcaa82e555100ffbcf6b23dd330e7bddd67cad6 (patch) | |
| tree | da5338cd470cf08a31cb2ea2b2fa14bd02b109bf /API/Database | |
| parent | ce4e06a235efda69c72d34815863d0b367577301 (diff) | |
| download | DevHive-2bcaa82e555100ffbcf6b23dd330e7bddd67cad6.tar DevHive-2bcaa82e555100ffbcf6b23dd330e7bddd67cad6.tar.gz DevHive-2bcaa82e555100ffbcf6b23dd330e7bddd67cad6.zip | |
DbRepository implemented
Diffstat (limited to 'API/Database')
| -rw-r--r-- | API/Database/DbRepository.cs | 65 | ||||
| -rw-r--r-- | API/Database/Interfaces/IRepository.cs | 24 |
2 files changed, 88 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 diff --git a/API/Database/Interfaces/IRepository.cs b/API/Database/Interfaces/IRepository.cs new file mode 100644 index 0000000..927e006 --- /dev/null +++ b/API/Database/Interfaces/IRepository.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Camera_Shop.Database +{ + public interface IRepository<TEntity> + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Return *count* instances of Entity from the database + Task<IAsyncEnumerable<TEntity>> Query(int count); + + //Find entity by id + Task<TEntity> FindByIdAsync(object id); + + //Modify Entity from database + Task EditAsync(object id, TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(object id); + } +}
\ No newline at end of file |
