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); /// /// Get odds for one country and one odd value maximum /// /// id of country, e.g = BE for Belgium /// Odd Maximum value [AllowAnonymous] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("country/{id}/{oddRequest}")] public async Task>> 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 }; } } /// /// Get odds for one country and one odd value maximum /// /// Odd Maximum value [AllowAnonymous] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{oddRequest}")] public async Task>> GetAll(double oddRequest) { try { List oddToSend = new List(); 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> GetOddsForCountry(string id, double oddRequest) { League league = new League(id); var result = await oddService.GetOddsForLeague(league); List oddToKeep = new List(); 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 } } }