From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../DevHive.Data/Repositories/BaseRepository.cs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/Data/DevHive.Data/Repositories/BaseRepository.cs (limited to 'src/Data/DevHive.Data/Repositories/BaseRepository.cs') diff --git a/src/Data/DevHive.Data/Repositories/BaseRepository.cs b/src/Data/DevHive.Data/Repositories/BaseRepository.cs new file mode 100644 index 0000000..ece372e --- /dev/null +++ b/src/Data/DevHive.Data/Repositories/BaseRepository.cs @@ -0,0 +1,59 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Repositories.Interfaces; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class BaseRepository : IRepository + where TEntity : class + { + private readonly DbContext _context; + + public BaseRepository(DbContext context) + { + this._context = context; + } + + public virtual async Task AddAsync(TEntity entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await this.SaveChangesAsync(); + } + + public virtual async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public virtual async Task EditAsync(Guid id, TEntity newEntity) + { + var entry = this._context.Entry(newEntity); + if (entry.State == EntityState.Detached) + this._context.Attach(newEntity); + + entry.State = EntityState.Modified; + + return await this.SaveChangesAsync(); + } + + public virtual async Task DeleteAsync(TEntity entity) + { + this._context.Remove(entity); + + return await this.SaveChangesAsync(); + } + + public virtual async Task SaveChangesAsync() + { + int result = await _context.SaveChangesAsync(); + + return result >= 1; + } + } +} -- cgit v1.2.3