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 From 99e3bbad3f4d2ab5999d0de508907e5985e721a4 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 17 Mar 2021 17:19:36 +0200 Subject: IRepository namespace fix; Useless folder removed --- src/Data/DevHive.Data/Interfaces/ICommentRepository.cs | 1 - src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs | 1 - src/Data/DevHive.Data/Interfaces/IPostRepository.cs | 1 - src/Data/DevHive.Data/Interfaces/IRatingRepository.cs | 2 -- src/Data/DevHive.Data/Interfaces/IRepository.cs | 2 +- src/Data/DevHive.Data/Interfaces/IRoleRepository.cs | 1 - src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs | 1 - src/Data/DevHive.Data/Interfaces/IUserRepository.cs | 1 - src/Data/DevHive.Data/Repositories/BaseRepository.cs | 2 +- 9 files changed, 2 insertions(+), 10 deletions(-) (limited to 'src/Data/DevHive.Data/Repositories/BaseRepository.cs') diff --git a/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs b/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs index f85849f..7b553ec 100644 --- a/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/ICommentRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs index ea0e8c6..af512c5 100644 --- a/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/ILanguageRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Interfaces/IPostRepository.cs b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs index 5a04aed..d40a487 100644 --- a/src/Data/DevHive.Data/Interfaces/IPostRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IPostRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs index db37d00..4840c59 100644 --- a/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRatingRepository.cs @@ -2,8 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - namespace DevHive.Data.Interfaces { public interface IRatingRepository : IRepository diff --git a/src/Data/DevHive.Data/Interfaces/IRepository.cs b/src/Data/DevHive.Data/Interfaces/IRepository.cs index 0d11cd3..7db8667 100644 --- a/src/Data/DevHive.Data/Interfaces/IRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace DevHive.Data.Repositories.Interfaces +namespace DevHive.Data.Interfaces { public interface IRepository where TEntity : class diff --git a/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs b/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs index a1080bb..b12772c 100644 --- a/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IRoleRepository.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs index f746d9d..91c82ea 100644 --- a/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/ITechnologyRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Interfaces/IUserRepository.cs b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs index aec0eb3..494f540 100644 --- a/src/Data/DevHive.Data/Interfaces/IUserRepository.cs +++ b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; namespace DevHive.Data.Interfaces { diff --git a/src/Data/DevHive.Data/Repositories/BaseRepository.cs b/src/Data/DevHive.Data/Repositories/BaseRepository.cs index ece372e..239a3bc 100644 --- a/src/Data/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/Data/DevHive.Data/Repositories/BaseRepository.cs @@ -1,6 +1,6 @@ using System; using System.Threading.Tasks; -using DevHive.Data.Repositories.Interfaces; +using DevHive.Data.Interfaces; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories -- cgit v1.2.3