Created
March 23, 2011 00:53
-
-
Save SuperYeti/882424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class DBBase | |
{ | |
public DateTime LastUpdateDate { get; set; } | |
public DateTime CreationDate { get; set; } | |
public abstract bool Active { get; set; } | |
public virtual bool Update() | |
{ | |
try | |
{ | |
this.LastUpdateDate = DateTime.Now; | |
var ret = DAL.Connection.Update(this); | |
if(ret <= 0) | |
return false; | |
return true; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public virtual bool Delete() | |
{ | |
try | |
{ | |
this.Active = false; | |
this.LastUpdateDate = DateTime.Now; | |
var ret = DAL.Connection.Update(this); | |
if(ret <= 0) | |
return false; | |
return true; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
} | |
public class ItemType : DBBase | |
{ | |
[Section("Item Type Information")] | |
[Skip] | |
[PrimaryKey, AutoIncrement] | |
public int ItemTypeId { get; set; } | |
[Entry("Name")] | |
public string Name { get; set; } | |
[Entry("Description")] | |
public string Description { get; set; } | |
[Entry("Install Cost")] | |
public decimal InstallCost { get; set; } | |
[Entry("Material Cost")] | |
public decimal MaterialCost { get; set; } | |
[Entry("Default Quantity")] | |
public decimal DefaultQuantity { get; set; } | |
public override bool Active { get; set; } | |
[Section] | |
[Alignment(UITextAlignment.Center)] | |
[OnTap("SaveItemType")] | |
[Ignore] | |
public string Save; | |
#region Constructors | |
public ItemType() | |
{ | |
} | |
#endregion | |
#region Static Methods | |
public static ItemType SaveItemType(ItemType itemType) | |
{ | |
if(itemType == null) | |
throw new ArgumentNullException("itemType", "itemType is null. This is not allowed."); | |
if(itemType.ItemTypeId <= 0) | |
{ | |
itemType.CreationDate = DateTime.Now; | |
itemType.Active = true; | |
itemType.LastUpdateDate = DateTime.Now; | |
if(InsertItemType(itemType)) | |
return itemType; | |
else | |
return null; | |
} | |
else | |
{ | |
var cust = GetItemType(itemType.ItemTypeId); | |
if(cust == null) | |
{ | |
itemType.CreationDate = DateTime.Now; | |
itemType.Active = true; | |
itemType.LastUpdateDate = DateTime.Now; | |
if(InsertItemType(itemType)) | |
return itemType; | |
else | |
return null; | |
} | |
else | |
{ | |
itemType.LastUpdateDate = DateTime.Now; | |
if(itemType.Update()) | |
return itemType; | |
else | |
return null; | |
} | |
} | |
} | |
public static ItemType DeleteItemType(ItemType itemType) | |
{ | |
if(itemType == null) | |
throw new ArgumentNullException("itemType", "itemType is null. This is not allowed."); | |
if(itemType.ItemTypeId <= 0) | |
{ | |
return null; | |
} | |
else | |
{ | |
var cust = GetItemType(itemType.ItemTypeId); | |
if(cust == null) | |
return null; | |
else | |
{ | |
if(itemType.Delete()) | |
return itemType; | |
else | |
return null; | |
} | |
} | |
} | |
private static bool InsertItemType(ItemType itemType) | |
{ | |
try | |
{ | |
itemType.CreationDate = DateTime.Now; | |
itemType.Active = true; | |
itemType.LastUpdateDate = DateTime.Now; | |
var ret = DAL.Connection.Insert(itemType); | |
if(ret <= 0 || itemType.ItemTypeId <= 0) | |
return false; | |
return true; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public static List<ItemType> GetItemTypes (bool ShowAll) | |
{ | |
try | |
{ | |
return DAL.Connection.Query<ItemType>("select * from ItemType where " + | |
"((? = 1) or (Active = 1 and ? = 0)) ", | |
new object[]{ ShowAll, ShowAll }); | |
} | |
catch (Exception) | |
{ | |
return new List<ItemType>(); | |
} | |
} | |
public static ItemType GetItemType (int ItemTypeID) | |
{ | |
try | |
{ | |
return DAL.Connection.Query<ItemType>("select * from ItemType where ItemTypeID = ?", ItemTypeID)[0]; | |
} | |
catch (Exception) | |
{ | |
return null; | |
} | |
} | |
public static ItemType GetItemType (string ItemTypeName) | |
{ | |
try | |
{ | |
return DAL.Connection.Query<ItemType>("select * from ItemType where Name = ?", ItemTypeName)[0]; | |
} | |
catch (Exception) | |
{ | |
return null; | |
} | |
} | |
public static List<ItemType> GetItemTypes (string Name, bool ShowAll) | |
{ | |
try | |
{ | |
return DAL.Connection.Query<ItemType>("select * from ItemType where " + | |
"((? = 1) or (Active = 1 and ? = 0)) and " + | |
"(FirstName + ' ' + LastName like '%?%') ", | |
new object[]{ ShowAll, ShowAll, Name }); | |
} | |
catch (Exception) | |
{ | |
return new List<ItemType>(); | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment