142 lines
5.0 KiB
C#

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 Exception : {ex}") { 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="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 };
}
}
private 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
}*/
}
}