From 4aea17277ac0e91561ee929964f42341388d859f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Sat, 5 Dec 2020 21:46:27 +0100 Subject: [PATCH] Added base interface and service --- .../Base/IServiceBase.cs | 19 +++++ .../Base/ServiceBase.cs | 73 +++++++++++++++++++ .../SessionCompanion.Services/Class1.cs | 8 -- 3 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 SessionCompanion/SessionCompanion.Services/Base/IServiceBase.cs create mode 100644 SessionCompanion/SessionCompanion.Services/Base/ServiceBase.cs delete mode 100644 SessionCompanion/SessionCompanion.Services/Class1.cs diff --git a/SessionCompanion/SessionCompanion.Services/Base/IServiceBase.cs b/SessionCompanion/SessionCompanion.Services/Base/IServiceBase.cs new file mode 100644 index 0000000..3c0cb53 --- /dev/null +++ b/SessionCompanion/SessionCompanion.Services/Base/IServiceBase.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace SessionCompanion.Services.Base +{ + public interface IServiceBase : IDisposable + { + IEnumerable Get(); + Task Get(int id); + Task> Get(Expression> expresssion); + Task Create(TViewModel viewModel); + Task Update(int id, TViewModel viewModel); + Task Delete(int id); + Task SaveAsync(); + } +} diff --git a/SessionCompanion/SessionCompanion.Services/Base/ServiceBase.cs b/SessionCompanion/SessionCompanion.Services/Base/ServiceBase.cs new file mode 100644 index 0000000..e32ab1f --- /dev/null +++ b/SessionCompanion/SessionCompanion.Services/Base/ServiceBase.cs @@ -0,0 +1,73 @@ +using SessionCompanion.Database.Tables; +using System; +using System.Collections.Generic; +using System.Text; +using AutoMapper; +using System.Threading.Tasks; +using SessionCompanion.Database.Repositories.Base; +using System.Linq.Expressions; +using System.Linq; +using Microsoft.EntityFrameworkCore; + +namespace SessionCompanion.Services.Base +{ + public class ServiceBase : IServiceBase where TEntity : BaseEntity + { + protected IMapper Mapper { get; private set; } + protected IRepository Repository { get; private set; } + + + public ServiceBase(IMapper mapper, IRepository repository) + { + Mapper = mapper; + Repository = repository; + } + + public virtual IEnumerable Get() + { + var models = Repository.Get(); + return Mapper.Map>(models); + } + + public virtual async Task Get(int id) + { + var result = await Repository.Get(id); + return Mapper.Map(result); + } + + public virtual async Task> Get(Expression> expression) + { + var result = await Repository.Get(expression).ToListAsync(); + return result; + } + + public virtual async Task Create(TViewModel viewModel) + { + var model = Mapper.Map(viewModel); + await Repository.Create(model); + } + + public virtual async Task Update(int id, TViewModel viewModel) + { + var model = Mapper.Map(viewModel); + model.Id = id; + await Repository.Update(model); + } + + public virtual async Task Delete(int id) + { + var model = await Repository.Get(id); + Repository.Delete(model); + } + + public virtual async Task SaveAsync() + { + await Repository.Save(); + } + + public virtual void Dispose() + { + Repository.Dispose(); + } + } +} diff --git a/SessionCompanion/SessionCompanion.Services/Class1.cs b/SessionCompanion/SessionCompanion.Services/Class1.cs deleted file mode 100644 index 276ba2c..0000000 --- a/SessionCompanion/SessionCompanion.Services/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace SessionCompanion.Services -{ - public class Class1 - { - } -}