Skip to content

Commit 0f08536

Browse files
committed
Product endpoint removed
1 parent b768664 commit 0f08536

9 files changed

Lines changed: 43 additions & 230 deletions

File tree

MockMe.API/Controllers/MockController.cs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,11 @@ public class MockController : ControllerBase
2121
public MockController(IMockService mockService) =>
2222
(_mockService) = (mockService);
2323

24-
[HttpGet("Product")]
25-
public async Task<IActionResult> Products([FromQuery(Name = "_limit")] int limit)
26-
{
27-
var items = await _mockService.GetProductsAsync(limit);
28-
return new OkObjectResult(items);
29-
}
30-
31-
[Authorize]
32-
[HttpPost("[action]")]
33-
public async Task<IActionResult> Product([FromBody] ProductRequest request)
34-
{
35-
if (request == null) return BadRequest(ModelState);
36-
37-
var data = await _mockService.ProductAdd(request);
38-
return new OkObjectResult(data);
39-
}
40-
4124
[Authorize]
4225
[HttpGet("Country")]
43-
public async Task<IActionResult> Countries()
26+
public IActionResult Countries()
4427
{
45-
var items = await _mockService.GetCountriesAsync();
28+
var items = _mockService.GetCountries();
4629
return new OkObjectResult(items);
4730
}
4831

@@ -80,15 +63,5 @@ public async Task<IActionResult> Reports()
8063
var items = Deserializer.FromJsonDictionary<Report>(json).OrderBy(o => o.Key);
8164
return new OkObjectResult(items);
8265
}
83-
84-
[Authorize]
85-
[HttpGet(nameof(Technologies))]
86-
public async Task<IActionResult> Technologies()
87-
{
88-
var path = @$"{Directory.GetCurrentDirectory()}\Shared\technologies.json";
89-
var json = await System.IO.File.ReadAllTextAsync(path);
90-
var items = Deserializer.FromJsonList<Technology>(json).OrderBy(o => o.TechnologyId);
91-
return new OkObjectResult(items);
92-
}
9366
}
9467
}

MockMe.API/Services/AssetService.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using MockMe.API.ViewModels;
44
using MockMe.Common;
55
using MockMe.Model;
6-
using MockMe.Repository;
76
using System;
87
using System.Collections.Generic;
98
using System.Linq;
@@ -19,12 +18,10 @@ public interface IAssetService
1918
public class AssetService : IAssetService
2019
{
2120
private readonly IMemoryCache cache;
22-
private readonly ITradeRepository tradeRepository;
2321

24-
public AssetService(IMemoryCache cache, ITradeRepository tradeRepository)
22+
public AssetService(IMemoryCache cache)
2523
{
2624
this.cache = cache;
27-
this.tradeRepository = tradeRepository;
2825
}
2926

3027
public async Task<IEnumerable<AssetViewModel>> GetAllAsync()
@@ -34,7 +31,7 @@ public async Task<IEnumerable<AssetViewModel>> GetAllAsync()
3431
// Cache Assets
3532
e.SetOptions(new MemoryCacheEntryOptions{ AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1) });
3633

37-
var currencyPairs = tradeRepository.CurrencyPairList();
34+
var currencyPairs = ((IEnumerable<CurrencyPair>)Enum.GetValues(typeof(CurrencyPair))).ToList();
3835
var result = currencyPairs.Select(pair =>
3936
Mapper.Map<AssetViewModel>(new Asset((int)pair, pair.Description())));
4037
return await Task.FromResult(result);

MockMe.API/Services/MockService.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
using Microsoft.Extensions.Caching.Memory;
1+
using Bogus;
2+
using Microsoft.Extensions.Caching.Memory;
23
using Microsoft.Extensions.Logging;
3-
using MockMe.Common;
44
using MockMe.Model;
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8-
using System.Threading.Tasks;
98

109
namespace MockMe.API.Services
1110
{
1211
public interface IMockService
1312
{
14-
Task<Product> ProductAdd(ProductRequest request);
15-
Task<IEnumerable<Country>> GetCountriesAsync();
16-
Task<IEnumerable<Product>> GetProductsAsync(int count);
13+
IEnumerable<Country> GetCountries();
1714
}
1815

1916
public class MockService : IMockService
@@ -24,37 +21,18 @@ public class MockService : IMockService
2421
public MockService(ILogger<MockService> logger, IMemoryCache cache) =>
2522
(_logger, _cache) = (logger, cache);
2623

27-
public async Task<Product> ProductAdd(ProductRequest request)
28-
{
29-
var data = new Product();
30-
31-
try
32-
{
33-
_logger.LogInformation("ProductRequest: {request}", request.ToJson(true));
34-
35-
data = MockUtil.GetData(request);
36-
}
37-
catch (Exception ex)
38-
{
39-
_logger.LogError("ProductAdd Error {ex}", ex);
40-
}
41-
42-
return await Task.FromResult(data);
43-
}
44-
45-
public async Task<IEnumerable<Country>> GetCountriesAsync()
24+
public IEnumerable<Country> GetCountries()
4625
{
4726
// Cache Countries for 24hrs due to high page load
48-
return await _cache.GetOrCreateAsync("Countries", async e =>
27+
return _cache.GetOrCreate("Countries", e =>
4928
{
50-
e.SetOptions(new MemoryCacheEntryOptions
51-
{
29+
e.SetOptions(new MemoryCacheEntryOptions {
5230
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
5331
});
5432

5533
try
5634
{
57-
var countries = await Task.FromResult(MockUtil.Countries(5));
35+
var countries = GenerateCountries(5);
5836
return countries.OrderBy(c => c.CountryName);
5937
}
6038
catch (Exception ex)
@@ -66,20 +44,24 @@ public async Task<IEnumerable<Country>> GetCountriesAsync()
6644
});
6745
}
6846

69-
public async Task<IEnumerable<Product>> GetProductsAsync(int count)
47+
static List<Country> GenerateCountries(int count)
7048
{
71-
var products = new List<Product>();
49+
var countries = new Faker<Country>()
50+
.RuleFor(o => o.CountryId, f => f.IndexFaker + 1)
51+
.RuleFor(o => o.CountryName, f => f.Address.Country())
52+
.RuleFor(o => o.CountryCode, f => f.Address.CountryCode())
53+
.Generate(count);
7254

73-
try
55+
if (countries.Exists(c => c.CountryName == "Australia"))
7456
{
75-
products = MockUtil.Products(count);
57+
countries.Single(c => c.CountryName == "Australia").CountryCode = "AU";
7658
}
77-
catch (Exception ex)
59+
else
7860
{
79-
_logger.LogError("GetProductsAsync Error {ex}", ex);
61+
countries.Add(new Country { CountryId = count + 1, CountryCode = "AU", CountryName = "Australia" });
8062
}
8163

82-
return await Task.FromResult(products);
64+
return countries;
8365
}
8466
}
8567
}

MockMe.API/Shared/technologies.json

Lines changed: 0 additions & 32 deletions
This file was deleted.

MockMe.Model/Product.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

MockMe.Model/ProductRequest.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

MockMe.Model/Technology.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

MockMe.Repository/MockUtil.cs

Lines changed: 0 additions & 74 deletions
This file was deleted.

MockMe.Repository/TradeRepository.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System;
1+
using Bogus;
2+
using MockMe.Common;
3+
using MockMe.Model;
4+
using System;
25
using System.Collections.Generic;
36
using System.Linq;
47
using System.Threading.Tasks;
5-
using MockMe.Common;
6-
using MockMe.Model;
78

89
namespace MockMe.Repository
910
{
@@ -14,26 +15,15 @@ public interface ITradeRepository
1415
Task<AssetTrade> UpdateAsync(Guid id, AssetTrade entity);
1516
Task<AssetTrade> DeleteAsync(Guid id);
1617
Task<AssetTrade> GenerateAsync();
17-
18-
List<CurrencyPair> CurrencyPairList();
1918
}
2019

2120
public class TradeRepository : ITradeRepository
2221
{
23-
private static List<CurrencyPair> CurrencyPairs;
24-
private static List<AssetTrade> Trades;
22+
static List<AssetTrade> Trades;
2523

2624
public TradeRepository()
2725
{
28-
if (CurrencyPairs == null)
29-
{
30-
CurrencyPairs = MockUtil.CurrencyPairs.ToList();
31-
}
32-
33-
if (Trades == null)
34-
{
35-
Trades = MockUtil.Trades(10);
36-
}
26+
Trades ??= GenerateTrades(10);
3727
}
3828

3929
public async Task<IEnumerable<AssetTrade>> GetAllAsync()
@@ -69,13 +59,25 @@ public async Task<AssetTrade> DeleteAsync(Guid id)
6959

7060
public async Task<AssetTrade> GenerateAsync()
7161
{
72-
var randomTrade = MockUtil.Trades(1).FirstOrDefault();
62+
var randomTrade = GenerateTrades(1).FirstOrDefault();
7363
return await Task.FromResult(randomTrade);
7464
}
7565

76-
public List<CurrencyPair> CurrencyPairList()
66+
List<AssetTrade> GenerateTrades(int count)
7767
{
78-
return CurrencyPairs;
68+
var currencyPairs = ((IEnumerable<CurrencyPair>)Enum.GetValues(typeof(CurrencyPair))).ToList();
69+
var trades = new Faker<AssetTrade>()
70+
.RuleFor(o => o.Id, f => Guid.NewGuid().ToString())
71+
.RuleFor(o => o.Asset, f => {
72+
var pair = new Faker().Random.ListItem((IList<CurrencyPair>)currencyPairs);
73+
return new Asset((int)pair, pair.Description());
74+
})
75+
.RuleFor(o => o.Amount, f => decimal.Parse(f.Random.Decimal(1000).ToString("0.00")))
76+
.RuleFor(o => o.Expiration, f => f.Random.Number(1, 1000))
77+
.RuleFor(o => o.Payout, f => f.Random.Number(1, 1000))
78+
.RuleFor(o => o.Direction, f => f.Random.Number(0, 1))
79+
.Generate(count);
80+
return trades;
7981
}
8082
}
8183
}

0 commit comments

Comments
 (0)