Skip to content

Commit

Permalink
Merge pull request #60 from MagicTheGathering/mtg-53
Browse files Browse the repository at this point in the history
Added the formats endpoint
  • Loading branch information
jregnier authored Dec 2, 2020
2 parents f34173b + ab5f2b1 commit d436fcf
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 74 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ var result = await service.GetCardSuperTypesAsync();
ICardService service = serviceProvider.GetCardService();
var result = await service.GetCardSubTypesAsync();
```
#### Get all game formats
```cs
ICardService service = serviceProvider.GetCardService();
var result = await service.GetFormatsAsync();
```
#### Find a set by code
```cs
ISetService service = serviceProvider.GetSetService();
Expand Down
36 changes: 19 additions & 17 deletions src/MtgApiManager.Lib.Test/Core/ApiEndPointsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,12 @@

namespace MtgApiManager.Lib.Test.Core
{
public class ApiEndPointsTests
{
[Theory]
[ClassData(typeof(ApiEndPointIdTestData))]
public void Id_Correct(int id, int expectedValue)
{
Assert.Equal(expectedValue, id);
}

[Theory]
[ClassData(typeof(ApiEndPointNameTestData))]
public void Name_Correct(string name, string expectedValue)
{
Assert.Equal(expectedValue, name);
}
}

public class ApiEndPointIdTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { ApiEndPoint.Cards.Id, 1 };
yield return new object[] { ApiEndPoint.Formats.Id, 6 };
yield return new object[] { ApiEndPoint.None.Id, 0 };
yield return new object[] { ApiEndPoint.Sets.Id, 2 };
yield return new object[] { ApiEndPoint.SubTypes.Id, 5 };
Expand All @@ -43,6 +27,7 @@ public class ApiEndPointNameTestData : IEnumerable<object[]>
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { ApiEndPoint.Cards.Name, nameof(ApiEndPoint.Cards).ToLower() };
yield return new object[] { ApiEndPoint.Formats.Name, nameof(ApiEndPoint.Formats).ToLower() };
yield return new object[] { ApiEndPoint.None.Name, nameof(ApiEndPoint.None).ToLower() };
yield return new object[] { ApiEndPoint.Sets.Name, nameof(ApiEndPoint.Sets).ToLower() };
yield return new object[] { ApiEndPoint.SubTypes.Name, nameof(ApiEndPoint.SubTypes).ToLower() };
Expand All @@ -52,4 +37,21 @@ public IEnumerator<object[]> GetEnumerator()

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public class ApiEndPointsTests
{
[Theory]
[ClassData(typeof(ApiEndPointIdTestData))]
public void Id_Correct(int id, int expectedValue)
{
Assert.Equal(expectedValue, id);
}

[Theory]
[ClassData(typeof(ApiEndPointNameTestData))]
public void Name_Correct(string name, string expectedValue)
{
Assert.Equal(expectedValue, name);
}
}
}
65 changes: 46 additions & 19 deletions src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

public class CardServiceTest
{
/// <summary>
/// Tests the <see cref="CardService.AllAsync"/> method.
/// </summary>
/// <returns>The asynchronous task.</returns>
[Fact]
public async Task AllAsyncTest()
{
Expand Down Expand Up @@ -164,10 +160,6 @@ public async Task AllAsyncTest()
Assert.NotNull(result.Value);
}

/// <summary>
/// Tests the <see cref="CardService.FindAsync(int)"/> methods.
/// </summary>
/// <returns>The asynchronous task.</returns>
[Fact]
public async Task FindAsyncTest()
{
Expand Down Expand Up @@ -308,10 +300,6 @@ public async Task FindAsyncTest()
Assert.NotNull(result.Value);
}

/// <summary>
/// Tests the <see cref="CardService.GetCardSubTypesAsync"/> method.
/// </summary>
/// <returns>The asynchronous task.</returns>
[Fact]
public async Task GetCardSubTypesAsyncTest()
{
Expand Down Expand Up @@ -440,10 +428,6 @@ public async Task GetCardSuperTypesAsyncTest()
Assert.NotNull(result.Value);
}

/// <summary>
/// Tests the <see cref="CardService.GetCardTypesAsync"/> method.
/// </summary>
/// <returns>The asynchronous task.</returns>
[Fact]
public async Task GetCardTypesAsyncTest()
{
Expand Down Expand Up @@ -506,9 +490,52 @@ public async Task GetCardTypesAsyncTest()
Assert.NotNull(result.Value);
}

/// <summary>
/// Tests the <see cref="CardService.Where{U}(System.Linq.Expressions.Expression{Func{CardDto, U}}, string)"/> method.
/// </summary>
[Fact]
public async Task GetFormatsAsync_Failure()
{
// Given
const string EXCEPTION_MESSAGE = "didn't work";
var moqAdapter = new Mock<IMtgApiServiceAdapter>(MockBehavior.Strict);
moqAdapter
.Setup(x => x.WebGetAsync<RootCardFormatsDto>(new Uri("https://api.magicthegathering.io/v1/formats")))
.Throws(new Exception(EXCEPTION_MESSAGE));
var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false);
var service = serviceProvider.GetCardService();

// When
var result = await service.GetFormatsAsync();

// Then
Assert.False(result.IsSuccess);
Assert.NotNull(result.Exception);
}

[Fact]
public async Task GetFormatsAsync_Success()
{
// Given
var formats = new List<string>
{
"format1",
"format2",
"format3",
};

var moqAdapter = new Mock<IMtgApiServiceAdapter>(MockBehavior.Strict);
moqAdapter
.Setup(x => x.WebGetAsync<RootCardFormatsDto>(new Uri("https://api.magicthegathering.io/v1/formats")))
.ReturnsAsync(new RootCardFormatsDto { Formats = formats });
var serviceProvider = new MtgServiceProvider(moqAdapter.Object, new ModelMapper(), false);
var service = serviceProvider.GetCardService();

// When
var result = await service.GetFormatsAsync();

// Then
Assert.True(result.IsSuccess);
Assert.Equal(formats.Count, result.Value.Count);
}

