108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using Iesi.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using Portoa.Persistence;
|
|
|
|
namespace VideoGameQuotes.Api {
|
|
public class Game : Entity<int>, IDtoMappable<GameDto> {
|
|
private readonly Iesi.Collections.Generic.ISet<GamingSystem> systems = new HashedSet<GamingSystem>();
|
|
private readonly Iesi.Collections.Generic.ISet<Publisher> publishers = new HashedSet<Publisher>();
|
|
|
|
public Game() {
|
|
Created = DateTime.UtcNow;
|
|
}
|
|
|
|
[CanBeNull]
|
|
public virtual string Website { get; set; }
|
|
public virtual User Creator { get; set; }
|
|
public virtual DateTime Created { get; set; }
|
|
public virtual IEnumerable<GamingSystem> Systems { get { return systems; } }
|
|
[Required]
|
|
public virtual string Name { get; set; }
|
|
public virtual IEnumerable<Publisher> Publishers { get { return publishers; } }
|
|
[CanBeNull, StringLength(1024)]
|
|
public virtual byte[] Icon { get; set; }
|
|
public virtual Region Region { get; set; }
|
|
|
|
#region adding and removing stuff
|
|
public virtual Game AddSystem(GamingSystem system) {
|
|
systems.Add(system);
|
|
return this;
|
|
}
|
|
|
|
public virtual void ClearSystems() {
|
|
systems.Clear();
|
|
}
|
|
|
|
public virtual Game AddPublisher(Publisher publisher) {
|
|
publishers.Add(publisher);
|
|
return this;
|
|
}
|
|
|
|
public virtual void ClearPublishers() {
|
|
publishers.Clear();
|
|
}
|
|
#endregion
|
|
|
|
public virtual GameDto ToDto() {
|
|
return new GameDto {
|
|
Id = Id,
|
|
Website = Website,
|
|
Created = Created,
|
|
Systems = Systems.Select(system => system.ToDto()),
|
|
Name = Name,
|
|
Regions = Region.ToString().Split(',').Select(region => region.Trim()),
|
|
Publishers = Publishers.Select(publisher => publisher.ToDto()),
|
|
Icon = Icon != null ? Convert.ToBase64String(Icon) : null
|
|
};
|
|
}
|
|
|
|
#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; }
|
|
[CanBeNull]
|
|
public string Website { get; set; }
|
|
[CanBeNull]
|
|
public string Icon { get; set; }
|
|
public DateTime Created { get; set; }
|
|
public IEnumerable<SystemDto> Systems { get; set; }
|
|
public string Name { get; set; }
|
|
public IEnumerable<PublisherDto> Publishers { get; set; }
|
|
public IEnumerable<string> Regions { get; set; }
|
|
}
|
|
|
|
public static class GameExtensions {
|
|
public static string GetBase64EncodedIcon(this Game game) {
|
|
if (game.Icon == null || game.Icon.Length == 0) {
|
|
return null;
|
|
}
|
|
|
|
return Convert.ToBase64String(game.Icon);
|
|
}
|
|
}
|
|
} |