diff --git a/README.md b/README.md index c2d59e0..a41a571 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/src/MtgApiManager.Lib.Test/Core/ApiEndPointsTests.cs b/src/MtgApiManager.Lib.Test/Core/ApiEndPointsTests.cs index 8651977..643c796 100644 --- a/src/MtgApiManager.Lib.Test/Core/ApiEndPointsTests.cs +++ b/src/MtgApiManager.Lib.Test/Core/ApiEndPointsTests.cs @@ -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 { public IEnumerator 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 }; @@ -43,6 +27,7 @@ public class ApiEndPointNameTestData : IEnumerable public IEnumerator 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() }; @@ -52,4 +37,21 @@ public IEnumerator 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); + } + } } \ No newline at end of file diff --git a/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs b/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs index ba0e12d..c20a2cb 100644 --- a/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs +++ b/src/MtgApiManager.Lib.Test/Service/CardServiceTest.cs @@ -18,10 +18,6 @@ public class CardServiceTest { - /// - /// Tests the method. - /// - /// The asynchronous task. [Fact] public async Task AllAsyncTest() { @@ -164,10 +160,6 @@ public async Task AllAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the methods. - /// - /// The asynchronous task. [Fact] public async Task FindAsyncTest() { @@ -308,10 +300,6 @@ public async Task FindAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - /// The asynchronous task. [Fact] public async Task GetCardSubTypesAsyncTest() { @@ -440,10 +428,6 @@ public async Task GetCardSuperTypesAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// - /// The asynchronous task. [Fact] public async Task GetCardTypesAsyncTest() { @@ -506,9 +490,52 @@ public async Task GetCardTypesAsyncTest() Assert.NotNull(result.Value); } - /// - /// Tests the method. - /// + [Fact] + public async Task GetFormatsAsync_Failure() + { + // Given + const string EXCEPTION_MESSAGE = "didn't work"; + var moqAdapter = new Mock(MockBehavior.Strict); + moqAdapter + .Setup(x => x.WebGetAsync(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 + { + "format1", + "format2", + "format3", + }; + + var moqAdapter = new Mock(MockBehavior.Strict); + moqAdapter + .Setup(x => x.WebGetAsync(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() { diff --git a/src/MtgApiManager.Lib.TestApp/MainViewModel.cs b/src/MtgApiManager.Lib.TestApp/MainViewModel.cs index b5e6150..01297f6 100644 --- a/src/MtgApiManager.Lib.TestApp/MainViewModel.cs +++ b/src/MtgApiManager.Lib.TestApp/MainViewModel.cs @@ -9,13 +9,12 @@ namespace MtgApiManager.Lib.TestApp public class MainViewModel : ViewModelBase { private readonly IMtgServiceProvider _serviceProvider; - private ObservableCollection _cardsCollection = null; private RelayCommand _cardSearchCommand; private string _cardSearchString = null; private RelayCommand _findSelectedCardCommand; private RelayCommand _findSelectedSetCommand; private RelayCommand _generateBoosterCommand; - private ObservableCollection _generatedBoosterCollection = null; + private RelayCommand _getCardFormatsCommand; private RelayCommand _getCardSubTypesCommand; private RelayCommand _getCardSuperTypesCommand; private RelayCommand _getCardTypesCommand; @@ -24,24 +23,18 @@ public class MainViewModel : ViewModelBase private string _selectedCardId = null; private ISet _selectedSet = null; private string _selectedSetCode = null; - private ObservableCollection _setsCollection = null; private RelayCommand _setSearchCommand; private string _setSearchString = null; - private ObservableCollection _typesCollection = null; - public MainViewModel() { - _cardsCollection = new ObservableCollection(); - _setsCollection = new ObservableCollection(); + CardsCollection = new ObservableCollection(); + SetsCollection = new ObservableCollection(); + TypesCollection = new ObservableCollection(); _serviceProvider = new MtgServiceProvider(); } - public ObservableCollection CardsCollection - { - get => _cardsCollection; - set => Set(() => CardsCollection, ref _cardsCollection, value); - } + public ObservableCollection CardsCollection { get; } public RelayCommand CardSearchCommand { @@ -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); } } @@ -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(result.Value); + foreach (var item in result.Value) + { + GeneratedBoosterCollection.Add(item); + } } IsLoading = false; @@ -152,10 +149,32 @@ public RelayCommand GenerateBoosterCommand } } - public ObservableCollection GeneratedBoosterCollection + public ObservableCollection 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 @@ -170,9 +189,13 @@ public RelayCommand GetCardSubTypesCommand ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardSubTypesAsync(); + TypesCollection.Clear(); if (result.IsSuccess) { - TypesCollection = new ObservableCollection(result.Value); + foreach (var item in result.Value) + { + TypesCollection.Add(item); + } } IsLoading = false; @@ -192,9 +215,13 @@ public RelayCommand GetCardSuperTypesCommand ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardSuperTypesAsync(); + TypesCollection.Clear(); if (result.IsSuccess) { - TypesCollection = new ObservableCollection(result.Value); + foreach (var item in result.Value) + { + TypesCollection.Add(item); + } } IsLoading = false; @@ -214,9 +241,13 @@ public RelayCommand GetCardTypesCommand ICardService cardService = _serviceProvider.GetCardService(); var result = await cardService.GetCardTypesAsync(); + TypesCollection.Clear(); if (result.IsSuccess) { - TypesCollection = new ObservableCollection(result.Value); + foreach (var item in result.Value) + { + TypesCollection.Add(item); + } } IsLoading = false; @@ -254,11 +285,7 @@ public string SelectedSetCode set => Set(() => SelectedSetCode, ref _selectedSetCode, value); } - public ObservableCollection SetsCollection - { - get => _setsCollection; - set => Set(() => SetsCollection, ref _setsCollection, value); - } + public ObservableCollection SetsCollection { get; } public RelayCommand SetSearchCommand { @@ -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); } } @@ -300,10 +327,6 @@ public string SetSearchString set => Set(() => SetSearchString, ref _setSearchString, value); } - public ObservableCollection TypesCollection - { - get => _typesCollection; - set => Set(() => TypesCollection, ref _typesCollection, value); - } + public ObservableCollection TypesCollection { get; } } } \ No newline at end of file diff --git a/src/MtgApiManager.Lib.TestApp/MainWindow.xaml b/src/MtgApiManager.Lib.TestApp/MainWindow.xaml index 6a6b228..e23a2bb 100644 --- a/src/MtgApiManager.Lib.TestApp/MainWindow.xaml +++ b/src/MtgApiManager.Lib.TestApp/MainWindow.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:MtgApiManager.Lib.TestApp" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="MainWindow" - Width="900" + Width="1000" Height="700" mc:Ignorable="d"> @@ -30,7 +30,7 @@ - + @@ -63,19 +63,18 @@ Grid.Column="4" Orientation="Horizontal">