Skip to content

Commit

Permalink
Merge pull request WCell#59 from Shyax/fluenthibernate
Browse files Browse the repository at this point in the history
First attempt to convert PetitionRecord to fluent, will work however includes some changes not required -- will refactor later
  • Loading branch information
jaddie committed Apr 17, 2014
2 parents 35b0c4e + 9131d01 commit 71be487
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
5 changes: 5 additions & 0 deletions Core/WCell.Database/DatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public T FindFirst<T>(DetachedCriteria criteria)
return results.Count > 0 ? results[0] : default(T);
}

public T FindFirst<T>(Expression<Func<T, bool>> expression)
{
return FindFirst<T>(DetachedCriteria.For<T>().Add(Restrictions.Where(expression)));
}

/// <summary>
/// Returns the total number of entities that match the given criteria
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions Services/WCell.RealmServer/Database/Mappings/PetitionRecordMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using FluentNHibernate.Mapping;
using WCell.RealmServer.Items;

namespace WCell.RealmServer.Database.Mappings
{
internal class PetitionRecordMap : ClassMap<PetitionRecord>
{
public PetitionRecordMap()
{
Id(x => x.OwnerId);
Map(x => x.Type).Not.Nullable();
Map(x => x.ItemId).Not.Nullable();
Map(x => x.Name).Not.Nullable().Unique();
HasMany(x => x.SignedIds).Element("SignedIds");
}
}
}
63 changes: 20 additions & 43 deletions Services/WCell.RealmServer/Items/PetitionCharter.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using WCell.Constants;
using WCell.RealmServer.Database;
using WCell.RealmServer.Entities;
using WCell.Core.Database;

namespace WCell.RealmServer.Items
{
public class PetitionCharter : Item
{
private PetitionRecord m_Petition;
private PetitionRecord _Petition;

public PetitionCharter()
{
}

protected override void OnLoad()
{
m_Petition = PetitionRecord.Find((int)Owner.EntityId.Low);
_Petition = RealmWorldDBMgr.DatabaseProvider.FindOne<PetitionRecord>(x => x.OwnerId == Owner.EntityId.Low);
}

protected internal override void DoDestroy()
{
Petition.Delete();
RealmWorldDBMgr.DatabaseProvider.Delete<PetitionRecord>(x => x.OwnerId == Owner.EntityId.Low);
base.DoDestroy();
}

public PetitionRecord Petition
{
get { return m_Petition; }
set { m_Petition = value; }
get { return _Petition; }
set { _Petition = value; }
}
}

[ActiveRecord("PetitionRecord", Access = PropertyAccess.Property)]
public class PetitionRecord : WCellRecord<PetitionRecord>
public class PetitionRecord
{

[Field("Type", NotNull = true)]
private int m_Type;

[PrimaryKey(PrimaryKeyType.Assigned, "OwnerId")]
private int m_OwnerId
{
get;
set;
}

public PetitionRecord()
{
}
Expand All @@ -62,34 +46,31 @@ public PetitionRecord(string name, uint ownerId, uint itemId, PetitionType type)
Type = type;
}

[Property("ItemId", NotNull = true)]
private int ItemId
public int ItemId
{
get;
set;
}

[Property("Name", NotNull = true, Unique = true)]
public string Name
{
get;
set;
}

public uint OwnerId
{
get { return (uint)m_OwnerId; }
set { m_OwnerId = (int)value; }
}
public uint OwnerId
{
get;
set;
}

public PetitionType Type
{
get { return (PetitionType)m_Type; }
set { m_Type = (int)value; }
get;
set;
}

[Property("SignedIds", NotNull = true)]
public List<uint> SignedIds
public IList<uint> SignedIds
{
get;
set;
Expand All @@ -98,26 +79,22 @@ public List<uint> SignedIds
public void AddSignature(uint signedId)
{
SignedIds.Add(signedId);
Update();
RealmWorldDBMgr.DatabaseProvider.Update<PetitionRecord>(this);
}

public static PetitionRecord LoadRecord(int ownerId)
{
return Find(ownerId);
return RealmWorldDBMgr.DatabaseProvider.FindOne<PetitionRecord>(x => x.OwnerId == ownerId);
}

public static bool CanBuyPetition(uint ownerId)
{
if(Exists((int)ownerId))
return false;
else
return true;
return !RealmWorldDBMgr.DatabaseProvider.Exists<PetitionRecord>(x => x.OwnerId == ownerId);
}

public static PetitionRecord LoadRecordByItemId(uint itemId)
{
var property = FindAllByProperty("ItemId", (int)itemId);
return property[0];
return RealmWorldDBMgr.DatabaseProvider.FindFirst<PetitionRecord>(x => x.ItemId == itemId);
}
}
}

0 comments on commit 71be487

Please sign in to comment.