diff --git a/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs b/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs index 1c2686f..f32a9fb 100644 --- a/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs +++ b/LaDOSE.Src/LaDOSE.Api/Controllers/EventController.cs @@ -4,20 +4,33 @@ using System.Linq; using System.Threading.Tasks; using LaDOSE.Business.Interface; using LaDOSE.Entity; +using LaDOSE.Entity.Wordpress; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace LaDOSE.Api.Controllers { + [Authorize] [Produces("application/json")] [Route("api/[controller]")] public class EventController : GenericController { - public EventController(IEventService service) : base(service) + private IGameService _gameService; + + public EventController(IEventService service,IGameService gameService) : base(service) { + this._gameService = gameService; } - - + + [HttpGet("GetUsers/{eventId}/{wpEventId}/{gameId}")] + public List GetUsers(int eventId, int wpEventId,int gameId) + { + var game = _gameService.GetById(gameId); + return _service.GetBooking(eventId, wpEventId,game); + + } + [HttpGet("Generate/{eventId}/{wpEventId}")] public bool GenerateChallonge(int eventId, int wpEventId) { @@ -25,4 +38,26 @@ namespace LaDOSE.Api.Controllers } } + + [Authorize] + [Produces("application/json")] + [Route("api/[controller]")] + public class UtilController : Controller + { + private IUtilService _service; + + public UtilController(IUtilService service) + { + _service = service; + } + + + + [HttpGet("UpdateBooking")] + public bool UpdateBooking() + { + return _service.UpdateBooking(); + + } + } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Api/Startup.cs b/LaDOSE.Src/LaDOSE.Api/Startup.cs index 3fbefeb..6758974 100644 --- a/LaDOSE.Src/LaDOSE.Api/Startup.cs +++ b/LaDOSE.Src/LaDOSE.Api/Startup.cs @@ -42,6 +42,7 @@ namespace LaDOSE.Api var MySqlUser = this.Configuration["MySql:User"]; var MySqlPassword = this.Configuration["MySql:Password"]; + services.AddCors(); services.AddMvc().AddJsonOptions(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.AddDbContextPool( // replace "YourDbContext" with the class name of your DbContext @@ -98,6 +99,7 @@ namespace LaDOSE.Api services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddTransient(p => new ChallongeProvider(this.Configuration["ApiKey:ChallongeApiKey"])); } diff --git a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs index 43aef00..8c3e385 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Context/LaDOSEDbContext.cs @@ -37,6 +37,7 @@ namespace LaDOSE.Entity.Context .WithMany(p => p.Event) .HasForeignKey(fk => fk.SeasonId); + #region SeasonGame modelBuilder.Entity() diff --git a/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPEvent.cs b/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPEvent.cs index c1b96f0..49aa138 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPEvent.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPEvent.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace LaDOSE.Entity.Wordpress @@ -10,7 +11,7 @@ namespace LaDOSE.Entity.Wordpress public int Id { get; set; } public string Name { get; set; } public string Slug { get; set; } - public string Date { get; set; } + public DateTime Date { get; set; } public virtual IEnumerable WPBookings { get; set; } } diff --git a/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPUser.cs b/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPUser.cs index 151b45c..7a2bf5e 100644 --- a/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPUser.cs +++ b/LaDOSE.Src/LaDOSE.Entity/Wordpress/WPUser.cs @@ -8,7 +8,7 @@ namespace LaDOSE.Entity.Wordpress [Key] public int Id { get; set; } public string Name { get; set; } - public string WPUserId { get; set; } + public string WPUserLogin { get; set; } public string WPMail { get; set; } public virtual IEnumerable WPBookings { get; set; } } diff --git a/LaDOSE.Src/LaDOSE.Service/Helper/PhpSerialize.cs b/LaDOSE.Src/LaDOSE.Service/Helper/PhpSerialize.cs new file mode 100644 index 0000000..40caca6 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Helper/PhpSerialize.cs @@ -0,0 +1,214 @@ +using System.Collections; +using System.Collections.Generic; +using System.Globalization; +using System.Text; + +namespace LaDOSE.Business.Helper +{ + /// + /// PhpSerializer Class. + /// + public class PhpSerializer + { + private readonly NumberFormatInfo nfi; + + private int pos; //for unserialize + + private Dictionary seenArrayLists; //for serialize (to infinte prevent loops) lol + //types: + // N = null + // s = string + // i = int + // d = double + // a = array (hashtable) + + private Dictionary seenHashtables; //for serialize (to infinte prevent loops) + //http://www.w3.org/TR/REC-xml/#sec-line-ends + + public Encoding StringEncoding = new UTF8Encoding(); + + public bool + XMLSafe = true; //This member tells the serializer wether or not to strip carriage returns from strings when serializing and adding them back in when deserializing + + public PhpSerializer() + { + nfi = new NumberFormatInfo(); + nfi.NumberGroupSeparator = ""; + nfi.NumberDecimalSeparator = "."; + } + + public string Serialize(object obj) + { + seenArrayLists = new Dictionary(); + seenHashtables = new Dictionary(); + + return Serialize(obj, new StringBuilder()).ToString(); + } //Serialize(object obj) + + private StringBuilder Serialize(object obj, StringBuilder sb) + { + if (obj == null) return sb.Append("N;"); + + if (obj is string) + { + var str = (string) obj; + if (XMLSafe) + { + str = str.Replace("\r\n", "\n"); //replace \r\n with \n + str = str.Replace("\r", "\n"); //replace \r not followed by \n with a single \n Should we do this? + } + + return sb.Append("s:" + StringEncoding.GetByteCount(str) + ":\"" + str + "\";"); + } + + if (obj is bool) return sb.Append("b:" + ((bool) obj ? "1" : "0") + ";"); + + if (obj is int) + { + var i = (int) obj; + return sb.Append("i:" + i.ToString(nfi) + ";"); + } + + if (obj is double) + { + var d = (double) obj; + + return sb.Append("d:" + d.ToString(nfi) + ";"); + } + + if (obj is ArrayList) + { + if (seenArrayLists.ContainsKey((ArrayList) obj)) + return sb.Append("N;"); //cycle detected + seenArrayLists.Add((ArrayList) obj, true); + + var a = (ArrayList) obj; + sb.Append("a:" + a.Count + ":{"); + for (var i = 0; i < a.Count; i++) + { + Serialize(i, sb); + Serialize(a[i], sb); + } + + sb.Append("}"); + return sb; + } + + if (obj is Hashtable) + { + if (seenHashtables.ContainsKey((Hashtable) obj)) + return sb.Append("N;"); //cycle detected + seenHashtables.Add((Hashtable) obj, true); + + var a = (Hashtable) obj; + sb.Append("a:" + a.Count + ":{"); + foreach (DictionaryEntry entry in a) + { + Serialize(entry.Key, sb); + Serialize(entry.Value, sb); + } + + sb.Append("}"); + return sb; + } + + return sb; + } //Serialize(object obj) + + public object Deserialize(string str) + { + pos = 0; + return this.deserialize(str); + } //Deserialize(string str) + + private object deserialize(string str) + { + if (str == null || str.Length <= pos) + return new object(); + + int start, end, length; + string stLen; + switch (str[pos]) + { + case 'N': + pos += 2; + return null; + case 'b': + char chBool; + chBool = str[pos + 2]; + pos += 4; + return chBool == '1'; + case 'i': + string stInt; + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(";", start); + stInt = str.Substring(start, end - start); + pos += 3 + stInt.Length; + return int.Parse(stInt, nfi); + case 'd': + string stDouble; + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(";", start); + stDouble = str.Substring(start, end - start); + pos += 3 + stDouble.Length; + return double.Parse(stDouble, nfi); + case 's': + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(":", start); + stLen = str.Substring(start, end - start); + var bytelen = int.Parse(stLen); + length = bytelen; + //This is the byte length, not the character length - so we migth + //need to shorten it before usage. This also implies bounds checking + if (end + 2 + length >= str.Length) length = str.Length - 2 - end; + var stRet = str.Substring(end + 2, length); + while (StringEncoding.GetByteCount(stRet) > bytelen) + { + length--; + stRet = str.Substring(end + 2, length); + } + + pos += 6 + stLen.Length + length; + if (XMLSafe) stRet = stRet.Replace("\n", "\r\n"); + return stRet; + case 'a': + //if keys are ints 0 through N, returns an ArrayList, else returns Hashtable + start = str.IndexOf(":", pos) + 1; + end = str.IndexOf(":", start); + stLen = str.Substring(start, end - start); + length = int.Parse(stLen); + var htRet = new Hashtable(length); + var alRet = new ArrayList(length); + pos += 4 + stLen.Length; //a:Len:{ + for (var i = 0; i < length; i++) + { + //read key + var key = deserialize(str); + //read value + var val = deserialize(str); + + if (alRet != null) + { + if (key is int && (int) key == alRet.Count) + alRet.Add(val); + else + alRet = null; + } + + htRet[key] = val; + } + + pos++; //skip the } + if (pos < str.Length && str[pos] == ';' + ) //skipping our old extra array semi-colon bug (er... php's weirdness) + pos++; + if (alRet != null) + return alRet; + else + return htRet; + default: + return ""; + } //switch + } //unserialzie(object) + } +} //class PhpSerializer \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs index a78c099..ee6184e 100644 --- a/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IEventService.cs @@ -1,9 +1,12 @@ -using LaDOSE.Entity; +using System.Collections.Generic; +using LaDOSE.Entity; +using LaDOSE.Entity.Wordpress; namespace LaDOSE.Business.Interface { public interface IEventService : IBaseService { bool CreateChallonge(int eventId, int wpEventId); + List GetBooking(int eventId, int wpEventId, Game game); } } \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs b/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs new file mode 100644 index 0000000..36d1058 --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Interface/IUtilService.cs @@ -0,0 +1,7 @@ +namespace LaDOSE.Business.Interface +{ + public interface IUtilService + { + bool UpdateBooking(); + } +} \ No newline at end of file diff --git a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs index 580d1e1..4d2d7f4 100644 --- a/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs +++ b/LaDOSE.Src/LaDOSE.Service/Service/EventService.cs @@ -1,8 +1,12 @@ 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; namespace LaDOSE.Business.Service @@ -33,6 +37,30 @@ namespace LaDOSE.Business.Service return eventAdded.Entity; } + public List 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 bookings = currentWpEvent.SelectMany(e => e.WPBookings).ToList(); + List users = new List(); + 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); diff --git a/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs b/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs new file mode 100644 index 0000000..180a73a --- /dev/null +++ b/LaDOSE.Src/LaDOSE.Service/Service/UtilService.cs @@ -0,0 +1,24 @@ +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; + } + } +} \ No newline at end of file