Challonge Provider can create tournament

This commit is contained in:
2018-10-07 15:15:11 +02:00
parent 9a9d4c7053
commit 681deda3d2
17 changed files with 245 additions and 18 deletions

View File

@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace LaDOSE.Api.Controllers
{
[Produces("application/json")]
[Route("api/[controller]")]
public class EventController : Controller
{
private IEventService _eventService;
public EventController(IEventService eventService)
{
_eventService = eventService;
}
[HttpPost]
public Event Post([FromBody]Event dto)
{
return _eventService.Create(dto);
}
[HttpGet("{id}")]
public Event Get(int id)
{
return _eventService.GetById(id);
}
[HttpGet("Generate/{dto}")]
public bool GenerateChallonge(int dto)
{
return _eventService.CreateChallonge(dto);
}
}
}

View File

@@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.4" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.2" />
</ItemGroup> </ItemGroup>

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using LaDOSE.Business.Interface; using LaDOSE.Business.Interface;
using LaDOSE.Business.Provider;
using LaDOSE.Business.Service; using LaDOSE.Business.Service;
using LaDOSE.Entity; using LaDOSE.Entity;
using LaDOSE.Entity.Context; using LaDOSE.Entity.Context;
@@ -40,6 +41,7 @@ namespace LaDOSE.Api
var MySqlDatabase = this.Configuration["MySql:Database"]; var MySqlDatabase = this.Configuration["MySql:Database"];
var MySqlUser = this.Configuration["MySql:User"]; var MySqlUser = this.Configuration["MySql:User"];
var MySqlPassword = this.Configuration["MySql:Password"]; var MySqlPassword = this.Configuration["MySql:Password"];
services.AddCors(); services.AddCors();
services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext services.AddDbContextPool<LaDOSEDbContext>( // replace "YourDbContext" with the class name of your DbContext
@@ -86,11 +88,18 @@ namespace LaDOSE.Api
}); });
// configure DI for application services // configure DI for application services
AddDIConfig(services);
}
private void AddDIConfig(IServiceCollection services)
{
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
services.AddScoped<IGameService, GameService>(); services.AddScoped<IGameService, GameService>();
services.AddScoped<IEventService, EventService>();
services.AddTransient<IChallongeProvider>(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"]));
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

View File

@@ -14,6 +14,9 @@
"User": "User", "User": "User",
"Password": "Password" "Password": "Password"
}, },
"ApiKey": {
"ChallongeApiKey": "Challonge ApiKey"
},
"AllowedHosts": "*", "AllowedHosts": "*",
"Port": 5000, "Port": 5000,
"JWTTokenSecret": "here goes the custom Secret key for authnetication" "JWTTokenSecret": "here goes the custom Secret key for authnetication"

View File

@@ -35,25 +35,29 @@ namespace LaDOSE.DiscordBot.Service
try try
{ {
List<TournamentResult> tournamentResultList = await new TournamentsQuery() List<TournamentResult> tournamentResultList = await new TournamentsQuery()
{ {
state = TournamentState.Ended state = TournamentState.Ended
} }
.call(this.ApiCaller); .call(this.ApiCaller);
var lastDate = tournamentResultList.Max(e => e.completed_at); var lastDate = tournamentResultList.Max(e => e.completed_at);
var lastRankingDate = new DateTime(lastDate.Year, lastDate.Month, lastDate.Day); if (lastDate.HasValue)
var lastTournament = tournamentResultList.Where(e => e.completed_at > lastRankingDate).ToList(); {
string returnValue = "Les derniers tournois : \n"; var lastRankingDate = new DateTime(lastDate.Value.Year, lastDate.Value.Month, lastDate.Value.Day);
foreach (var tournamentResult in lastTournament)
{
returnValue += $"{tournamentResult.name} : <https://challonge.com/{tournamentResult.url}> \n";
}
DernierTournois = returnValue; var lastTournament = tournamentResultList.Where(e => e.completed_at > lastRankingDate).ToList();
return true; string returnValue = "Les derniers tournois : \n";
foreach (var tournamentResult in lastTournament)
{
returnValue += $"{tournamentResult.name} : <https://challonge.com/{tournamentResult.url}> \n";
}
DernierTournois = returnValue;
}
return true;
} }
catch catch
{ {

View File

@@ -21,12 +21,16 @@ namespace LaDOSE.Entity.Context
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<SeasonGame>() modelBuilder.Entity<SeasonGame>()
.HasKey(t => new { t.SeasonId, t.GameId }); .HasKey(t => new { t.SeasonId, t.GameId });
modelBuilder.Entity<EventGame>()
.HasKey(t => new { t.EventId, t.GameId });
modelBuilder.Entity<Event>() modelBuilder.Entity<Event>()
.HasOne(s => s.Season) .HasOne(s => s.Season)
.WithMany(p => p.Event) .WithMany(p => p.Event)
.HasForeignKey(fk => fk.SeasonId); .HasForeignKey(fk => fk.SeasonId);
modelBuilder.Entity<SeasonGame>() modelBuilder.Entity<SeasonGame>()
.HasOne(pt => pt.Season) .HasOne(pt => pt.Season)
.WithMany(p => p.Games) .WithMany(p => p.Games)
@@ -37,6 +41,17 @@ namespace LaDOSE.Entity.Context
.WithMany(p => p.Seasons) .WithMany(p => p.Seasons)
.HasForeignKey(pt => pt.GameId); .HasForeignKey(pt => pt.GameId);
modelBuilder.Entity<EventGame>()
.HasOne(pt => pt.Event)
.WithMany(p => p.Games)
.HasForeignKey(pt => pt.EventId);
modelBuilder.Entity<EventGame>()
.HasOne(pt => pt.Game)
.WithMany(p => p.Events)
.HasForeignKey(pt => pt.GameId);
} }
} }

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
namespace LaDOSE.Entity namespace LaDOSE.Entity
@@ -13,5 +14,9 @@ namespace LaDOSE.Entity
public Season Season { get; set; } public Season Season { get; set; }
public bool Ranking { get; set; } public bool Ranking { get; set; }
public virtual IEnumerable<EventGame> Games { get; set; }
} }
} }