[Fact]
public void WhereTest()
{
Expand Down
89 changes: 56 additions & 33 deletions src/MtgApiManager.Lib.TestApp/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ namespace MtgApiManager.Lib.TestApp
public class MainViewModel : ViewModelBase
{
private readonly IMtgServiceProvider _serviceProvider;
private ObservableCollection<ICard> _cardsCollection = null;
private RelayCommand _cardSearchCommand;
private string _cardSearchString = null;
private RelayCommand _findSelectedCardCommand;
private RelayCommand _findSelectedSetCommand;
private RelayCommand _generateBoosterCommand;
private ObservableCollection<ICard> _generatedBoosterCollection = null;
private RelayCommand _getCardFormatsCommand;
private RelayCommand _getCardSubTypesCommand;
private RelayCommand _getCardSuperTypesCommand;
private RelayCommand _getCardTypesCommand;
Expand All @@ -24,24 +23,18 @@ public class MainViewModel : ViewModelBase
private string _selectedCardId = null;
private ISet _selectedSet = null;
private string _selectedSetCode = null;
private ObservableCollection<ISet> _setsCollection = null;
private RelayCommand _setSearchCommand;
private string _setSearchString = null;

private ObservableCollection<string> _typesCollection = null;

public MainViewModel()
{
_cardsCollection = new ObservableCollection<ICard>();
_setsCollection = new ObservableCollection<ISet>();
CardsCollection = new ObservableCollection<ICard>();
SetsCollection = new ObservableCollection<ISet>();
TypesCollection = new ObservableCollection<string>();
_serviceProvider = new MtgServiceProvider();
}

public ObservableCollection<ICard> CardsCollection
{
get => _cardsCollection;
set => Set(() => CardsCollection, ref _cardsCollection, value);
}
public ObservableCollection<ICard> CardsCollection { get; }

public RelayCommand CardSearchCommand
{
Expand All @@ -61,13 +54,13 @@ public RelayCommand CardSearchCommand
var result = await cardService.AllAsync();
_cardsCollection.Clear();
CardsCollection.Clear();
if (result.IsSuccess)
{
foreach (var item in result.Value)
{
_cardsCollection.Add(item);
CardsCollection.Add(item);
}
}
Expand Down Expand Up @@ -141,9 +134,13 @@ public RelayCommand GenerateBoosterCommand
ISetService setService = _serviceProvider.GetSetService();
var result = await setService.GenerateBoosterAsync(_selectedSetCode);
GeneratedBoosterCollection.Clear();
if (result.IsSuccess)
{
GeneratedBoosterCollection = new ObservableCollection<ICard>(result.Value);
foreach (var item in result.Value)
{
GeneratedBoosterCollection.Add(item);
}
}
IsLoading = false;
Expand All @@ -152,10 +149,32 @@ public RelayCommand GenerateBoosterCommand
}
}

public ObservableCollection<ICard> GeneratedBoosterCollection
public ObservableCollection<ICard> GeneratedBoosterCollection { get; }

public RelayCommand GetCardFormatsCommand
{
get => _generatedBoosterCollection;
set => Set(() => GeneratedBoosterCollection, ref _generatedBoosterCollection, value);
get
{
return _getCardFormatsCommand ??= new RelayCommand(
async () =>
{
IsLoading = true;
ICardService cardService = _serviceProvider.GetCardService();
var result = await cardService.GetFormatsAsync();
TypesCollection.Clear();
if (result.IsSuccess)
{
foreach (var item in result.Value)
{
TypesCollection.Add(item);
}
}
IsLoading = false;
});
}
}

public RelayCommand GetCardSubTypesCommand
Expand All @@ -170,9 +189,13 @@ public RelayCommand GetCardSubTypesCommand
ICardService cardService = _serviceProvider.GetCardService();
var result = await cardService.GetCardSubTypesAsync();
TypesCollection.Clear();
if (result.IsSuccess)
{
TypesCollection = new ObservableCollection<string>(result.Value);
foreach (var item in result.Value)
{
TypesCollection.Add(item);
}
}
IsLoading = false;
Expand All @@ -192,9 +215,13 @@ public RelayCommand GetCardSuperTypesCommand
ICardService cardService = _serviceProvider.GetCardService();
var result = await cardService.GetCardSuperTypesAsync();
TypesCollection.Clear();
if (result.IsSuccess)
{
TypesCollection = new ObservableCollection<string>(result.Value);
foreach (var item in result.Value)
{
TypesCollection.Add(item);
}
}
IsLoading = false;
Expand All @@ -214,9 +241,13 @@ public RelayCommand GetCardTypesCommand
ICardService cardService = _serviceProvider.GetCardService();
var result = await cardService.GetCardTypesAsync();
TypesCollection.Clear();
if (result.IsSuccess)
{
TypesCollection = new ObservableCollection<string>(result.Value);
foreach (var item in result.Value)
{
TypesCollection.Add(item);
}
}
IsLoading = false;
Expand Down Expand Up @@ -254,11 +285,7 @@ public string SelectedSetCode
set => Set(() => SelectedSetCode, ref _selectedSetCode, value);
}

public ObservableCollection<ISet> SetsCollection
{
get => _setsCollection;
set => Set(() => SetsCollection, ref _setsCollection, value);
}
public ObservableCollection<ISet> SetsCollection { get; }

public RelayCommand SetSearchCommand
{
Expand All @@ -278,13 +305,13 @@ public RelayCommand SetSearchCommand
var result = await setService.AllAsync();
_setsCollection.Clear();
SetsCollection.Clear();
if (result.IsSuccess)
{
foreach (var item in result.Value)
{
_setsCollection.Add(item);
SetsCollection.Add(item);
}
}
Expand All @@ -300,10 +327,6 @@ public string SetSearchString
set => Set(() => SetSearchString, ref _setSearchString, value);
}

public ObservableCollection<string> TypesCollection
{
get => _typesCollection;
set => Set(() => TypesCollection, ref _typesCollection, value);
}
public ObservableCollection<string> TypesCollection { get; }
}
}
Loading

0 comments on commit d436fcf

Please sign in to comment.