public static RemoveItemResponse RemoveItem(dynamic jsonRequestData, SqlConnection connection, ILogger log) { string item = (string)jsonRequestData; FindItemResponse resp = TryFindItem(item, connection, log); if (resp.Count == 0) { return(new RemoveItemResponse(false, item)); } var queryString = $@" DELETE FROM dbo.Tags WHERE Tags.NameKey LIKE @param1; DELETE FROM dbo.Items WHERE Items.NameKey LIKE @param1;"; using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandText = queryString; command.Parameters.AddWithValue("@param1", QueryHelper.Instance.SingularizeAndLower(item)); int itemsRemoved = command.ExecuteNonQuery(); if (itemsRemoved > 0) { Item i = resp.Result.First(); return(new RemoveItemResponse(itemsRemoved > 0, item, i.Row, i.Col)); } else { return(new RemoveItemResponse(false, item)); } } }
public static BundleWithResponse BundleWithItem(string text, int quantity, SqlConnection connection, ILogger log) { string newItem = string.Empty; string existingItem = string.Empty; TagSet tagSet; if (text.Contains(" add tags ")) { string[] itemsAndTags = text.Split(" add tags ", StringSplitOptions.RemoveEmptyEntries); string[] newAndExistingItem = itemsAndTags[0].Split(" with ", StringSplitOptions.RemoveEmptyEntries); newItem = newAndExistingItem[0].Trim(); existingItem = newAndExistingItem[1].Trim(); tagSet = new TagSet(itemsAndTags[1]); } else { string[] newAndExistingItem = text.Split(" with ", StringSplitOptions.RemoveEmptyEntries); newItem = newAndExistingItem[0].Trim(); existingItem = newAndExistingItem[1].Trim(); tagSet = new TagSet(newItem); } FindItemResponse foundItems = TryFindItem(existingItem, connection, log); if (foundItems.Count == 1) { Item foundItem = foundItems.Result[0]; Item item = new Item { Name = newItem, Quantity = quantity, Row = foundItem.Row.Value, Col = foundItem.Col.Value, IsSmallBox = foundItem.IsSmallBox.Value }; if (TryInsertItem(item, connection, log)) { InsertTags(connection, newItem, tagSet); FindItemResponse resp = FindItem(newItem, connection, log); if (resp.Count > 0) { return(new BundleWithResponse(true, newItem, quantity, existingItem, item.Row, item.Col)); } else { return(new BundleWithResponse(false, newItem, quantity, existingItem)); } } } return(new BundleWithResponse()); }
// 1. Check if item exists, if yes, return the box it's in. // 2. Query for currently used boxes, find an empty box if one exists, and return it's row/column location // 3. Insert an entry into the Items table with the row/column info // 4. If successful, insert entries into the Tags table with the words from the item name as tags // 4. Return a response with data indicating the insert was successful, and the row/column info // // Format of info can be: // a. "<item name>" // b. "<item name> into a <small box|big box> with tags <tag0 tag1 tag2 ...>" // c. "<item name> with tags <tag0 tag1 tag2 ...> into a <small box|big box>" // // Todo: Singularize item public static ICommandResponse InsertItem(dynamic jsonRequestData, SqlConnection connection, ILogger log) { string info = jsonRequestData["Info"]; int quantity = jsonRequestData["Quantity"]; string infoLower = info.ToLowerInvariant(); bool hasBox = TryGetBoxInfo(infoLower, out int boxIndex, out string boxSearch, out bool useSmallBox); bool hasTags = TryGetTagsInfo(infoLower, boxIndex, out int tagsIndex, out TagSet tagSet); string itemName = GetItemInfo(info, hasBox, hasTags, boxIndex, tagsIndex); tagSet.ParseAndUnionWith(itemName); FindItemResponse findItemResponse = FindItem(itemName, connection, log); if (findItemResponse.Count > 0) { return(findItemResponse); } // Item doesn't exist; insert. // Find existing boxes var sqlAllConsumedBoxes = string.Format("SELECT DISTINCT ROW,COL FROM dbo.Items"); MatrixModel matrix = new MatrixModel(); using (SqlCommand command = new SqlCommand(sqlAllConsumedBoxes, connection)) { SqlDataReader reader = command.ExecuteReader(); try { while (reader.Read()) { matrix.AddItem((int)reader[Dbo.Items.Row], (int)reader[Dbo.Items.Col]); } } finally { reader.Close(); } } var(row, col) = matrix.GetNextAvailableBox(useSmallBox); if (row == -1 && col == -1) { return(new InsertItemResponse(false)); } Item item = new Item(itemName, quantity, row, col, useSmallBox); return(InsertItemWithTags(item, tagSet, connection, log)); }
public static HowManyResponse HowMany(dynamic jsonRequestData, SqlConnection connection, ILogger log) { string itemName = jsonRequestData; FindItemResponse itemResponse = TryFindItem(itemName, connection, log); if (itemResponse.Count == 1) { Item i = itemResponse.Result.First(); return(new HowManyResponse(true, i.Name, i.Quantity, i.Row, i.Col)); } else { return(new HowManyResponse(false, itemName)); } }