Test Creation DesktopApp

This commit is contained in:
2019-03-09 14:03:25 +01:00
parent e8fd116eab
commit ac9614a70a
34 changed files with 1131 additions and 189 deletions

View File

@@ -0,0 +1,8 @@
namespace LaDOSE.Business.Helper
{
public class Reservation
{
public string Name { get; set; }
public bool Valid { get; set; }
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace LaDOSE.Business.Helper
{
public static class WpEventDeserialize
{
public static readonly List<String> EventManagerField =new List<string>(new []{"HR3", "HR2", "COMMENT", "BOOKING_COMMENT"});
public static List<Reservation> Parse(string meta)
{
if (meta == null) return new List<Reservation>();
PhpSerializer p = new PhpSerializer();
var b = p.Deserialize(meta);
Hashtable Wpbook = b as Hashtable;
var games = new List<Reservation>();
if (Wpbook != null)
{
Hashtable reg2 = Wpbook["booking"] as Hashtable;
foreach (string reg2Key in reg2.Keys)
{
if (!EventManagerField.Contains(reg2Key.ToUpperInvariant()))
games.Add(new Reservation()
{
Name = reg2Key,
Valid = (string)reg2[reg2Key] == "1"
});
}
return games;
}
return null;
}
}
}

View File

@@ -9,5 +9,7 @@ namespace LaDOSE.Business.Interface
T Create(T entity);
bool Update(T entity);
bool Delete(int id);
T AddOrUpdate(T entity);
}
}

View File

@@ -6,7 +6,7 @@ namespace LaDOSE.Business.Interface
{
public interface IEventService : IBaseService<Event>
{
bool CreateChallonge(int eventId, int wpEventId);
List<WPUser> GetBooking(int eventId, int wpEventId, Game game);
}
}

View File

@@ -1,7 +0,0 @@
namespace LaDOSE.Business.Interface
{
public interface IUtilService
{
bool UpdateBooking();
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using LaDOSE.Entity;
using LaDOSE.Entity.Wordpress;
namespace LaDOSE.Business.Interface
@@ -6,5 +7,9 @@ namespace LaDOSE.Business.Interface
public interface IWordPressService
{
List<WPEvent> GetWpEvent();
List<WPUser> GetBooking(int wpEventId, Game game);
List<WPUser> GetBookingOptions(int wpEventId, Game game);
bool UpdateBooking();
bool CreateChallonge(int gameId, int wpEventId);
}
}

View File

@@ -3,10 +3,11 @@ using System.Linq;
using LaDOSE.Business.Interface;
using LaDOSE.Entity.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace LaDOSE.Business.Service
{
public class BaseService<T> : IBaseService<T> where T : class
public class BaseService<T> : IBaseService<T> where T : Entity.Context.Entity
{
protected LaDOSEDbContext _context;
@@ -43,5 +44,21 @@ namespace LaDOSE.Business.Service
_context.Remove(find);
return _context.Entry(find).State == EntityState.Deleted;
}
public virtual T AddOrUpdate(T entity)
{
EntityEntry<T> entityEntry;
if (entity.Id == 0)
{
entityEntry = this._context.Add(entity);
}
else
{
entityEntry = this._context.Update(entity);
}
this._context.SaveChanges();
return entityEntry.Entity;
}
}
}

View File

@@ -15,14 +15,15 @@ namespace LaDOSE.Business.Service
{
private IChallongeProvider _challongeProvider;
public EventService(LaDOSEDbContext context,IChallongeProvider challongeProvider) : base(context)
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);
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)
@@ -37,71 +38,5 @@ namespace LaDOSE.Business.Service
return eventAdded.Entity;
}
public List<WPUser> GetBooking(int eventId, int wpEventId,Game game)
{
var currentEvent = _context.Event.Include(e => e.Games).ThenInclude(e => e.Game).FirstOrDefault(e => e.Id == eventId);
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList();
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList();
List<WPUser> users = new List<WPUser>();
foreach (var booking in bookings)
{
PhpSerializer p = new PhpSerializer();
var b = p.Deserialize(booking.Meta);
Hashtable Wpbook = b as Hashtable;
Hashtable reg = Wpbook["registration"] as Hashtable;
Hashtable reg2 = Wpbook["booking"] as Hashtable;
if (reg2.ContainsKey(game.Name) && ((string)reg2[game.Name]) == "1")
{
booking.WPUser.WPBookings = null;
users.Add(booking.WPUser);
}
}
return users;
}
public bool CreateChallonge(int eventId,int wpEventId)
{
var currentEvent = _context.Event.Include(e=>e.Games).ThenInclude(e=>e.Game).FirstOrDefault(e=>e.Id == eventId);
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e=>e.Id == wpEventId);
var users = currentWpEvent.SelectMany(e => e.WPBookings.Select(u => u.WPUser));
var userNames = users.Select(e => e.Name).Distinct().ToList();
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}";
var tournament = _challongeProvider.CreateTournament(name,url).Result;
var eventGame = currentEvent.Games.FirstOrDefault(e => e.GameId == game.Id);
eventGame.ChallongeId = tournament.id;
eventGame.ChallongeUrl = tournament.url;
foreach (var userName in userNames)
{
try
{
_challongeProvider.AddPlayer(tournament.id, userName);
}
catch
{
Console.WriteLine($"Erreur d ajout sur {userName}" );
continue;
}
}
_context.Entry(eventGame).State = EntityState.Modified;
}
_context.SaveChanges();
return true;
}
return false;
}
}
}

View File

@@ -1,24 +0,0 @@
using LaDOSE.Business.Interface;
using LaDOSE.Entity.Context;
using Microsoft.EntityFrameworkCore;
namespace LaDOSE.Business.Service
{
public class UtilService : IUtilService
{
private LaDOSEDbContext _context;
public UtilService(LaDOSEDbContext context)
{
_context = context;
}
public bool UpdateBooking()
{
_context.Database.SetCommandTimeout(60);
_context.Database.ExecuteSqlCommand("call ladoseapi.ImportEvent();");
_context.Database.SetCommandTimeout(30);
return true;
}
}
}

View File

@@ -1,22 +1,136 @@
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using LaDOSE.Business.Helper;
using LaDOSE.Business.Interface;
using LaDOSE.Entity;
using LaDOSE.Entity.Context;
using LaDOSE.Entity.Wordpress;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace LaDOSE.Business.Service
{
public class WordPressService : IWordPressService
{
private LaDOSEDbContext _context;
public WordPressService(LaDOSEDbContext context)
private IChallongeProvider _challongeProvider;
public WordPressService(LaDOSEDbContext context, IChallongeProvider challongeProvider)
{
this._context = context;
this._challongeProvider = challongeProvider;
}
public List<WPEvent> GetWpEvent()
{
return _context.Set<WPEvent>().ToList();
var wpEvents = _context.Set<WPEvent>().OrderByDescending(e => e.Id).Include(e => e.WPBookings)
.ThenInclude(e => e.WPUser).Where(e => e.WPBookings.Count() != 0).Take(10).ToList();
return wpEvents;
}
public bool UpdateBooking()
{
_context.Database.SetCommandTimeout(60);
_context.Database.ExecuteSqlCommand("call ladoseapi.ImportEvent();");
_context.Database.SetCommandTimeout(30);
return true;
}
public List<WPUser> GetBooking(int wpEventId, Game game)
{
var selectedGameWpId = game.WordPressTag.Split(';');
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList();
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList();
List<WPUser> users = new List<WPUser>();
foreach (var booking in bookings)
{
var reservations = WpEventDeserialize.Parse(booking.Meta);
if (reservations != null)
{
var gamesReservation = reservations.Where(e => e.Valid).Select(e => e.Name);
if (selectedGameWpId.Any(e => gamesReservation.Contains(e)))
{
users.Add(booking.WPUser);
}
}
}
return users;
}
public List<WPUser> GetBookingOptions(int wpEventId, Game game)
{
var selectedGameWpId = game.WordPressTagOs.Split(';');
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser).Where(e => e.Id == wpEventId).ToList();
List<WPBooking> bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList();
List<WPUser> users = new List<WPUser>();
foreach (var booking in bookings)
{
var reservations = WpEventDeserialize.Parse(booking.Meta);
if (reservations != null)
{
var gamesReservation = reservations.Where(e => e.Valid).Select(e => e.Name);
if (selectedGameWpId.Any(e => gamesReservation.Contains(e)))
{
users.Add(booking.WPUser);
}
}
}
return users;
}
public bool CreateChallonge(int gameId, int wpEventId)
{
var selectedGame = _context.Game.FirstOrDefault(e => e.Id == gameId);
var selectedGameWpId = selectedGame.WordPressTag.Split(';');
var currentWpEvent = _context.WPEvent.Include(e => e.WPBookings).ThenInclude(e => e.WPUser)
.Where(e => e.Id == wpEventId);
var users = currentWpEvent.SelectMany(e => e.WPBookings.Select(u => u.WPUser));
if (selectedGame != null)
{
var currentEvent = currentWpEvent.FirstOrDefault();
var eventDate = currentEvent.Date?.ToString("MM/dd/yy");
var remove = currentEvent.Date?.ToString("Mdyy");
var url = $"{remove}{selectedGame.Id}";
var name = $"[{eventDate}]Ranking {currentEvent.Name} {selectedGame.Name}";
var tournament = _challongeProvider.CreateTournament(name, url).Result;
foreach (var booking in currentEvent.WPBookings)
{
var reservations = WpEventDeserialize.Parse(booking.Meta);
if (reservations != null)
{
var gamesReservation = reservations.Where(e=>e.Valid).Select(e=>e.Name);
if(selectedGameWpId.Any(e => gamesReservation.Contains(e)))
{
try
{
_challongeProvider.AddPlayer(tournament.id, booking.WPUser.Name);
}
catch
{
Console.WriteLine($"Erreur d ajout sur {booking.WPUser.Name}");
continue;
}
}
}
}
return true;
}
return false;
}
}
}