Skip to content

Commit

Permalink
Merge pull request #14 from fossapps/add-get-features-endpoint
Browse files Browse the repository at this point in the history
feat(features): add get all features endpoint
  • Loading branch information
cyberhck authored Jul 11, 2020
2 parents 03466ae + 2908fb2 commit 9ab5fca
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Feature.Manager.Api/Features/FeatureController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -113,5 +114,30 @@ public async Task<IActionResult> ResetFeatToken(string featId)
});
}
}

[HttpGet]
[ProducesResponseType(typeof(IEnumerable<Feature>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> 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"
});
}
}
}
}
7 changes: 7 additions & 0 deletions Feature.Manager.Api/Features/FeatureRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Feature.Manager.Api.Features.ViewModels;
using Feature.Manager.Api.Models;
Expand All @@ -11,6 +12,7 @@ public interface IFeatureRepository
Task<Feature> FindByFeatId(string featId);
Task<Feature> CreateFeature(CreateFeatureRequest request);
Task<Feature> ResetFeatureToken(string featId, string newToken);
Task<List<Feature>> All();
}
public class FeatureRepository : IFeatureRepository
{
Expand Down Expand Up @@ -50,5 +52,10 @@ public async Task<Feature> ResetFeatureToken(string featId, string newToken)
await _db.SaveChangesAsync();
return feature;
}

public async Task<List<Feature>> All()
{
return await _db.Features.AsNoTracking().ToListAsync();
}
}
}
14 changes: 14 additions & 0 deletions Feature.Manager.Api/Features/FeatureService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,7 @@ public interface IFeatureService
Task<Feature> Create(CreateFeatureRequest request);
Task<Feature> ResetFeatureToken(string featId);
Task<Feature> GetFeatureByFeatId(string featId);
Task<List<Feature>> GetAllFeatures();
}

public class FeatureService : IFeatureService
Expand Down Expand Up @@ -78,5 +80,17 @@ public async Task<Feature> GetFeatureByFeatId(string featId)
throw new UnknownDbException(e.Message);
}
}

public async Task<List<Feature>> GetAllFeatures()
{
try
{
return await _featureRepository.All();
}
catch (Exception e)
{
throw new UnknownDbException(e.Message);
}
}
}
}
37 changes: 37 additions & 0 deletions Feature.Manager.UnitTest/Features/FeatureServiceTest.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,10 +14,30 @@ public class FeatureServiceTest
{
private Mock<IFeatureRepository> _mockRepository;
private FeatureService _systemUnderTest;
private readonly List<Api.Features.Feature> _featureList = new List<Api.Features.Feature>
{
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<IFeatureRepository>();
repo.Setup(x => x.All()).ReturnsAsync(_featureList);
repo.Setup(x => x.FindByFeatId("TEST-123")).ReturnsAsync(new Api.Features.Feature
{
Description = "test description",
Expand Down Expand Up @@ -152,5 +173,21 @@ public async Task GetFeatureByFeatIdHandlesExceptions()
{
Assert.ThrowsAsync<UnknownDbException>(async () => await _systemUnderTest.GetFeatureByFeatId("TEST-3"));
}

[Test]
public async Task GetAllFeaturesHandlesExceptions()
{
var mock = new Mock<IFeatureRepository>();
mock.Setup(x => x.All()).ThrowsAsync(new InvalidCastException());
var systemUnderTest = new FeatureService(mock.Object, null);
Assert.ThrowsAsync<UnknownDbException>(() => systemUnderTest.GetAllFeatures());
}

[Test]
public async Task GetAllFeaturesReturnsFromRepo()
{
var result = await _systemUnderTest.GetAllFeatures();
Assert.AreSame(_featureList, result);
}
}
}

0 comments on commit 9ab5fca

Please sign in to comment.