mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
OddsService + OddController with GetOddForCountry and GetAll (with odd in the request)
This commit is contained in:
parent
8ec099f688
commit
4668d80d4a
Binary file not shown.
Binary file not shown.
Binary file not shown.
142
MyCore/Controllers/OddController.cs
Normal file
142
MyCore/Controllers/OddController.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -18,8 +18,6 @@ namespace MyCore.Controllers
|
||||
[ApiController]
|
||||
public class ValuesController : ControllerBase
|
||||
{
|
||||
private OddService oddService = new OddService(OddService.RegionOdd.UK);
|
||||
|
||||
|
||||
// GET api/values
|
||||
/// <summary>
|
||||
@ -32,28 +30,6 @@ namespace MyCore.Controllers
|
||||
{
|
||||
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" };
|
||||
}
|
||||
|
||||
|
||||
@ -20,13 +20,26 @@ namespace MyCore.DTO
|
||||
public string Site_key;
|
||||
public string Site_nice;
|
||||
public int Last_update;
|
||||
public OddMatch OddMatch;
|
||||
public OddMatch Odds;
|
||||
}
|
||||
|
||||
public class OddMatch
|
||||
{
|
||||
public IEnumerable<double> H2h;
|
||||
}
|
||||
|
||||
public class OddH2H
|
||||
{
|
||||
public double HomeOdd;
|
||||
public double DrawOdd;
|
||||
public double VisitOdd;
|
||||
}
|
||||
|
||||
public class OddNice
|
||||
{
|
||||
public List<string> Teams;
|
||||
public int Commence_time;
|
||||
public string Home_team;
|
||||
public OddH2H Odds;
|
||||
}
|
||||
}
|
||||
|
||||
21
MyCore/DTO/RequestParam.cs
Normal file
21
MyCore/DTO/RequestParam.cs
Normal 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";
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,11 @@
|
||||
using MyCore.DTO;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
@ -21,8 +24,10 @@ namespace MyCore.Services
|
||||
public class League
|
||||
{
|
||||
public string ApiIdentifiant { get; set; }
|
||||
public string Identifiant { get; set; }
|
||||
|
||||
public League(string league) {
|
||||
Identifiant = league;
|
||||
switch (league)
|
||||
{
|
||||
case "BE":
|
||||
@ -84,27 +89,67 @@ namespace MyCore.Services
|
||||
_url = $"{_servicePoint}apiKey={_apiKey}®ion={_region}";
|
||||
|
||||
_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)
|
||||
{
|
||||
List<Odd> oddsToReturn = new List<Odd>();
|
||||
try
|
||||
{
|
||||
|
||||
var builder = new UriBuilder(_servicePoint);
|
||||
builder.Port = -1;
|
||||
var query = HttpUtility.ParseQueryString(builder.Query);
|
||||
query["apiKey"] = _apiKey;
|
||||
query["region"] = _region;
|
||||
query["sport"] = league.ApiIdentifiant;
|
||||
builder.Query = query.ToString();
|
||||
string url = builder.ToString();
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
var builder = new UriBuilder(_servicePoint);
|
||||
builder.Port = -1;
|
||||
var query = HttpUtility.ParseQueryString(builder.Query);
|
||||
query["apiKey"] = _apiKey;
|
||||
query["region"] = _region;
|
||||
query["sport"] = league.ApiIdentifiant;
|
||||
builder.Query = query.ToString();
|
||||
string url = builder.ToString();
|
||||
|
||||
_client.BaseAddress = new Uri(url);
|
||||
var result = await _client.GetAsync(url);
|
||||
_client.BaseAddress = new Uri(url);
|
||||
|
||||
Console.WriteLine(result.StatusCode);
|
||||
|
||||
return oddsToReturn;
|
||||
var response = await client.GetAsync(url);
|
||||
if (response != null)
|
||||
{
|
||||
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()
|
||||
|
||||
@ -56,6 +56,20 @@
|
||||
It's a mqtt publish test ! :)
|
||||
</summary>
|
||||
</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">
|
||||
<summary>
|
||||
Get a list of user
|
||||
|
||||
@ -56,6 +56,20 @@
|
||||
It's a mqtt publish test ! :)
|
||||
</summary>
|
||||
</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">
|
||||
<summary>
|
||||
Get a list of user
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user