From b95eedbf7cb1a11817e8ef1507d110c8de123c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Andr=C3=A9?= Date: Sun, 10 Nov 2019 19:31:55 +0100 Subject: [PATCH] Update the repository pattern --- .../AircraftService.cs | 13 ++++----- .../DropZoneService.cs | 4 +-- Back/skydiveLogs-api.Business/JumpService.cs | 4 +-- .../JumpTypeService.cs | 12 +++++---- Back/skydiveLogs-api.Business/StatsService.cs | 10 +++---- .../AircraftRepository.cs | 4 +-- .../DropZoneRepository.cs | 25 ++++++++--------- Back/skydiveLogs-api.Data/GearRepository.cs | 4 +-- .../Interface/IAircraftRepository.cs | 10 +++---- .../Interface/IDataProvider.cs | 12 +++++++++ .../Interface/IDropZoneRepository.cs | 11 +++----- .../Interface/IGearRepository.cs | 7 +++-- .../Interface/IJumpRepository.cs | 10 +++---- .../Interface/IJumpTypeRepository.cs | 10 +++---- .../Interface/IRepository.cs | 12 +++++++++ Back/skydiveLogs-api.Data/JumpRepository.cs | 4 +-- .../JumpTypeRepository.cs | 4 +-- Back/skydiveLogs-api.Data/LiteDbProvider.cs | 27 +++++++++++++++++++ Back/skydiveLogs-api.Ioc/IocService.cs | 2 ++ 19 files changed, 112 insertions(+), 73 deletions(-) create mode 100644 Back/skydiveLogs-api.Data/Interface/IDataProvider.cs create mode 100644 Back/skydiveLogs-api.Data/Interface/IRepository.cs create mode 100644 Back/skydiveLogs-api.Data/LiteDbProvider.cs diff --git a/Back/skydiveLogs-api.Business/AircraftService.cs b/Back/skydiveLogs-api.Business/AircraftService.cs index 49ce2b5..fd7e205 100644 --- a/Back/skydiveLogs-api.Business/AircraftService.cs +++ b/Back/skydiveLogs-api.Business/AircraftService.cs @@ -1,10 +1,11 @@ -using skydiveLogs_api.Business.Interface; -using skydiveLogs_api.Model; -using System; +using System; using System.Collections.Generic; -using System.Text; + +using skydiveLogs_api.Business.Interface; +using skydiveLogs_api.Model; using skydiveLogs_api.Data.Interface; + namespace skydiveLogs_api.Business { public class AircraftService : IAircraftService @@ -26,12 +27,12 @@ namespace skydiveLogs_api.Business public Aircraft GetAircraftById(int id) { - return _aircraftRepository.GetAircraftById(id); + return _aircraftRepository.GetById(id); } public IEnumerable GetAllAircrafts() { - return _aircraftRepository.GetAllAircrafts(); + return _aircraftRepository.GetAll(); } public void UpdateAircraft(int id, Aircraft aircraft) diff --git a/Back/skydiveLogs-api.Business/DropZoneService.cs b/Back/skydiveLogs-api.Business/DropZoneService.cs index 3a129c4..23abe8d 100644 --- a/Back/skydiveLogs-api.Business/DropZoneService.cs +++ b/Back/skydiveLogs-api.Business/DropZoneService.cs @@ -27,12 +27,12 @@ namespace skydiveLogs_api.Business public IEnumerable GetAllDzs() { - return _dropZoneRepository.GetAllDzs(); + return _dropZoneRepository.GetAll(); } public DropZone GetDzById(int id) { - return _dropZoneRepository.GetDzById(id); + return _dropZoneRepository.GetById(id); } public void UpdateDz(int id, DropZone dropZone) diff --git a/Back/skydiveLogs-api.Business/JumpService.cs b/Back/skydiveLogs-api.Business/JumpService.cs index a9fad38..548d4be 100644 --- a/Back/skydiveLogs-api.Business/JumpService.cs +++ b/Back/skydiveLogs-api.Business/JumpService.cs @@ -27,12 +27,12 @@ namespace skydiveLogs_api.Business public IEnumerable GetAllJumps() { - return _jumpRepository.GetAllJumps(); + return _jumpRepository.GetAll(); } public Jump GetJumpById(int id) { - return _jumpRepository.GetJumpById(id); + return _jumpRepository.GetById(id); } public void UpdateJump(int id, Jump jump) diff --git a/Back/skydiveLogs-api.Business/JumpTypeService.cs b/Back/skydiveLogs-api.Business/JumpTypeService.cs index cd8ffb6..b16f499 100644 --- a/Back/skydiveLogs-api.Business/JumpTypeService.cs +++ b/Back/skydiveLogs-api.Business/JumpTypeService.cs @@ -1,9 +1,11 @@ -using skydiveLogs_api.Business.Interface; -using skydiveLogs_api.Model; -using System; +using System; using System.Collections.Generic; + +using skydiveLogs_api.Business.Interface; +using skydiveLogs_api.Model; using skydiveLogs_api.Data.Interface; + namespace skydiveLogs_api.Business { public class JumpTypeService : IJumpTypeService @@ -25,12 +27,12 @@ namespace skydiveLogs_api.Business public IEnumerable GetAllJumpTypes() { - return _jumpTypeRepository.GetAllJumpTypes(); + return _jumpTypeRepository.GetAll(); } public JumpType GetJumpTypeById(int id) { - return _jumpTypeRepository.GetJumpTypeById(id); + return _jumpTypeRepository.GetById(id); } public void UpdateJumpType(int id, JumpType value) diff --git a/Back/skydiveLogs-api.Business/StatsService.cs b/Back/skydiveLogs-api.Business/StatsService.cs index d16ac02..74ba612 100644 --- a/Back/skydiveLogs-api.Business/StatsService.cs +++ b/Back/skydiveLogs-api.Business/StatsService.cs @@ -17,7 +17,7 @@ namespace skydiveLogs_api.Business public IEnumerable GetStatsByAircraft() { - var allJumps = _jumpRepository.GetAllJumps(); + var allJumps = _jumpRepository.GetAll(); return allJumps.GroupBy(j => j.AircraftId, j => j, @@ -31,7 +31,7 @@ namespace skydiveLogs_api.Business public IEnumerable GetStatsByDz() { - var allJumps = _jumpRepository.GetAllJumps(); + var allJumps = _jumpRepository.GetAll(); return allJumps.GroupBy(j => j.DropZoneId, j => j, @@ -45,7 +45,7 @@ namespace skydiveLogs_api.Business public IEnumerable GetStatsByJumpType() { - var allJumps = _jumpRepository.GetAllJumps(); + var allJumps = _jumpRepository.GetAll(); return allJumps.GroupBy(j => j.JumpTypeId, j => j, @@ -59,7 +59,7 @@ namespace skydiveLogs_api.Business public IEnumerable GetStatsByRig() { - var allJumps = _jumpRepository.GetAllJumps(); + var allJumps = _jumpRepository.GetAll(); return allJumps.GroupBy(j => j.GearId, j => j, @@ -73,7 +73,7 @@ namespace skydiveLogs_api.Business public IEnumerable GetStatsByYear() { - var allJumps = _jumpRepository.GetAllJumps(); + var allJumps = _jumpRepository.GetAll(); return allJumps.GroupBy(j => j.JumpDate.Year, j => j, diff --git a/Back/skydiveLogs-api.Data/AircraftRepository.cs b/Back/skydiveLogs-api.Data/AircraftRepository.cs index 7b9c57d..e9b345a 100644 --- a/Back/skydiveLogs-api.Data/AircraftRepository.cs +++ b/Back/skydiveLogs-api.Data/AircraftRepository.cs @@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data { public class AircraftRepository : IAircraftRepository { - public IEnumerable GetAllAircrafts() + public IEnumerable GetAll() { IEnumerable result = new List(); @@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data return result; } - public Aircraft GetAircraftById(int id) + public Aircraft GetById(int id) { Aircraft result; diff --git a/Back/skydiveLogs-api.Data/DropZoneRepository.cs b/Back/skydiveLogs-api.Data/DropZoneRepository.cs index 559f874..f9ff870 100644 --- a/Back/skydiveLogs-api.Data/DropZoneRepository.cs +++ b/Back/skydiveLogs-api.Data/DropZoneRepository.cs @@ -11,21 +11,18 @@ namespace skydiveLogs_api.Data { public class DropZoneRepository : IDropZoneRepository { - public IEnumerable GetAllDzs() + public DropZoneRepository(IDataProvider dataProvider) { - IEnumerable result = new List(); - - using (var db = new LiteDatabase(@".\Data\MyData.db")) - { - var col = db.GetCollection("DropZone"); - - result = col.FindAll().ToList(); - } - - return result; + _dataProvider = dataProvider; + _col = _dataProvider.GetCollection(); } - public DropZone GetDzById(int id) + public IEnumerable GetAll() + { + return _col.FindAll().ToList(); + } + + public DropZone GetById(int id) { DropZone result; @@ -38,5 +35,9 @@ namespace skydiveLogs_api.Data return result; } + + private readonly IDataProvider _dataProvider; + + private readonly LiteCollection _col; } } diff --git a/Back/skydiveLogs-api.Data/GearRepository.cs b/Back/skydiveLogs-api.Data/GearRepository.cs index dea28de..df68165 100644 --- a/Back/skydiveLogs-api.Data/GearRepository.cs +++ b/Back/skydiveLogs-api.Data/GearRepository.cs @@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data { public class GearRepository : IGearRepository { - public IEnumerable GetAllGears() + public IEnumerable GetAll() { IEnumerable result = new List(); @@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data return result; } - public Gear GetGearById(int id) + public Gear GetById(int id) { Gear result; diff --git a/Back/skydiveLogs-api.Data/Interface/IAircraftRepository.cs b/Back/skydiveLogs-api.Data/Interface/IAircraftRepository.cs index 0e02305..4e0fab6 100644 --- a/Back/skydiveLogs-api.Data/Interface/IAircraftRepository.cs +++ b/Back/skydiveLogs-api.Data/Interface/IAircraftRepository.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using skydiveLogs_api.Model; +using skydiveLogs_api.Model; + namespace skydiveLogs_api.Data.Interface { - public interface IAircraftRepository + public interface IAircraftRepository : IRepository { - IEnumerable GetAllAircrafts(); - Aircraft GetAircraftById(int id); } } diff --git a/Back/skydiveLogs-api.Data/Interface/IDataProvider.cs b/Back/skydiveLogs-api.Data/Interface/IDataProvider.cs new file mode 100644 index 0000000..b304134 --- /dev/null +++ b/Back/skydiveLogs-api.Data/Interface/IDataProvider.cs @@ -0,0 +1,12 @@ +using LiteDB; + + +namespace skydiveLogs_api.Data.Interface +{ + public interface IDataProvider + { + LiteCollection GetCollection(); + + void Close(); + } +} diff --git a/Back/skydiveLogs-api.Data/Interface/IDropZoneRepository.cs b/Back/skydiveLogs-api.Data/Interface/IDropZoneRepository.cs index 8d744df..c63d336 100644 --- a/Back/skydiveLogs-api.Data/Interface/IDropZoneRepository.cs +++ b/Back/skydiveLogs-api.Data/Interface/IDropZoneRepository.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using skydiveLogs_api.Model; +using skydiveLogs_api.Model; + namespace skydiveLogs_api.Data.Interface { - public interface IDropZoneRepository + public interface IDropZoneRepository : IRepository { - IEnumerable GetAllDzs(); - - DropZone GetDzById(int id); } } diff --git a/Back/skydiveLogs-api.Data/Interface/IGearRepository.cs b/Back/skydiveLogs-api.Data/Interface/IGearRepository.cs index 9821582..96c54b9 100644 --- a/Back/skydiveLogs-api.Data/Interface/IGearRepository.cs +++ b/Back/skydiveLogs-api.Data/Interface/IGearRepository.cs @@ -1,10 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; +using skydiveLogs_api.Model; + namespace skydiveLogs_api.Data.Interface { - public interface IGearRepository + public interface IGearRepository : IRepository { } } diff --git a/Back/skydiveLogs-api.Data/Interface/IJumpRepository.cs b/Back/skydiveLogs-api.Data/Interface/IJumpRepository.cs index c5c226a..b6f1012 100644 --- a/Back/skydiveLogs-api.Data/Interface/IJumpRepository.cs +++ b/Back/skydiveLogs-api.Data/Interface/IJumpRepository.cs @@ -1,14 +1,10 @@ -using System.Collections.Generic; -using skydiveLogs_api.Model; +using skydiveLogs_api.Model; + namespace skydiveLogs_api.Data.Interface { - public interface IJumpRepository + public interface IJumpRepository : IRepository { - IEnumerable GetAllJumps(); - - Jump GetJumpById(int id); - bool AddJump(Jump newJump); } } diff --git a/Back/skydiveLogs-api.Data/Interface/IJumpTypeRepository.cs b/Back/skydiveLogs-api.Data/Interface/IJumpTypeRepository.cs index 7eb08f7..4334b92 100644 --- a/Back/skydiveLogs-api.Data/Interface/IJumpTypeRepository.cs +++ b/Back/skydiveLogs-api.Data/Interface/IJumpTypeRepository.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using skydiveLogs_api.Model; +using skydiveLogs_api.Model; + namespace skydiveLogs_api.Data.Interface { - public interface IJumpTypeRepository + public interface IJumpTypeRepository : IRepository { - IEnumerable GetAllJumpTypes(); - JumpType GetJumpTypeById(int id); } } diff --git a/Back/skydiveLogs-api.Data/Interface/IRepository.cs b/Back/skydiveLogs-api.Data/Interface/IRepository.cs new file mode 100644 index 0000000..0652f6a --- /dev/null +++ b/Back/skydiveLogs-api.Data/Interface/IRepository.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; + +namespace skydiveLogs_api.Data.Interface +{ + public interface IRepository + { + IEnumerable GetAll(); + + T GetById(int id); + + } +} \ No newline at end of file diff --git a/Back/skydiveLogs-api.Data/JumpRepository.cs b/Back/skydiveLogs-api.Data/JumpRepository.cs index 731b2cb..ffcd378 100644 --- a/Back/skydiveLogs-api.Data/JumpRepository.cs +++ b/Back/skydiveLogs-api.Data/JumpRepository.cs @@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data { public class JumpRepository : IJumpRepository { - public IEnumerable GetAllJumps() + public IEnumerable GetAll() { IEnumerable result = new List(); @@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data return result; } - public Jump GetJumpById(int id) + public Jump GetById(int id) { Jump result; diff --git a/Back/skydiveLogs-api.Data/JumpTypeRepository.cs b/Back/skydiveLogs-api.Data/JumpTypeRepository.cs index 1bab9d7..069fd23 100644 --- a/Back/skydiveLogs-api.Data/JumpTypeRepository.cs +++ b/Back/skydiveLogs-api.Data/JumpTypeRepository.cs @@ -11,7 +11,7 @@ namespace skydiveLogs_api.Data { public class JumpTypeRepository : IJumpTypeRepository { - public IEnumerable GetAllJumpTypes() + public IEnumerable GetAll() { IEnumerable result = new List(); @@ -25,7 +25,7 @@ namespace skydiveLogs_api.Data return result; } - public JumpType GetJumpTypeById(int id) + public JumpType GetById(int id) { JumpType result; diff --git a/Back/skydiveLogs-api.Data/LiteDbProvider.cs b/Back/skydiveLogs-api.Data/LiteDbProvider.cs new file mode 100644 index 0000000..e04f36a --- /dev/null +++ b/Back/skydiveLogs-api.Data/LiteDbProvider.cs @@ -0,0 +1,27 @@ +using LiteDB; + +using skydiveLogs_api.Data.Interface; + + +namespace skydiveLogs_api.Data +{ + public class LiteDbProvider : IDataProvider + { + public LiteDbProvider(string connectionString) + { + _db = new LiteDatabase(connectionString); + } + + public LiteCollection GetCollection() + { + return _db.GetCollection(); + } + + public void Close() + { + _db.Dispose(); + } + + private readonly LiteDatabase _db; + } +} diff --git a/Back/skydiveLogs-api.Ioc/IocService.cs b/Back/skydiveLogs-api.Ioc/IocService.cs index d2a70a0..e9b1ae3 100644 --- a/Back/skydiveLogs-api.Ioc/IocService.cs +++ b/Back/skydiveLogs-api.Ioc/IocService.cs @@ -27,6 +27,8 @@ namespace skydiveLogs_api.Ioc _services.AddScoped(); _services.AddScoped(); _services.AddScoped(); + + _services.AddScoped(); } private readonly IServiceCollection _services;