Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Last active December 26, 2015 22:49
Show Gist options
  • Save UberMouse/7225871 to your computer and use it in GitHub Desktop.
Save UberMouse/7225871 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using SteamLibraryIntersecter.Models;
using SteamLibraryIntersecter.Steam.Entities;
namespace SteamLibraryIntersecter.DAL
{
public class Dal : IDisposable
{
private MongoServer mongoServer = null;
private bool disposed = false;
private string connectionString = System.Environment.GetEnvironmentVariable("CUSTOMCONNSTR_MONGOLAB_URI");
private string dbName = "MongoLab-ec";
private string collectionName = "Games";
// Creates a Note and inserts it into the collection in MongoDB.
public void InsertGames(params SteamGame[] game)
{
var collection = GetGamesCollection();
try
{
collection.InsertBatch(game, WriteConcern.Acknowledged);
}
catch (MongoCommandException ex)
{
var x = ex;
}
}
public IEnumerable<SteamGame> RetrieveGames(params string[] appIds)
{
var collection = GetGamesCollection();
return collection.FindAs<SteamGame>(Query<SteamGame>.In(x => x.AppId, appIds.Select(x => new BsonString(x))));
}
private MongoCollection<SteamGame> GetGamesCollection()
{
var client = (ConfigurationManager.AppSettings["onAzure"] == "true") ? new MongoClient(connectionString) : new MongoClient();
var database = client.GetServer()[dbName];
return database.GetCollection<SteamGame>(collectionName);
}
# region IDisposable
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
if (mongoServer != null)
{
this.mongoServer.Disconnect();
}
}
}
this.disposed = true;
}
# endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.IdGenerators;
using SteamLibraryIntersecter.Steam.Entities;
namespace SteamLibraryIntersecter.Models
{
public class SteamGame
{
[BsonId(IdGenerator = typeof(CombGuidGenerator))]
public Guid Id { get; set; }
[BsonElement("AppId")]
public string AppId { get; set; }
[BsonElement("Name")]
public string Name { get; set; }
[BsonElement("Coop")]
public bool Coop { get; set; }
[BsonElement("Multiplayer")]
public bool Multiplayer { get; set; }
public static SteamGame FromGame(Game game)
{
return new SteamGame
{
AppId = game.AppId,
Coop = game.Coop,
Multiplayer = game.Multiplayer,
Name = game.Name
};
}
public static Game ToGame(SteamGame game)
{
return new Game
{
AppId = game.AppId,
Coop = game.Coop,
Multiplayer = game.Multiplayer,
Name = game.Name
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment