2011-02-09 00:05:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-02-23 09:19:32 +00:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2011-02-16 02:48:11 +00:00
|
|
|
|
using System.Linq;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
using Iesi.Collections.Generic;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Api {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
public class Game : Entity<Game, int>, IDtoMappable<GameDto> {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
private readonly Iesi.Collections.Generic.ISet<GamingSystem> systems = new HashedSet<GamingSystem>();
|
|
|
|
|
private readonly Iesi.Collections.Generic.ISet<Publisher> publishers = new HashedSet<Publisher>();
|
2011-02-09 00:05:32 +00:00
|
|
|
|
|
2011-02-09 03:41:35 +00:00
|
|
|
|
public Game() {
|
|
|
|
|
Created = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-23 09:19:32 +00:00
|
|
|
|
[CanBeNull]
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public virtual string Website { get; set; }
|
2011-02-09 00:05:32 +00:00
|
|
|
|
public virtual User Creator { get; set; }
|
|
|
|
|
public virtual DateTime Created { get; set; }
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public virtual IEnumerable<GamingSystem> Systems { get { return systems; } }
|
2011-02-23 09:19:32 +00:00
|
|
|
|
[Required]
|
2011-02-09 00:05:32 +00:00
|
|
|
|
public virtual string Name { get; set; }
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public virtual IEnumerable<Publisher> Publishers { get { return publishers; } }
|
2011-02-23 09:19:32 +00:00
|
|
|
|
[CanBeNull, StringLength(1024)]
|
|
|
|
|
public virtual byte[] Icon { get; set; }
|
2011-02-09 00:05:32 +00:00
|
|
|
|
public virtual Region Region { get; set; }
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
#region adding and removing stuff
|
|
|
|
|
public virtual Game AddSystem(GamingSystem system) {
|
2011-02-09 00:05:32 +00:00
|
|
|
|
systems.Add(system);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
return this;
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
public virtual void ClearSystems() {
|
|
|
|
|
systems.Clear();
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
public virtual Game AddPublisher(Publisher publisher) {
|
|
|
|
|
publishers.Add(publisher);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void ClearPublishers() {
|
|
|
|
|
publishers.Clear();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2011-02-16 02:48:11 +00:00
|
|
|
|
|
|
|
|
|
public virtual GameDto ToDto() {
|
|
|
|
|
return new GameDto {
|
|
|
|
|
Id = Id,
|
|
|
|
|
Website = Website,
|
|
|
|
|
Created = Created,
|
|
|
|
|
Systems = Systems.Select(system => system.ToDto()),
|
|
|
|
|
Name = Name,
|
2011-02-23 09:19:32 +00:00
|
|
|
|
Regions = Region.ToString().Split(',').Select(region => region.Trim()),
|
2011-02-16 02:48:11 +00:00
|
|
|
|
Publishers = Publishers.Select(publisher => publisher.ToDto())
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region criterion handlers
|
|
|
|
|
public sealed class SystemCriterionHandler : CriterionHandler<Game> {
|
|
|
|
|
protected override Func<Game, bool> HandleInteger(int value) {
|
|
|
|
|
return game => game.Systems.Any(system => system.Id == value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Func<Game, bool> HandleString(string value) {
|
|
|
|
|
return game => game.Systems.Any(system => system.Name == value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class PublisherCriterionHandler : CriterionHandler<Game> {
|
|
|
|
|
protected override Func<Game, bool> HandleInteger(int value) {
|
|
|
|
|
return game => game.Publishers.Any(publisher => publisher.Id == value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Func<Game, bool> HandleString(string value) {
|
|
|
|
|
return game => game.Publishers.Any(publisher => publisher.Name == value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GameDto {
|
|
|
|
|
public int Id { get; set; }
|
|
|
|
|
public string Website { get; set; }
|
|
|
|
|
public DateTime Created { get; set; }
|
|
|
|
|
public IEnumerable<SystemDto> Systems { get; set; }
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
public IEnumerable<PublisherDto> Publishers { get; set; }
|
2011-02-23 09:19:32 +00:00
|
|
|
|
public IEnumerable<string> Regions { get; set; }
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|
2011-02-24 10:55:08 +00:00
|
|
|
|
|
|
|
|
|
public static class GameExtensions {
|
|
|
|
|
public static string GetBase64EncodedIcon(this Game game) {
|
|
|
|
|
if (game.Icon == null || game.Icon.Length == 0) {
|
|
|
|
|
return "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHBSURBVDjLlVM9aMJQEP6eNEF0sbiUouLgoLRkKXS1IG4dC6Xg2LXQRXATHbqVzq4iQjc3sVscnUSnYIdIB9GC4L/xL333IEFsBj04jpf77nt3l+8x0zRxaMViMbTdbtXVahVer9dYLBY/0+k0mcvltEPsGRzMMIyPQCAQ9ng8IAJd14OdTuedp+4PsS4ngslkctFoNNBsNgWB2+3GaDQKOWEdCTgY2WyW9Xo9QbBcLoUfTSDLsoiMMUFgkRxNwHeAdDpt+nw+8EUKp29O5rhEvnEoigJJktBqteD3+0/rgINNulHTNCjzGR5++1Bvb67x+vLF/dmxg3K5HOZB2+12MncxfzAYxJ25wcXjE5ixZCu9m/wufybfUqnLUqmUtwmomAtKi0ajcrVaxWAwQKFQEHOfK1dQajUwrwdSrw8ZEiKRSC4ej0NV1TwjJXI2IxaLyZwA4/FYFHL12T6fz+3o9XrhcrmQyWTQbreZ6IAnZS5dVCoVEpFYmFVEPpvNxJm+0zmRSIhoj0AJunU4HNogq3C/EwtHuqBfaxNQkhJ8NpGwAPtxs9n8c5ug2+2iXq/bojl0S41URKPuv2Dm9JxPsT8W0mO2IJm2EgAAAABJRU5ErkJggg==";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Convert.ToBase64String(game.Icon);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-09 00:05:32 +00:00
|
|
|
|
}
|