OddsService + OddController with GetOddForCountry and GetAll (with odd in the request)

This commit is contained in:
Thomas Fransolet 2020-01-14 19:57:34 +01:00
parent 8ec099f688
commit 4668d80d4a
10 changed files with 265 additions and 40 deletions

View File

@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Server;
using MyCore.DTO;
using MyCore.Models;
using MyCore.Services;
using static MyCore.Services.OddService;
namespace MyCore.Controllers
{
[Authorize(Roles = "Admin")]
[Route("api/odd")]
[ApiController]
public class OddController : ControllerBase
{
private OddService oddService = new OddService(OddService.RegionOdd.UK);
/// <summary>
/// Get odds for one country and one odd value maximum
/// </summary>
/// <param name="id">id of country, e.g = BE for Belgium</param>
/// <param name="oddRequest">Odd Maximum value</param>
[AllowAnonymous]
[ProducesResponseType(typeof(List<OddNice>), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("country/{id}/{oddRequest}")]
public async Task<ActionResult<List<OddNice>>> GetForCountry(string id, double oddRequest)
{
try
{
try
{
League leagueTest = new League(id);
}
catch (Exception ex)
{
return new ObjectResult("The league you mentionned not exists") { StatusCode = 404 };
}
var result = await GetOddsForCountry(id, oddRequest);
return new OkObjectResult(result);
}
catch (Exception e)
{
Console.WriteLine(e);
return new ObjectResult("The league you mentionned not exists") { StatusCode = 500 };
}
}
/// <summary>
/// Get odds for one country and one odd value maximum
/// </summary>
/// <param name="id">id of country, e.g = BE for Belgium</param>
/// <param name="oddRequest">Odd Maximum value</param>
[AllowAnonymous]
[ProducesResponseType(typeof(List<OddNice>), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{oddRequest}")]
public async Task<ActionResult<List<OddNice>>> GetAll(double oddRequest)
{
try
{
List<OddNice> oddToSend = new List<OddNice>();
foreach (var league in oddService.allLeagues)
{
var result = await GetOddsForCountry(league.Identifiant, oddRequest);
foreach (var element in result)
{
oddToSend.Add(element);
}
}
return new OkObjectResult(oddToSend);
}
catch (Exception e)
{
Console.WriteLine(e);
return new ObjectResult("The league you mentionned not exists") { StatusCode = 500 };
}
}
public async Task<List<OddNice>> GetOddsForCountry(string id, double oddRequest)
{
League league = new League(id);
var result = await oddService.GetOddsForLeague(league);
List<OddNice> oddToKeep = new List<OddNice>();
foreach (var odd in result)
{
if (odd.Sites.Where(s => s.Site_key == "unibet" && s.Odds.H2h.Where(o => o <= oddRequest).Count() > 0).Count() > 0)
{
var unibet = odd.Sites.Where(s => s.Site_key == "unibet").ToList().FirstOrDefault();
OddNice oddNice = new OddNice();
oddNice.Commence_time = odd.Commence_time;
oddNice.Home_team = odd.Home_team;
oddNice.Teams = odd.Teams;
oddNice.Odds = new OddH2H();
oddNice.Odds.HomeOdd = unibet.Odds.H2h.ToArray()[0];
oddNice.Odds.VisitOdd = unibet.Odds.H2h.ToArray()[1];
oddNice.Odds.DrawOdd = unibet.Odds.H2h.ToArray()[2];
oddToKeep.Add(oddNice);
}
}
return oddToKeep;
}
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
}
}
}

View File

@ -18,8 +18,6 @@ namespace MyCore.Controllers
[ApiController] [ApiController]
public class ValuesController : ControllerBase public class ValuesController : ControllerBase
{ {
private OddService oddService = new OddService(OddService.RegionOdd.UK);
// GET api/values // GET api/values
/// <summary> /// <summary>
@ -32,28 +30,6 @@ namespace MyCore.Controllers
{ {
WidgetWeather widgetWeather = new WidgetWeather(); WidgetWeather widgetWeather = new WidgetWeather();
try
{
League league = new League("BE");
oddService.GetOddsForLeague(league).ContinueWith(res =>
{
if (res.Status == TaskStatus.RanToCompletion)
{
var test = res.Result;
}
else
{
}
}); ;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return new string[] { "value1", "value2" }; return new string[] { "value1", "value2" };
} }

View File

@ -20,13 +20,26 @@ namespace MyCore.DTO
public string Site_key; public string Site_key;
public string Site_nice; public string Site_nice;
public int Last_update; public int Last_update;
public OddMatch OddMatch; public OddMatch Odds;
} }
public class OddMatch public class OddMatch
{
public IEnumerable<double> H2h;
}
public class OddH2H
{ {
public double HomeOdd; public double HomeOdd;
public double DrawOdd; public double DrawOdd;
public double VisitOdd; public double VisitOdd;
} }
public class OddNice
{
public List<string> Teams;
public int Commence_time;
public string Home_team;
public OddH2H Odds;
}
} }

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyCore.DTO
{
public class RequestParam
{
private const int maxPageCount = 50;
public int? Page { get; set; }
private int? _pageCount = maxPageCount;
public int? PageCount
{
get { return _pageCount; }
set { _pageCount = (value > maxPageCount) ? maxPageCount : value; }
}
public string SiteId { get; set; }
public string OrderBy { get; set; } = "Name";
}
}

View File

@ -1,8 +1,11 @@
using MyCore.DTO; using MyCore.DTO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
@ -21,8 +24,10 @@ namespace MyCore.Services
public class League public class League
{ {
public string ApiIdentifiant { get; set; } public string ApiIdentifiant { get; set; }
public string Identifiant { get; set; }
public League(string league) { public League(string league) {
Identifiant = league;
switch (league) switch (league)
{ {
case "BE": case "BE":
@ -84,27 +89,67 @@ namespace MyCore.Services
_url = $"{_servicePoint}apiKey={_apiKey}&region={_region}"; _url = $"{_servicePoint}apiKey={_apiKey}&region={_region}";
_client.BaseAddress = new Uri(_url); _client.BaseAddress = new Uri(_url);
// TO REVIEW !
League leagueBE = new League("BE");
League leagueUK = new League("UK");
League leagueDK = new League("DK");
League leagueFR = new League("FR");
League leagueDE = new League("DE");
League leagueIT = new League("IT");
League leagueNE = new League("NE");
League leaguePT = new League("PT");
League leagueRU = new League("RU");
League leagueES = new League("ES");
League leagueCH = new League("CH");
League leagueTR = new League("TR");
allLeagues.Add(leagueBE);
allLeagues.Add(leagueUK);
allLeagues.Add(leagueDK);
allLeagues.Add(leagueFR);
allLeagues.Add(leagueDE);
allLeagues.Add(leagueIT);
allLeagues.Add(leagueNE);
allLeagues.Add(leaguePT);
allLeagues.Add(leagueRU);
allLeagues.Add(leagueES);
allLeagues.Add(leagueCH);
allLeagues.Add(leagueTR);
} }
public async Task<List<Odd>> GetOddsForLeague(League league) public async Task<List<Odd>> GetOddsForLeague(League league)
{ {
List<Odd> oddsToReturn = new List<Odd>(); try
{
var builder = new UriBuilder(_servicePoint); using (HttpClient client = new HttpClient())
builder.Port = -1; {
var query = HttpUtility.ParseQueryString(builder.Query); var builder = new UriBuilder(_servicePoint);
query["apiKey"] = _apiKey; builder.Port = -1;
query["region"] = _region; var query = HttpUtility.ParseQueryString(builder.Query);
query["sport"] = league.ApiIdentifiant; query["apiKey"] = _apiKey;
builder.Query = query.ToString(); query["region"] = _region;
string url = builder.ToString(); query["sport"] = league.ApiIdentifiant;
builder.Query = query.ToString();
string url = builder.ToString();
_client.BaseAddress = new Uri(url); _client.BaseAddress = new Uri(url);
var result = await _client.GetAsync(url);
Console.WriteLine(result.StatusCode); var response = await client.GetAsync(url);
if (response != null)
return oddsToReturn; {
var jsonString = await response.Content.ReadAsStringAsync();
var data = ((JObject)JsonConvert.DeserializeObject(jsonString))["data"];
return JsonConvert.DeserializeObject<List<Odd>>(data.ToString());
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return null;
} }
public List<Odd> GetOddsForAllLeagues() public List<Odd> GetOddsForAllLeagues()

View File

@ -56,6 +56,20 @@
It's a mqtt publish test ! :) It's a mqtt publish test ! :)
</summary> </summary>
</member> </member>
<member name="M:MyCore.Controllers.OddController.GetForCountry(System.String,System.Double)">
<summary>
Get odds for one country and one odd value maximum
</summary>
<param name="id">id of country, e.g = BE for Belgium</param>
<param name="oddRequest">Odd Maximum value</param>
</member>
<member name="M:MyCore.Controllers.OddController.GetAll(System.Double)">
<summary>
Get odds for one country and one odd value maximum
</summary>
<param name="id">id of country, e.g = BE for Belgium</param>
<param name="oddRequest">Odd Maximum value</param>
</member>
<member name="M:MyCore.Controllers.UserController.Get"> <member name="M:MyCore.Controllers.UserController.Get">
<summary> <summary>
Get a list of user Get a list of user

View File

@ -56,6 +56,20 @@
It's a mqtt publish test ! :) It's a mqtt publish test ! :)
</summary> </summary>
</member> </member>
<member name="M:MyCore.Controllers.OddController.GetForCountry(System.String,System.Double)">
<summary>
Get odds for one country and one odd value maximum
</summary>
<param name="id">id of country, e.g = BE for Belgium</param>
<param name="oddRequest">Odd Maximum value</param>
</member>
<member name="M:MyCore.Controllers.OddController.GetAll(System.Double)">
<summary>
Get odds for one country and one odd value maximum
</summary>
<param name="id">id of country, e.g = BE for Belgium</param>
<param name="oddRequest">Odd Maximum value</param>
</member>
<member name="M:MyCore.Controllers.UserController.Get"> <member name="M:MyCore.Controllers.UserController.Get">
<summary> <summary>
Get a list of user Get a list of user