Mineplex2018-withcommit/Website/LOC.Website.Common/Models/PetAdministrator.cs

176 lines
5.6 KiB
C#
Raw Normal View History

2013-08-27 17:14:08 +02:00
namespace LOC.Website.Common.Models
{
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Core.Model.Account;
using Core.Model.Server;
using Core.Tokens.Client;
using Data;
using System;
using System.Data.SqlClient;
2013-08-27 17:14:08 +02:00
public class PetAdministrator : IPetAdministrator
{
private readonly INautilusRepositoryFactory _repositoryFactory;
private readonly ILogger _logger;
2013-08-27 17:14:08 +02:00
public PetAdministrator(INautilusRepositoryFactory repositoryFactory, ILogger logger)
2013-08-27 17:14:08 +02:00
{
_repositoryFactory = repositoryFactory;
_logger = logger;
2013-08-27 17:14:08 +02:00
}
public List<Pet> GetPets(List<Pet> petTokens)
{
using (var repository = _repositoryFactory.CreateRepository())
{
bool added = false;
2013-08-27 17:14:08 +02:00
foreach (var item in petTokens.Where(item => !repository.Any<Pet>(x => x.PetType == item.PetType)))
{
repository.Add(item);
added = true;
2013-08-27 17:14:08 +02:00
}
if (added)
repository.CommitChanges();
2013-08-27 17:14:08 +02:00
return repository.GetAll<Pet>().Include(x => x.SalesPackage).ToList();
}
}
public List<PetExtra> GetPetExtras(List<PetExtra> petExtraTokens)
{
using (var repository = _repositoryFactory.CreateRepository())
{
bool added = false;
2013-08-27 17:14:08 +02:00
foreach (var item in petExtraTokens.Where(item => !repository.Any<PetExtra>(x => x.Name == item.Name && x.Material == item.Material)))
{
repository.Add(item);
added = true;
2013-08-27 17:14:08 +02:00
}
if (added)
repository.CommitChanges();
2013-08-27 17:14:08 +02:00
return repository.GetAll<PetExtra>().Include(x => x.SalesPackage).ToList();
}
}
public void AddPet(PetChangeToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).Include(x => x.Pets).FirstOrDefault();
if (account == null)
return;
account.Pets.Add(new OwnedPet { PetType = token.PetType, PetName = token.PetName });
repository.CommitChanges();
}
}
public void UpdatePet(PetChangeToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).Include(x => x.Pets).FirstOrDefault();
if (account == null)
return;
var pet = account.Pets.FirstOrDefault(x => x.PetType.Equals(token.PetType));
2013-08-27 17:14:08 +02:00
if (pet == null)
return;
account.Pets.RemoveAll(x => x.PetType.Equals(token.PetType) && x.OwnedPetId != pet.OwnedPetId);
2013-08-27 17:14:08 +02:00
pet.PetName = token.PetName;
repository.Edit(pet);
repository.Edit(account);
2013-08-27 17:14:08 +02:00
repository.CommitChanges();
}
}
public void RemovePet(PetChangeToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).Include(x => x.Pets).FirstOrDefault();
if (account == null)
return;
var ownedPet = account.Pets.FirstOrDefault(x => x.PetType == token.PetType);
if (ownedPet == null)
return;
account.Pets.Remove(ownedPet);
repository.CommitChanges();
}
}
public void AddPetNameTag(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == name).FirstOrDefault();
if (account == null)
return;
account.PetNameTagCount++;
repository.CommitChanges();
}
}
public void RemovePetNameTag(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == name).FirstOrDefault();
if (account == null)
return;
account.PetNameTagCount--;
if (account.PetNameTagCount < 0)
account.PetNameTagCount = 0;
repository.CommitChanges();
}
}
#region IPetAdministrator Members
public List<AccountPetToken> GetBulkPets(int start, int count)
{
using (var repository = _repositoryFactory.CreateRepository())
{
try
{
return repository.Context.Database.SqlQuery<AccountPetToken>(@"SELECT OwnedPetId AS PetId, Uuid, PetType, PetName FROM (SELECT OwnedPetId, A.Uuid, PetType, PetName, ROW_NUMBER() OVER (ORDER BY OwnedPetId) as row FROM Nautilus.dbo.OwnedPets INNER JOIN Nautilus.dbo.Accounts AS A ON A.AccountId = Account_AccountId) a WHERE row > " + start + @" AND row <= " + count + @";", string.Empty).ToList();
}
catch (Exception ex)
{
_logger.Log("ERROR", ex.Message + " : " + ex.InnerException);
}
return new List<AccountPetToken>();
}
}
#endregion
2013-08-27 17:14:08 +02:00
}
}