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 ;
2015-08-16 11:16:19 +02:00
using System ;
using System.Data.SqlClient ;
2013-08-27 17:14:08 +02:00
public class PetAdministrator : IPetAdministrator
{
private readonly INautilusRepositoryFactory _repositoryFactory ;
2015-08-16 11:16:19 +02:00
private readonly ILogger _logger ;
2013-08-27 17:14:08 +02:00
2015-08-16 11:16:19 +02:00
public PetAdministrator ( INautilusRepositoryFactory repositoryFactory , ILogger logger )
2013-08-27 17:14:08 +02:00
{
_repositoryFactory = repositoryFactory ;
2015-08-16 11:16:19 +02:00
_logger = logger ;
2013-08-27 17:14:08 +02:00
}
public List < Pet > GetPets ( List < Pet > petTokens )
{
using ( var repository = _repositoryFactory . CreateRepository ( ) )
{
2014-09-19 09:12:37 +02:00
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 ) ;
2014-09-19 09:12:37 +02:00
added = true ;
2013-08-27 17:14:08 +02:00
}
2014-09-19 09:12:37 +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 ( ) )
{
2014-09-19 09:12:37 +02:00
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 ) ;
2014-09-19 09:12:37 +02:00
added = true ;
2013-08-27 17:14:08 +02:00
}
2014-09-19 09:12:37 +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 ) ) ;
2014-09-19 09:12:37 +02:00
2013-08-27 17:14:08 +02:00
if ( pet = = null )
return ;
2014-09-19 09:12:37 +02:00
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 ) ;
2013-11-21 21:08:58 +01:00
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 ( ) ;
}
}
2015-08-16 11:16:19 +02:00
#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
}
}