narsil/Tanshu.Accounts.SqlDAO/WaiterDAO.cs

102 lines
4.4 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Tanshu.Accounts.Contracts;
using Tanshu.Data.DAO;
using Tanshu.Accounts.DAOFactory;
namespace Tanshu.Accounts.SqlDAO
{
public class WaiterDAO : BaseDAO, IWaiterDAO
{
public WaiterDAO(IConnectionDAO connection)
: base(connection)
{ }
public bool Insert(WaiterBO waiter)
{
using (SqlCommand cmd = new SqlCommand(@"
SELECT @WaiterID = NEWID(), @Code = ISNULL(MAX(Code), 0) + 1 FROM Waiters;
INSERT INTO Waiters (WaiterID, Code, Name) VALUES (@WaiterID, @Code, @Name);
SELECT @timestamp = timestamp FROM Waiters WHERE WaiterID = @WaiterID;
"))
{
cmd.Parameters.Add("@WaiterID", System.Data.SqlDbType.UniqueIdentifier);
cmd.Parameters["@WaiterID"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add("@Code", System.Data.SqlDbType.Int);
cmd.Parameters["@Code"].Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.AddWithValue("@Name", waiter.Name);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
waiter.WaiterID = (Guid)cmd.Parameters["@WaiterID"].Value;
waiter.Code = (int)cmd.Parameters["@Code"].Value;
waiter.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public bool Update(WaiterBO waiter)
{
using (SqlCommand cmd = new SqlCommand(@"
UPDATE Waiters SET Name = @Name, Code = @Code WHERE WaiterID = @WaiterID;
SELECT @timestamp = timestamp FROM Waiters WHERE WaiterID = @WaiterID;
"))
{
cmd.Parameters.AddWithValue("@WaiterID", waiter.WaiterID);
cmd.Parameters.AddWithValue("@Code", waiter.Code);
cmd.Parameters.AddWithValue("@Name", waiter.Name);
cmd.Parameters.Add("@timestamp", System.Data.SqlDbType.Timestamp);
cmd.Parameters["@timestamp"].Direction = System.Data.ParameterDirection.Output;
connection.ExecuteNonQuery(cmd);
waiter.timestamp = (byte[])cmd.Parameters["@timestamp"].Value;
return true;
}
}
public bool Delete(Guid waiterID)
{
bool result;
using (SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM SaleVoucher WHERE WaiterID = @WaiterID"))
{
cmd.Parameters.AddWithValue("@WaiterID", waiterID);
result = 0 == (int)connection.ExecuteScalar(cmd);
}
if (result == false)
return false;
using (SqlCommand cmd = new SqlCommand("DELETE FROM Waiters WHERE WaiterID = @WaiterID"))
{
cmd.Parameters.AddWithValue("@WaiterID", waiterID);
connection.ExecuteNonQuery(cmd);
return true;
}
}
public WaiterBO GetWaiter(Guid waiterID)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Waiters WHERE WaiterID = @WaiterID");
cmd.Parameters.AddWithValue("@WaiterID", waiterID);
return BusinessObjectDAO<WaiterBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public WaiterBO GetWaiter(int code)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Waiters WHERE Code = @Code");
cmd.Parameters.AddWithValue("@Code", code);
return BusinessObjectDAO<WaiterBO>.GetBusinessObject(connection.ExecuteReader(cmd));
}
public List<WaiterBO> GetWaiters()
{
return BusinessObjectDAO<WaiterBO>.GetBusinessObjects(connection.ExecuteReader("SELECT * FROM Waiters ORDER BY Name"));
}
public List<WaiterBO> GetFilteredWaiters(Dictionary<string, string> filter)
{
string name = string.Format("%{0}%", filter["Name"].Trim());
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Waiters WHERE Name LIKE @Name ORDER BY Name"))
{
cmd.Parameters.AddWithValue("@Name", name);
return BusinessObjectDAO<WaiterBO>.GetBusinessObjects(connection.ExecuteReader(cmd));
}
}
}
}