From 66c6931703e67b5f0ff10fd16e9bd706ae3b3e36 Mon Sep 17 00:00:00 2001 From: Nishchal Date: Sun, 12 Jul 2020 00:02:04 +0700 Subject: [PATCH] feat(features): add get all features endpoint --- .../Features/FeatureController.cs | 22 +++++++++++ .../Features/FeatureRepository.cs | 7 ++++ .../Features/FeatureService.cs | 14 +++++++ .../Features/FeatureServiceTest.cs | 37 +++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/Feature.Manager.Api/Features/FeatureController.cs b/Feature.Manager.Api/Features/FeatureController.cs index 7787058..8d47189 100644 --- a/Feature.Manager.Api/Features/FeatureController.cs +++ b/Feature.Manager.Api/Features/FeatureController.cs @@ -113,5 +113,27 @@ public async Task ResetFeatToken(string featId) }); } } + + public async Task GetFeatureList() + { + try + { + return Ok(await _featureService.GetAllFeatures()); + } + catch (UnknownDbException) + { + return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails + { + Title = "unknown error" + }); + } + catch (Exception e) + { + return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails + { + Title = "unknown error" + }); + } + } } } diff --git a/Feature.Manager.Api/Features/FeatureRepository.cs b/Feature.Manager.Api/Features/FeatureRepository.cs index ab346dc..c67f8d6 100644 --- a/Feature.Manager.Api/Features/FeatureRepository.cs +++ b/Feature.Manager.Api/Features/FeatureRepository.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Feature.Manager.Api.Features.ViewModels; using Feature.Manager.Api.Models; @@ -11,6 +12,7 @@ public interface IFeatureRepository Task FindByFeatId(string featId); Task CreateFeature(CreateFeatureRequest request); Task ResetFeatureToken(string featId, string newToken); + Task> All(); } public class FeatureRepository : IFeatureRepository { @@ -50,5 +52,10 @@ public async Task ResetFeatureToken(string featId, string newToken) await _db.SaveChangesAsync(); return feature; } + + public async Task> All() + { + return await _db.Features.AsNoTracking().ToListAsync(); + } } } diff --git a/Feature.Manager.Api/Features/FeatureService.cs b/Feature.Manager.Api/Features/FeatureService.cs index 5524b86..c949a1f 100644 --- a/Feature.Manager.Api/Features/FeatureService.cs +++ b/Feature.Manager.Api/Features/FeatureService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Feature.Manager.Api.Features.Exceptions; using Feature.Manager.Api.Features.ViewModels; @@ -11,6 +12,7 @@ public interface IFeatureService Task Create(CreateFeatureRequest request); Task ResetFeatureToken(string featId); Task GetFeatureByFeatId(string featId); + Task> GetAllFeatures(); } public class FeatureService : IFeatureService @@ -78,5 +80,17 @@ public async Task GetFeatureByFeatId(string featId) throw new UnknownDbException(e.Message); } } + + public async Task> GetAllFeatures() + { + try + { + return await _featureRepository.All(); + } + catch (Exception e) + { + throw new UnknownDbException(e.Message); + } + } } } diff --git a/Feature.Manager.UnitTest/Features/FeatureServiceTest.cs b/Feature.Manager.UnitTest/Features/FeatureServiceTest.cs index 4c05771..1d74e40 100644 --- a/Feature.Manager.UnitTest/Features/FeatureServiceTest.cs +++ b/Feature.Manager.UnitTest/Features/FeatureServiceTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using Feature.Manager.Api.Features; using Feature.Manager.Api.Features.Exceptions; @@ -13,10 +14,30 @@ public class FeatureServiceTest { private Mock _mockRepository; private FeatureService _systemUnderTest; + private readonly List _featureList = new List + { + new Api.Features.Feature + { + Description = "desc1", + Hypothesis = "hypo2", + Id = "123123123", + FeatId = "APP-1", + FeatureToken = "2134123123" + }, + new Api.Features.Feature + { + Description = "desc2", + Hypothesis = "hypo2", + Id = "098098098", + FeatId = "APP-1", + FeatureToken = "sdfalsdjfkdf" + }, + }; [SetUp] public void Setup() { var repo = new Mock(); + repo.Setup(x => x.All()).ReturnsAsync(_featureList); repo.Setup(x => x.FindByFeatId("TEST-123")).ReturnsAsync(new Api.Features.Feature { Description = "test description", @@ -152,5 +173,21 @@ public async Task GetFeatureByFeatIdHandlesExceptions() { Assert.ThrowsAsync(async () => await _systemUnderTest.GetFeatureByFeatId("TEST-3")); } + + [Test] + public async Task GetAllFeaturesHandlesExceptions() + { + var mock = new Mock(); + mock.Setup(x => x.All()).ThrowsAsync(new InvalidCastException()); + var systemUnderTest = new FeatureService(mock.Object, null); + Assert.ThrowsAsync(() => systemUnderTest.GetAllFeatures()); + } + + [Test] + public async Task GetAllFeaturesReturnsFromRepo() + { + var result = await _systemUnderTest.GetAllFeatures(); + Assert.AreSame(_featureList, result); + } } }