Tuesday, July 29, 2008

LINQ sqlhelper

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using bebc.types;
using bebc.dal;

namespace bebc.bll
{


///
/// Information about catalog
///

public class Catalog
{
juicyDataContext _dbContext;
dimensionsDataContext _dbDContext;

public Catalog()
{
_dbContext = new juicyDataContext();
_dbDContext = new dimensionsDataContext();
}


///
/// Returns a collection of products that are special offers
///

public List getSpecialOffers(int page, int pageSize)
{
List myList = new List();
int startRecord = (page - 1) * pageSize;

try
{
var query = (from product in _dbDContext.JUICY_Filtered_Titles
where product.IsSpecial == 1
select product).Skip(startRecord).Take(pageSize);


foreach (JUICY_Filtered_Title myProduct in query)
{
SpecialOffer myProductOffer = new
SpecialOffer("", myProduct.Title, (decimal)myProduct.ListPrice,
myProduct.Category, (decimal)myProduct.ListPrice, myProduct.ItemId, "");

myList.Add(myProductOffer);
}

}
catch (Exception ex)
{
Static.LogError(ex);
}


return myList;
}

///
/// Returns a collection of products that are classed as new titles
///

public List getNewTitles(int page, int pageSize)
{
List myList = new List();
int startRecord = (page - 1) * pageSize;

try
{
var query = (from product in _dbDContext.JUICY_Filtered_Titles
where product.IsNew == 1
select product).Skip(startRecord).Take(pageSize);


foreach (JUICY_Filtered_Title myProduct in query)
{
SimpleProduct myNewProduct = new
SimpleProduct("", myProduct.Title, myProduct.Category, myProduct.ItemId, "");

myList.Add(myNewProduct);
}

}
catch (Exception ex)
{
Static.LogError(ex);
}


return myList;
}

///
/// Gets the number of new titles in the database for paging
///

/// Count is stored as a string in the information property
public GenericResponse getSpecialOffersCount()
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from product in _dbDContext.JUICY_Filtered_Titles
where product.IsSpecial == 1
select product).Count();

genResponse.information = query.ToString();
genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Gets the number of special offers in the database for paging
///

/// Count is stored as a string in the information property
public GenericResponse getNewTitlesCount()
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from product in _dbDContext.JUICY_Filtered_Titles
where product.IsNew == 1
select product).Count();

genResponse.information = query.ToString();
genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}


///
/// Returns a collection of publishers
///

public List getPublishers()
{
List myList = new List();


try
{
var query = _dbDContext.JUICY_Publisher_Lists.Select(o => new KeyValueItem(o.Title, o.Title));

return query.ToList();

}
catch (Exception ex)
{
Static.LogError(ex);
}



return myList;
}


///
/// Returns a collection of categories
///

public List getCategories()
{
List myList = new List();

try
{
var query = _dbContext.bebcCategories.Select(o => new KeyValueItem(o.id, o.title));
myList = query.ToList();

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}


///
/// Returns the title of a category from its id
///

///
/// Returns the title as the information property
///

public GenericResponse getCategoryTitle(string id)
{
GenericResponse genResponse = new GenericResponse();

try
{
var query = (from item in _dbContext.bebcCategories
where item.id == id
select item.title).Single();

genResponse.information = query;

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.EMTPY;
genResponse.ex = ex_exists;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}


///
/// Returns a collection of learning levels
///

public List getLearningLevels()
{
List myList = new List();

try
{
var query = _dbContext.bebcLearningLevels.Select(o => new KeyValueItem(o.id, o.title));
myList = query.ToList();

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}


///
/// Returns a paged collection of products within the search critera
///

public List searchProducts(int page, int pageSize, string isbn, string title, string author, string category, string publisher, string learningLevel)
{
// add parameters and paging
List myList = new List();


return myList;

}


///
/// Returns the product details for the passed item id
///

public ProductDetail getProductDetails(string itemId)
{
ProductDetail myProduct;

try
{
var query = (_dbDContext.JUICY_Get_Product_Details(itemId)).First();
myProduct = new ProductDetail(query.Item_code, query.ISBN13, query.Stock.ToString(),
query.Category_Level, query.Publisher, query.Title, query.Author,
(decimal)query.List_Price, query.notes, query.ISBN10, (decimal)query.List_Price);

}
catch (InvalidOperationException)
{
myProduct = null;
}
catch (Exception ex)
{
Static.LogError(ex);
myProduct = null;
}

return myProduct;
}

///
/// Returns a collection of products related to the passed item id
///

public List getRelatedProducts(int page, int pageSize, string itemId)
{
List myList = new List();
int startRecord = (page -1) * pageSize;

try
{
var query = _dbDContext.JUICY_Get_Related_Items(itemId).Skip(startRecord).Take(pageSize);

foreach (JUICY_Get_Related_ItemsResult myResult in query)
{
myList.Add(new SimpleProduct(myResult.ISBN13, myResult.Title, myResult.Category_Level, myResult.ISBN10, myResult.Item_code));
}

}
catch (InvalidOperationException)
{
myList = null;
}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// Gets the number of related products to the passed item id for paging
///

/// Count is stored as a string in the information property
public GenericResponse getRelatedProductsCount(string itemId)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = _dbDContext.JUICY_Get_Related_ItemsCount(itemId);

/*if (query.Column1.HasValue)
genResponse.information = query.Column1.Value.ToString();
else
genResponse.information = "0";
*/
genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

}

}

========================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using bebc.dal;
using bebc.types;

namespace bebc.bll
{

///
/// Cart
///

public class JCart
{
juicyDataContext _dbContext;
Guid _cartId;

public JCart()
{
_cartId = Guid.NewGuid();
_dbContext = new juicyDataContext();
}

public JCart(Guid cartId)
{
_cartId = cartId;
_dbContext = new juicyDataContext();
}

///
/// Add Product to the cart object
///

public GenericResponse AddProduct(CartProduct myProduct)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();

try
{
CartItem myItem = new CartItem();
myItem.cartId = _cartId;
myItem.isbn10 = myProduct.isbn10;
myItem.isbn13 = myProduct.isbn13;
myItem.quantity = myProduct.quantity;
myItem.price = myProduct.price;
myItem.productCode = myProduct.productcode;

_dbContext.CartItems.InsertOnSubmit(myItem);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Sets the delivery address for the cart object
///

public GenericResponse SetDeliveryAddress(Address myAddress)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();
int deliveryId = Convert.ToInt32(addCartAddress(myAddress, AddressType.Delivery));

try
{
if (deliveryId > 0)
{
var query = (from c in _dbContext.Carts
where c.id == _cartId
select c).Single();

query.deliveryId = deliveryId;

_dbContext.SubmitChanges();
genResponse.Response = Response.OK;
}
else
{
genResponse.ex = new Exception("Delivery address id not returned");
genResponse.Response = Response.ERROR;
}

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_exists;
Static.LogError(ex_exists);
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Sets the billing address for the cart object
///

public GenericResponse SetBilingAddress(Address myAddress)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();
int billingId = Convert.ToInt32(addCartAddress(myAddress,AddressType.Billing));

try
{
if (billingId > 0)
{
var query = (from c in _dbContext.Carts
where c.id == _cartId
select c).Single();

query.billingId = billingId;

_dbContext.SubmitChanges();
genResponse.Response = Response.OK;
}
else
{
genResponse.ex = new Exception("Billing address id not returned");
genResponse.Response = Response.ERROR;
}

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_exists;
Static.LogError(ex_exists);
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Adds an address to the cart object and sets the type
///

private GenericResponse addCartAddress(Address myAddress, AddressType type)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();

try
{
CartAddress myCartAddress = new CartAddress();
myCartAddress.type = Convert.ToInt32(type);
myCartAddress.title = myAddress.title;
myCartAddress.firstname = myAddress.firstname;
myCartAddress.surname = myAddress.surname;
myCartAddress.companyname = myAddress.companyname;
myCartAddress.address1 = myAddress.address1;
myCartAddress.address2 = myAddress.address2;
myCartAddress.town = myAddress.town;
myCartAddress.county = myAddress.county;
myCartAddress.country = myAddress.country;
myCartAddress.postcode = myAddress.postcode;
myCartAddress.telephone = myAddress.phone;
myCartAddress.fax = myAddress.fax;

_dbContext.CartAddresses.InsertOnSubmit(myCartAddress);
_dbContext.SubmitChanges();

genResponse.information = myCartAddress.id.ToString();
genResponse.Response = Response.OK;


}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Removes a Product from the cart object database record
///

public GenericResponse RemoveProduct(int cartProductId)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();

try
{
var query = (from p in _dbContext.CartItems
where p.id == cartProductId
select p).Single();

_dbContext.CartItems.DeleteOnSubmit(query);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;
}
catch (InvalidOperationException ex_exits)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_exits;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Updates the Product quantity
///

public GenericResponse UpdateProduct(int cartProductId, int quantity)
{
CartIdSet();
GenericResponse genResponse = new GenericResponse();

try
{
var query = (from prod in _dbContext.CartItems
where prod.id == cartProductId
select prod).Single();

query.quantity = quantity;
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_exits)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_exits;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;
}

///
/// Returns a list of cart products
///

public List GetProducts(int page, int pageSize)
{
CartIdSet();

int startRecord = (page - 1) * pageSize;

List myList = null;

try
{
var query = from p in _dbContext.CartItems
where p.cartId == _cartId
select p;

foreach (CartItem myItem in query)
{
myList.Add(new CartProduct(myItem.isbn13,(decimal)myItem.price, myItem.productCode,
myItem.quantity,myItem.id,myItem.isbn10));
}

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// returns the totals for the current cart object
///

/// Set full to false if only return total vat and price. Flase returns all
public CartTotals GetTotals(bool full)
{
CartIdSet();

CartTotals myTotals = null;

try
{
if (full)
{

}
else
{
// var query = from prod in _dbContext.CartItems
// where prod.cartId == _cartId
// select

// myTotals = new CartTotals(
}

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myTotals;
}

///
/// Creates an order in the accounts system from the cart object and passed payment details
///

/// Card payment details
/// information - Auth Failed ()
public GenericResponse CreateOrder(CardPaymentDetails cardDetails)
{
CartIdSet();

GenericResponse genResponse = new GenericResponse();

try
{
/// PAYMENT AUTHORISATION GOES HERE

GenericResponse authResponse = authCartPaymentDetails(cardDetails);

if (authResponse.Response == Response.OK)
{
/// ORDER CODE TO GO HERE
}
else
{
genResponse.Response = Response.FAILEDAUTH;
genResponse.ex = authResponse.ex;
genResponse.information = authResponse.information;
}

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex_exists;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Clears the all related cart objects. Purpose for clearing cart once order made
///

public GenericResponse ClearCart()
{

GenericResponse genResponse = new GenericResponse();

try
{

/// Remove cart Items
var query_items = from c in _dbContext.CartItems
where c.cartId == _cartId
select c;

_dbContext.CartItems.DeleteAllOnSubmit(query_items);

var query_baddress = (from c in _dbContext.Carts
where c.id == _cartId
select c.billingId).Single();

var query_daddress = (from c in _dbContext.Carts
where c.id == _cartId
select c.deliveryId).Single();

/// Remove cart Addresses
var query_addresses = from c in _dbContext.CartAddresses
where c.id == query_baddress || c.id == query_daddress
select c;

_dbContext.CartAddresses.DeleteAllOnSubmit(query_addresses);

/// Remove cart object
var query_cart = (from c in _dbContext.Carts
where c.id == _cartId
select c).Single();

_dbContext.Carts.DeleteOnSubmit(query_cart);

_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex_exists;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Calls the payment api to check card details are valid
///

/// card payment details
private GenericResponse authCartPaymentDetails(CardPaymentDetails cardDetails)
{

GenericResponse genResponse = new GenericResponse();

try
{



genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_exists)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex_exists;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Checks if the cart id is set otherwiese throws an exception
///

private void CartIdSet()
{
if (_cartId == null)
throw new Exception("Cart Id Not Set");
}

}
}

==============================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using bebc.dal;
using bebc.types;

namespace bebc.bll
{

///
/// Admin Class is allows allow to admin level functions
///

public class Admin
{
juicyDataContext _dbContext;

public Admin()
{
_dbContext = new juicyDataContext();
}

#region Reviews

///
/// Returns a paginated list of Reviews
///

/// pass null to not filter by title
public List GetReviews(int page, int pageSize, AdminStatus status, string title)
{
List myList = new List();

try
{
int startRecord = (page - 1) * pageSize;

if (title != null)
{

if (status == AdminStatus.Both)
{
var queryTitle = (from r in _dbContext.Reviews
where r.title.Contains(title)
select r).Skip(startRecord).Take(pageSize);

myList = queryTitle.ToList();
}
else
{
var query = (from r in _dbContext.Reviews
where r.title.Contains(title) && r.status == (short)status
select r).Skip(startRecord).Take(pageSize);

myList = query.ToList();
}
}
else
{
if (status == AdminStatus.Both)
{
var queryTitle = (from r in _dbContext.Reviews
select r).Skip(startRecord).Take(pageSize);

myList = queryTitle.ToList();
}
else
{
var query = (from r in _dbContext.Reviews
where r.status == (short)status
select r).Skip(startRecord).Take(pageSize);

myList = query.ToList();
}
}

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// Returns the number of Review based on the status passed
///

public int GetReviewsCount(AdminStatus status, string title)
{
int myCount = 0;

try
{
if (title != null)
{

if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.Reviews
where r.title.Contains(title)
select r).Count();

myCount = queryAll;
}
else
{
var query = (from r in _dbContext.Reviews
where r.status == (short)status && r.title.Contains(title)
select r).Count();

myCount = query;
}
}
else
{
if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.Reviews
select r).Count();

myCount = queryAll;
}
else
{
var query = (from r in _dbContext.Reviews
where r.status == (short)status
select r).Count();

myCount = query;
}
}

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myCount;
}

///
/// Add a Review Review to the database
///

/// Id property does not need to be set
public GenericResponse AddReviews(Review ReviewReviews)
{
GenericResponse genResponse = new GenericResponse();

try
{

_dbContext.Reviews.InsertOnSubmit(ReviewReviews);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Removes a Review by id from the database
///

public GenericResponse deleteReviews(int ReviewsId)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.Reviews
where b.id == ReviewsId
select b).Single();

_dbContext.Reviews.DeleteOnSubmit(query);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Updates an existing Reviews details
///

public GenericResponse updateReviews(Review updateReviews)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.Reviews
where b.id == updateReviews.id
select b).Single();

if (updateReviews.title != null)
query.title = updateReviews.title;

if (updateReviews.posted != null)
query.posted = updateReviews.posted;

if (updateReviews.status != null)
query.status = updateReviews.status;

if (updateReviews.author != null)
query.author = updateReviews.author;

if (updateReviews.authoremail != null)
query.authoremail = updateReviews.authoremail;

if (updateReviews.county != null)
query.county = updateReviews.county;

if (updateReviews.country != null)
query.country = updateReviews.country;

if (updateReviews.body != null)
query.body = updateReviews.body;

_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}


#endregion

#region News

///
/// Returns a paginated list of News
///

/// pass null to not filter by title
public List GetNews(int page, int pageSize, AdminStatus status, string Title)
{
List myList = new List();

try
{
int startRecord = (page - 1) * pageSize;

if (Title != null)
{

if (status == AdminStatus.Both)
{
var queryTitle = (from r in _dbContext.NewsItems
where r.title.Contains(Title)
select r).Skip(startRecord).Take(pageSize);

myList = queryTitle.ToList();
}
else
{
var query = (from r in _dbContext.NewsItems
where r.title.Contains(Title) && r.Status == (short)status
select r).Skip(startRecord).Take(pageSize);

myList = query.ToList();
}
}
else
{
if (status == AdminStatus.Both)
{
var queryTitle = (from r in _dbContext.NewsItems
select r).Skip(startRecord).Take(pageSize);

myList = queryTitle.ToList();
}
else
{
var query = (from r in _dbContext.NewsItems
where r.Status == (short)status
select r).Skip(startRecord).Take(pageSize);

myList = query.ToList();
}
}

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// Returns the number of New based on the status passed
///

public int GetNewsCount(AdminStatus status, string title)
{
int myCount = 0;

try
{
if (title != null)
{
if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.NewsItems
where r.title.Contains(title)
select r).Count();

myCount = queryAll;
}
else
{
var query = (from r in _dbContext.NewsItems
where r.Status == (short)status && r.title.Contains(title)
select r).Count();

myCount = query;
}
}
else
{
if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.NewsItems
select r).Count();

myCount = queryAll;
}
else
{
var query = (from r in _dbContext.NewsItems
where r.Status == (short)status
select r).Count();

myCount = query;
}
}
}
catch (Exception ex)
{
Static.LogError(ex);
}

return myCount;
}

///
/// Add a new New to the database
///

/// Id property does not need to be set
public GenericResponse AddNews(NewsItem newNews)
{
GenericResponse genResponse = new GenericResponse();

try
{

_dbContext.NewsItems.InsertOnSubmit(newNews);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Removes a New by id from the database
///

public GenericResponse deleteNews(int NewsId)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.NewsItems
where b.id == NewsId
select b).Single();

_dbContext.NewsItems.DeleteOnSubmit(query);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Updates an existing News details
///

public GenericResponse updateNews(NewsItem updateNews)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.NewsItems
where b.id == updateNews.id
select b).Single();

if (updateNews.title != null)
query.title = updateNews.title;

if (updateNews.posted != null)
query.posted = updateNews.posted;

if (updateNews.Status != null)
query.Status = updateNews.Status;

if (updateNews.body != null)
query.body = updateNews.body;

_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}


#endregion

#region Banners

///
/// Returns a paginated list of banners
///

public List GetBanners(int page, int pageSize, AdminStatus status)
{
List myList = new List();

try
{
int startRecord = (page -1) * pageSize;

if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.Banners
select r).Skip(startRecord).Take(pageSize);

myList = queryAll.ToList();
}
else
{
var query = (from r in _dbContext.Banners
where r.status == (short)status
select r).Skip(startRecord).Take(pageSize);

myList = query.ToList();
}
}
catch(Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// Returns the number of banner based on the status passed
///

public int GetBannersCount(AdminStatus status)
{
int myCount = 0;

try
{

if (status == AdminStatus.Both)
{
var queryAll = (from r in _dbContext.Banners
select r).Count();

myCount = queryAll;
}
else
{
var query = (from r in _dbContext.Banners
where r.status == (short)status
select r).Count();

myCount = query;
}
}
catch (Exception ex)
{
Static.LogError(ex);
}

return myCount;
}

///
/// Add a new banner to the database
///

/// Id property does not need to be set
public GenericResponse AddBanner(Banner newBanner)
{
GenericResponse genResponse = new GenericResponse();

try
{

_dbContext.Banners.InsertOnSubmit(newBanner);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Removes a banner by id from the database
///

public GenericResponse deleteBanner(int bannerId)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.Banners
where b.id == bannerId
select b).Single();

_dbContext.Banners.DeleteOnSubmit(query);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Updates an existing banners details
///

public GenericResponse updateBanner(Banner updateBanner)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.Banners
where b.id == updateBanner.id
select b).Single();

if (updateBanner.imagealt != null)
query.imagealt = updateBanner.imagealt;

if (updateBanner.imageurl != null)
query.imageurl = updateBanner.imageurl;

if (updateBanner.status != null)
query.status = updateBanner.status;

if (updateBanner.url != null)
query.url = updateBanner.url;

_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}


#endregion

#region BestSellers

///
/// Returns a paginated list of BestSellers
///

public List GetBestSellers(int page, int pageSize)
{
List myList = new List();

try
{
int startRecord = (page - 1) * pageSize;


var queryAll = (from r in _dbContext.BestSellers
select r).Skip(startRecord).Take(pageSize);

myList = queryAll.ToList();

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myList;
}

///
/// Returns the number of BestSellers
///

public int GetBestSellersCount()
{
int myCount = 0;

try
{

var queryAll = (from r in _dbContext.BestSellers
select r).Count();

myCount = queryAll;

}
catch (Exception ex)
{
Static.LogError(ex);
}

return myCount;
}

///
/// Add a new BestSeller to the database
///

/// Id property does not need to be set
public GenericResponse AddBestSeller(BestSeller newBestSeller)
{
GenericResponse genResponse = new GenericResponse();

try
{

_dbContext.BestSellers.InsertOnSubmit(newBestSeller);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Removes a BestSeller by id from the database
///

public GenericResponse deleteBestSeller(int BestSellerId)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.BestSellers
where b.id == BestSellerId
select b).Single();

_dbContext.BestSellers.DeleteOnSubmit(query);
_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

///
/// Updates an existing BestSellers details
///

public GenericResponse updateBestSeller(BestSeller updateBestSeller)
{

GenericResponse genResponse = new GenericResponse();

try
{

var query = (from b in _dbContext.BestSellers
where b.id == updateBestSeller.id
select b).Single();

if (updateBestSeller.order != null)
query.order = updateBestSeller.order;

if (updateBestSeller.title != null)
query.title = updateBestSeller.title;

if (updateBestSeller.url != null)
query.url = updateBestSeller.url;

_dbContext.SubmitChanges();

genResponse.Response = Response.OK;

}
catch (InvalidOperationException ex_no)
{
genResponse.Response = Response.NORECORD;
genResponse.ex = ex_no;
}
catch (Exception ex)
{
genResponse.Response = Response.ERROR;
genResponse.ex = ex;
Static.LogError(ex);
}

return genResponse;

}

#endregion

}
}
=========================
using System;
using System.Security.Cryptography;
using System.Text;
using System.Globalization;

namespace bebc
{

///
/// Static provides simple generic functions and error logging
///

public class Static
{
///
/// Handles the passed error
///

public static void LogError(Exception ex, bool Throw)
{
throw ex;
}

///
/// Handles the passed error
///

public static void LogError(Exception ex)
{
throw ex;
}

///
/// Handles the passed error
///

public static void LogError(Exception ex, bool Throw, string information)
{
throw ex;
}

///
/// Converts the source text to title case
///

public static string ConvertToTitleText(string source)
{
//TextInfo myText = TextInfo);
//return myText.ToTitleCase(source);
return source;
}

///
/// Generates a hash of the passed text
///

public static string generateHash(string sourceText)
{
string result = "";

Encoder enc = Encoding.Unicode.GetEncoder();

byte[] unicodeText = new byte[sourceText.Length * 2];
enc.GetBytes(sourceText.ToCharArray(), 0, sourceText.Length, unicodeText, 0, true);

MD5 md5 = new MD5CryptoServiceProvider();
byte[] resultArray = md5.ComputeHash(unicodeText);


StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
sb.Append(resultArray[i].ToString("X2"));
}

result = sb.ToString();

md5.Clear();
md5 = null;
sb = null;
enc = null;

return result.ToUpper();
}

///
/// Creates a guid based random password of set length
///

public static string generateRandomPassword(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();

// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);

// Make sure length is valid
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

// Return the first length bytes
return guidResult.Substring(0, length);
}
}
}
=======================at; } }



using System;

namespace bebc.types
{

///
/// GenericItem returns a id and name property
///

public class KeyValueItem
{
private string _id;
private string _name;

public KeyValueItem(string id, string name)
{
_id = id;
_name = name;
}

public string Id { get { return _id; } }
public string Name { get { return _name; } }
}

///
/// SimpleProduct has minimal bits of information about a product
///

public class SimpleProduct
{
private string _isbn13;
private string _title;
private string _category;
private string _isbn10;
private string _id;
public SimpleProduct(string isbn13, string title, string category, string isbn10, string id)
{
_isbn13 = isbn13;
_title = title;
_category = category;
_isbn10 = isbn10;
_id = id;
}
public string isbn13 { get { return _isbn13; } }
public string title { get { return _title; } }
public string category { get { return _category; } }
public string isbn10 { get { return _isbn10; } }
public string id { get { return _id; } }
}

///
/// SpecialOffer has products details for an item within spcial offers
///

public class SpecialOffer
{
private string _isbn13;
private string _title;
private decimal _currentPrice;
private string _category;
private decimal _fullPrice;
private string _id;
private string _isbn10;
public SpecialOffer(string isbn13, string title, decimal currentPrice, string category, decimal fullPrice, string id, string isbn10)
{
_isbn13 = isbn13;
_title = title;
_currentPrice = currentPrice;
_category = category;
_fullPrice = fullPrice;
_id = id;
_isbn10 = isbn10;
}
public string isbn13 { get { return _isbn13; } }
public string title { get { return _title; } }
public decimal currentPrice { get { return _currentPrice; } }
public string category { get { return _category; } }
public decimal fullPrice { get { return _fullPrice; } }
public string id { get { return _id; } }
public string isbn10 { get { return _isbn10; } }
}

///
/// ProductDetails has information about a product
///

public class ProductDetail
{
private string _id;
private string _isbn13;
private string _stockStatus;
private string _category;
private string _publisher;
private string _title;
private string _authors;
private decimal _fullPrice;
private string _description;
private string _isbn10;
private decimal _currentPrice;
public ProductDetail(string id, string isbn13, string stockStatus, string category, string publisher, string title, string authors, decimal fullPrice, string description, string isbn10, decimal currentPrice)
{
_id = id;
_isbn13 = isbn13;
_stockStatus = stockStatus;
_category = category;
_publisher = publisher;
_title = title;
_authors = authors;
_fullPrice = fullPrice;
_description = description;
_isbn10 = isbn10;
_currentPrice = currentPrice;
}
public string id { get { return _id; } }
public string isbn13 { get { return _isbn13; } }
public string stockStatus { get { return _stockStatus; } }
public string category { get { return _category; } }
public string publisher { get { return _publisher; } }
public string title { get { return _title; } }
public string authors { get { return _authors; } }
public decimal fullPrice { get { return _fullPrice; } }
public string description { get { return _description; } }
public string isbn10 { get { return _isbn10; } }
public decimal currentPrice { get { return _currentPrice; } }
}

///
/// Address has information about a user address
///

public class Address
{
private string _address2;
private string _companyname;
private string _postcode;
private string _county;
private string _country;
private string _id;
private string _surname;
private string _phone;
private string _address1;
private string _title;
private string _firstname;
private string _email;
private string _town;
private string _fax;
public Address(string address2, string companyname, string postcode, string county, string country, string id, string surname, string phone, string address1, string title, string firstname, string email, string town, string fax)
{
_address2 = address2;
_companyname = companyname;
_postcode = postcode;
_county = county;
_country = country;
_id = id;
_surname = surname;
_phone = phone;
_address1 = address1;
_title = title;
_firstname = firstname;
_email = email;
_town = town;
_fax = fax;
}
public string address2 { get { return _address2; } }
public string companyname { get { return _companyname; } }
public string postcode { get { return _postcode; } }
public string county { get { return _county; } }
public string country { get { return _country; } }
public string id { get { return _id; } }
public string surname { get { return _surname; } }
public string phone { get { return _phone; } }
public string address1 { get { return _address1; } }
public string title { get { return _title; } }
public string firstname { get { return _firstname; } }
public string email { get { return _email; } }
public string town { get { return _town; } }
public string fax { get { return _fax; } }
}

///
/// Holds credit card payment details for order processing
///

public class CardPaymentDetails
{
private string _cardno;
private string _issueno;
private string _cardtype;
private string _authcode;
private string _expirydate;
private string _startdate;
public CardPaymentDetails(string cardno, string issueno, string cardtype, string authcode, string expirydate, string startdate)
{
_cardno = cardno;
_issueno = issueno;
_cardtype = cardtype;
_authcode = authcode;
_expirydate = expirydate;
_startdate = startdate;
}
public string cardno { get { return _cardno; } }
public string issueno { get { return _issueno; } }
public string cardtype { get { return _cardtype; } }
public string authcode { get { return _authcode; } }
public string expirydate { get { return _expirydate; } }
public string startdate { get { return _startdate; } }
}


///
/// Allow you to result a generic response which shows errors, status etc
///

public struct GenericResponse
{
///
/// Database Status Response Message
///

public Response Response;

///
/// Any additional information
///

public string information;

///
/// Exception if exists
///

public Exception ex;
}


///
/// Profile details of a user
///

public class Profile
{
private string _address2;
private string _id;
private string _postcode;
private string _address1;
private string _county;
private string _country;
private string _type;
private string _surname;
private string _phone;
private string _companyname;
private string _title;
private string _currency;
private string _firstname;
private string _email;
private string _town;
private string _fax;
public Profile(string address2, string id, string postcode, string address1, string county, string country, string type, string surname, string phone, string companyname, string title, string currency, string firstname, string email, string town, string fax)
{
_address2 = address2;
_id = id;
_postcode = postcode;
_address1 = address1;
_county = county;
_country = country;
_type = type;
_surname = surname;
_phone = phone;
_companyname = companyname;
_title = title;
_currency = currency;
_firstname = firstname;
_email = email;
_town = town;
_fax = fax;
}
public string address2 { get { return _address2; } }
public string id { get { return _id; } }
public string postcode { get { return _postcode; } }
public string address1 { get { return _address1; } }
public string county { get { return _county; } }
public string country { get { return _country; } }
public string type { get { return _type; } }
public string surname { get { return _surname; } }
public string phone { get { return _phone; } }
public string companyname { get { return _companyname; } }
public string title { get { return _title; } }
public string currency { get { return _currency; } }
public string firstname { get { return _firstname; } }
public string email { get { return _email; } }
public string town { get { return _town; } }
public string fax { get { return _fax; } }
}

///
/// Holds information about a cart product item
///

public class CartProduct
{
private string _isbn13;
private decimal _price;
private string _productcode;
private int _quantity;
private int _id;
private string _isbn10;

///
/// For Getting Product Details as Id set
///

public CartProduct(string isbn13, decimal price, string productcode, int quantity, int id, string isbn10)
{
_isbn13 = isbn13;
_price = price;
_productcode = productcode;
_quantity = quantity;
_id = id;
_isbn10 = isbn10;
}

///
/// For adding a product, id is set in database
///

public CartProduct(string isbn13, decimal price, string productcode, int quantity, string isbn10)
{
_isbn13 = isbn13;
_price = price;
_productcode = productcode;
_quantity = quantity;
_isbn10 = isbn10;
}
public string isbn13 { get { return _isbn13; } }
public decimal price { get { return _price; } }
public string productcode { get { return _productcode; } }
public int quantity { get { return _quantity; } }
public int id { get { return _id; } }
public string isbn10 { get { return _isbn10; } }
}

///
/// Holds totals of the cart object
///

public class CartTotals
{
private decimal _total;
private decimal _subtotalvat;
private decimal _subtotal;
private decimal _postage;
private decimal _discount;
private decimal _totalvat;
public CartTotals(decimal total, decimal subtotalvat, decimal subtotal, decimal postage, decimal discount, decimal totalvat)
{
_total = total;
_subtotalvat = subtotalvat;
_subtotal = subtotal;
_postage = postage;
_discount = discount;
_totalvat = totalvat;
}
public decimal total { get { return _total; } }
public decimal subtotalvat { get { return _subtotalvat; } }
public decimal subtotal { get { return _subtotal; } }
public decimal postage { get { return _postage; } }
public decimal discount { get { return _discount; } }
public decimal totalvat { get { return _totalvat; } }
}
}


}
}

No comments: