Skip to content

Commit 9ab5fca

Browse files
authored
Merge pull request #14 from fossapps/add-get-features-endpoint
feat(features): add get all features endpoint
2 parents 03466ae + 2908fb2 commit 9ab5fca

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

Feature.Manager.Api/Features/FeatureController.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using Feature.Manager.Api.Features.Exceptions;
45
using Feature.Manager.Api.Features.ViewModels;
@@ -113,5 +114,30 @@ public async Task<IActionResult> ResetFeatToken(string featId)
113114
});
114115
}
115116
}
117+
118+
[HttpGet]
119+
[ProducesResponseType(typeof(IEnumerable<Feature>), StatusCodes.Status200OK)]
120+
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
121+
public async Task<IActionResult> GetFeatureList()
122+
{
123+
try
124+
{
125+
return Ok(await _featureService.GetAllFeatures());
126+
}
127+
catch (UnknownDbException)
128+
{
129+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails
130+
{
131+
Title = "unknown error"
132+
});
133+
}
134+
catch (Exception e)
135+
{
136+
return StatusCode(StatusCodes.Status500InternalServerError, new ProblemDetails
137+
{
138+
Title = "unknown error"
139+
});
140+
}
141+
}
116142
}
117143
}

Feature.Manager.Api/Features/FeatureRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Threading.Tasks;
23
using Feature.Manager.Api.Features.ViewModels;
34
using Feature.Manager.Api.Models;
@@ -11,6 +12,7 @@ public interface IFeatureRepository
1112
Task<Feature> FindByFeatId(string featId);
1213
Task<Feature> CreateFeature(CreateFeatureRequest request);
1314
Task<Feature> ResetFeatureToken(string featId, string newToken);
15+
Task<List<Feature>> All();
1416
}
1517
public class FeatureRepository : IFeatureRepository
1618
{
@@ -50,5 +52,10 @@ public async Task<Feature> ResetFeatureToken(string featId, string newToken)
5052
await _db.SaveChangesAsync();
5153
return feature;
5254
}
55+
56+
public async Task<List<Feature>> All()
57+
{
58+
return await _db.Features.AsNoTracking().ToListAsync();
59+
}
5360
}
5461
}

Feature.Manager.Api/Features/FeatureService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using Feature.Manager.Api.Features.Exceptions;
45
using Feature.Manager.Api.Features.ViewModels;
@@ -11,6 +12,7 @@ public interface IFeatureService
1112
Task<Feature> Create(CreateFeatureRequest request);
1213
Task<Feature> ResetFeatureToken(string featId);
1314
Task<Feature> GetFeatureByFeatId(string featId);
15+
Task<List<Feature>> GetAllFeatures();
1416
}
1517

1618
public class FeatureService : IFeatureService
@@ -78,5 +80,17 @@ public async Task<Feature> GetFeatureByFeatId(string featId)
7880
throw new UnknownDbException(e.Message);
7981
}
8082
}
83+
84+
public async Task<List<Feature>> GetAllFeatures()
85+
{
86+
try
87+
{
88+
return await _featureRepository.All();
89+
}
90+
catch (Exception e)
91+
{
92+
throw new UnknownDbException(e.Message);
93+
}
94+
}
8195
}
8296
}

Feature.Manager.UnitTest/Features/FeatureServiceTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using Feature.Manager.Api.Features;
45
using Feature.Manager.Api.Features.Exceptions;
@@ -13,10 +14,30 @@ public class FeatureServiceTest
1314
{
1415
private Mock<IFeatureRepository> _mockRepository;
1516
private FeatureService _systemUnderTest;
17+
private readonly List<Api.Features.Feature> _featureList = new List<Api.Features.Feature>
18+
{
19+
new Api.Features.Feature
20+
{
21+
Description = "desc1",
22+
Hypothesis = "hypo2",
23+
Id = "123123123",
24+
FeatId = "APP-1",
25+
FeatureToken = "2134123123"
26+
},
27+
new Api.Features.Feature
28+
{
29+
Description = "desc2",
30+
Hypothesis = "hypo2",
31+
Id = "098098098",
32+
FeatId = "APP-1",
33+
FeatureToken = "sdfalsdjfkdf"
34+
},
35+
};
1636
[SetUp]
1737
public void Setup()
1838
{
1939
var repo = new Mock<IFeatureRepository>();
40+
repo.Setup(x => x.All()).ReturnsAsync(_featureList);
2041
repo.Setup(x => x.FindByFeatId("TEST-123")).ReturnsAsync(new Api.Features.Feature
2142
{
2243
Description = "test description",
@@ -152,5 +173,21 @@ public async Task GetFeatureByFeatIdHandlesExceptions()
152173
{
153174
Assert.ThrowsAsync<UnknownDbException>(async () => await _systemUnderTest.GetFeatureByFeatId("TEST-3"));
154175
}
176+
177+
[Test]
178+
public async Task GetAllFeaturesHandlesExceptions()
179+
{
180+
var mock = new Mock<IFeatureRepository>();
181+
mock.Setup(x => x.All()).ThrowsAsync(new InvalidCastException());
182+
var systemUnderTest = new FeatureService(mock.Object, null);
183+
Assert.ThrowsAsync<UnknownDbException>(() => systemUnderTest.GetAllFeatures());
184+
}
185+
186+
[Test]
187+
public async Task GetAllFeaturesReturnsFromRepo()
188+
{
189+
var result = await _systemUnderTest.GetAllFeatures();
190+
Assert.AreSame(_featureList, result);
191+
}
155192
}
156193
}

0 commit comments

Comments
 (0)