public ShoppingListItem Create(int shoppingListId, ShoppingListItem source) { ShoppingListItem created = null; ShoppingList list = new ShoppingListDAO ().GetById (shoppingListId); if ( list == null || source.Item == null || source.Store == null ) { return null; } SqlConnection connection = ConnectionFactory.GetConnection (); SqlCommand command = new SqlCommand (QUERY_CREATE, connection); command.Parameters.AddWithValue ("@ShoppingListId", list.Id); command.Parameters.AddWithValue ("@ItemId", source.Item.Id); command.Parameters.AddWithValue ("@StoreId", source.Store.Id); command.Parameters.AddWithValue ("@Amount", source.Amount); command.Parameters.AddWithValue ("@Price", source.Price); command.Parameters.AddWithValue ("@Position", source.Position); command.Parameters.AddWithValue ("@Note", source.Note == null ? "" : source.Note); command.Parameters.AddWithValue ("@Checked", source.Checked); try { connection.Open (); int id = (int) command.ExecuteScalar (); created = GetById (id); } finally { connection.Close (); } return created; }
private ShoppingListItem ReadShoppingList(SqlDataReader reader) { ShoppingListItem listItem = new ShoppingListItem (); Item item = new Item (); Store store = new Store (); item.Id = (int) reader["ItemId"]; item.Name = reader["ItemName"].ToString (); item.Category = reader["Category"].ToString (); item.Brand = reader["Brand"].ToString (); item.Description = reader["Description"].ToString (); store.Id = (int) reader["StoreId"]; store.Name = reader["StoreName"].ToString (); store.Franchise = reader["Franchise"].ToString (); store.Address = reader["Address"].ToString (); listItem.Id = (int) reader["Id"]; listItem.Store = store; listItem.Item = item; listItem.Amount = (int) reader["Amount"]; listItem.Price = (decimal) reader["Price"]; listItem.Position = (int) reader["Position"]; listItem.Note = reader["Note"].ToString (); listItem.Checked = ((int) reader["Checked"]) < 1; return listItem; }
public ShoppingListItem Update(int id, ShoppingListItem source) { User me = CurrentUser.retrieve (); ShoppingListItem original = GetById (id); if ( original == null ) { return null; } SqlConnection connection = ConnectionFactory.GetConnection (); SqlCommand command = new SqlCommand (QUERY_UPDATE, connection); command.Parameters.AddWithValue ("@Id", original.Id); command.Parameters.AddWithValue ("@ItemId", source.Item == null ? original.Item.Id : source.Item.Id); command.Parameters.AddWithValue ("@StoreId", source.Store == null ? original.Store.Id : source.Store.Id); command.Parameters.AddWithValue ("@Amount", source.Amount == null ? original.Amount : source.Amount); command.Parameters.AddWithValue ("@Price", source.Price == null ? original.Price : source.Price); command.Parameters.AddWithValue ("@Position", source.Position == null ? original.Position : source.Position); command.Parameters.AddWithValue ("@Note", source.Note == null ? original.Note : source.Note); command.Parameters.AddWithValue ("@Checked", source.Checked == null ? original.Checked : source.Checked); try { connection.Open (); command.ExecuteScalar (); } finally { connection.Close (); } return GetById (original.Id); }