View File

@@ -0,0 +1,12 @@
namespace LaDOSE.Entity
{
public class EventGame
{
public int EventId { get; set; }
public Event Event { get; set; }
public int GameId { get; set; }
public Game Game { get; set; }
}
}

View File

@@ -12,5 +12,7 @@ namespace LaDOSE.Entity
public string ImgUrl { get; set; } public string ImgUrl { get; set; }
public virtual IEnumerable<SeasonGame> Seasons { get; set; } public virtual IEnumerable<SeasonGame> Seasons { get; set; }
public virtual IEnumerable<EventGame> Events { get; set; }
} }
} }

View File

@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;
namespace LaDOSE.Business.Interface
{
public interface IChallongeProvider
{
Task<Boolean> GetLastTournament();
string GetLastTournamentMessage();
Task<Tuple<int, string>> CreateTournament(string name, string url);
}
}

View File

@@ -4,6 +4,6 @@ namespace LaDOSE.Business.Interface
{ {
public interface IEventService : IBaseService<Event> public interface IEventService : IBaseService<Event>
{ {
bool CreateChallonge(int dto);
} }
} }

View File

@@ -10,4 +10,10 @@
<ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" /> <ProjectReference Include="..\LaDOSE.Entity\LaDOSE.Entity.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="ChallongeCSharpDriver">
<HintPath>..\..\Library\ChallongeCSharpDriver.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChallongeCSharpDriver;
using ChallongeCSharpDriver.Caller;
using ChallongeCSharpDriver.Core.Queries;
using ChallongeCSharpDriver.Core.Results;
using LaDOSE.Business.Interface;
namespace LaDOSE.Business.Provider
{
public class ChallongeProvider : IChallongeProvider
{
private ChallongeConfig Config;
public string ApiKey { get; set; }
public ChallongeHTTPClientAPICaller ApiCaller { get; set; }
public string DernierTournois { get; set; }
public ChallongeProvider(string apiKey)
{
this.ApiKey = apiKey;
this.Config = new ChallongeConfig(this.ApiKey);
this.ApiCaller = new ChallongeHTTPClientAPICaller(Config);
DernierTournois = "Aucun tournois.";
}
public async Task<Tuple<int, string>> CreateTournament(string name, string url)
{
var p = await new CreateTournamentQuery(name, TournamentType.Double_Elimination, url).call(ApiCaller);
return new Tuple<int, string>(p.id, p.url);
}
public async Task<Boolean> GetLastTournament()
{
try
{
List<TournamentResult> tournamentResultList = await new TournamentsQuery()
{
state = TournamentState.Ended
}
.call(this.ApiCaller);
var lastDate = tournamentResultList.Max(e => e.completed_at);
if (lastDate.HasValue)
{
var lastRankingDate = new DateTime(lastDate.Value.Year, lastDate.Value.Month, lastDate.Value.Day);
var lastTournament = tournamentResultList.Where(e => e.completed_at > lastRankingDate).ToList();
string returnValue = "Les derniers tournois : \n";
foreach (var tournamentResult in lastTournament)
{
returnValue += $"{tournamentResult.name} : <https://challonge.com/{tournamentResult.url}> \n";
}
DernierTournois = returnValue;
}
return true;
}
catch
{
return false;
}
}
public string GetLastTournamentMessage()
{
return DernierTournois;
}
}
}

View File

@@ -1,14 +1,24 @@
using System; using System;
using System.Linq;
using LaDOSE.Business.Interface; using LaDOSE.Business.Interface;
using LaDOSE.Entity; using LaDOSE.Entity;
using LaDOSE.Entity.Context; using LaDOSE.Entity.Context;
using Microsoft.EntityFrameworkCore;
namespace LaDOSE.Business.Service namespace LaDOSE.Business.Service
{ {
public class EventService : BaseService<Event>, IEventService public class EventService : BaseService<Event>, IEventService
{ {
public EventService(LaDOSEDbContext context) : base(context) private IChallongeProvider _challongeProvider;
public EventService(LaDOSEDbContext context,IChallongeProvider challongeProvider) : base(context)
{ {
this._challongeProvider = challongeProvider;
}
public override Event GetById(int id)
{
return _context.Event.Include(e=>e.Season).Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == id);
} }
public override Event Create(Event e) public override Event Create(Event e)
@@ -22,5 +32,25 @@ namespace LaDOSE.Business.Service
_context.SaveChanges(); _context.SaveChanges();
return eventAdded.Entity; return eventAdded.Entity;
} }
public bool CreateChallonge(int dto)
{
var currentEvent = _context.Event.Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == dto);
if (currentEvent != null)
{
var games = currentEvent.Games.Select(e => e.Game);
var s = currentEvent.Date.ToString("MM/dd/yy");
foreach (var game in games)
{
var url = $"TestDev{game.Id}{game.Name}";
var name = $"[{s}]Ranking {currentEvent.Name}{game.Name}";
_challongeProvider.CreateTournament(name,url);
}
return true;
}
return false;
}
} }
} }

View File

@@ -17,7 +17,7 @@ namespace LaDOSE.Business.Service
public override IEnumerable<Game> GetAll() public override IEnumerable<Game> GetAll()
{ {
return _context.Game.Include(e => e.Seasons).ToList(); return _context.Game.Include(e => e.Seasons).ThenInclude(e=>e.Season).ToList();
} }

Binary file not shown.