Skip to content

Commit

Permalink
Added some Guard logic from CommunityToolkit.Diagnostics to the services
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Jul 24, 2022
1 parent 2fb08bd commit d1dd492
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 93 deletions.
1 change: 1 addition & 0 deletions ShoppingList/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
global using ShoppingList.Services;
global using ShoppingList.ViewModels;
global using SQLiteNetExtensions.Attributes;
global using CommunityToolkit.Diagnostics;


14 changes: 13 additions & 1 deletion ShoppingList/Services/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public DatabaseHandler()
/// </summary>
public DatabaseHandler(string newDBName)
{
Guard.IsNotNullOrEmpty(newDBName, nameof(newDBName));


_pathToDb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), newDBName);
_db = new SQLiteConnection(_pathToDb);
_db.CreateTable<Item>();
Expand All @@ -58,6 +61,9 @@ public DatabaseHandler(string newDBName)
/// <returns></returns>
public List<T> GetQueryByName<T>(string name) where T : new()
{

Guard.IsNotNullOrEmpty(name, nameof(name));

List<T> returnList = _db.Query<T>("SELECT * FROM " + typeof(T).Name + "s" + " WHERE name = '" + name + "'");

return returnList;
Expand All @@ -75,7 +81,7 @@ public DatabaseHandler(string newDBName)
/// <exception cref="Exception">If nothing is found, throws a generic exception</exception>
public T GetQueryById<T>(int id) where T : new()
{

List<T> returnList = _db.Query<T>("SELECT * FROM " + typeof(T).Name + "s" + " WHERE id = '" + id + "'");
if (returnList.Count > 0)
return returnList[0];
Expand Down Expand Up @@ -111,6 +117,8 @@ public DatabaseHandler(string newDBName)
/// <exception cref="Exception"></exception>
public T Insert<T>(T newData) where T : new()
{
Guard.IsNotNull(newData, nameof(newData));

var numOfRowsInserted = _db.Insert(newData);

if (numOfRowsInserted == 0)
Expand All @@ -127,6 +135,8 @@ public DatabaseHandler(string newDBName)
/// <exception cref="Exception"></exception>
public void Delete<T>(T deleteTarget) where T : new()
{
Guard.IsNotNull(deleteTarget, nameof(deleteTarget));

var numOfRowsFound = _db.Delete(deleteTarget);

if (numOfRowsFound == 0)
Expand All @@ -135,6 +145,8 @@ public DatabaseHandler(string newDBName)

public void Update<T>(T updateTarget) where T : new()
{
Guard.IsNotNull(updateTarget, nameof(updateTarget));

var numOfRowsFound = _db.Update(updateTarget);

if (numOfRowsFound == 0)
Expand Down
15 changes: 15 additions & 0 deletions ShoppingList/Services/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public ItemService()

public ItemService(DatabaseHandler db)
{
Guard.IsNotNull(db, nameof(db));

_db = db;
}

Expand All @@ -21,18 +23,23 @@ public List<Item> GetAllItems()

public List<Item> GetItemByName(string name)
{
Guard.IsNotNullOrEmpty(name, nameof(name));

var returnLists = _db.GetQueryByName<Item>(name);
return returnLists;
}

public Item GetItemById(Item item)
{
Guard.IsNotNull(item, nameof(item));

var returnItem = _db.GetQueryById<Item>(item.Id);
return returnItem;
}
public List<Item> GetUserListItems(UserList ul)
{
Guard.IsNotNull(ul, nameof(ul));

var itemList = _db.GetQueryByParentId<Item>(ul.Id);

foreach (Item item in itemList)
Expand All @@ -44,6 +51,8 @@ public List<Item> GetUserListItems(UserList ul)

public List<Item> GetItemByParentId(UserList ul)
{
Guard.IsNotNull(ul, nameof(ul));

var returnLists = _db.GetQueryByParentId<Item>(ul.Id);
foreach (Item item in returnLists)
{
Expand All @@ -55,6 +64,8 @@ public List<Item> GetItemByParentId(UserList ul)

public Item CreateItem(Item newItem)
{
Guard.IsNotNull(newItem, nameof(newItem));

var item = _db.Insert(newItem);
newItem.LocationData.ParentId = item.Id;
newItem.Id = item.Id;
Expand All @@ -65,11 +76,15 @@ public Item CreateItem(Item newItem)

public void DeleteItem(Item deletedItem)
{
Guard.IsNotNull(deletedItem, nameof(deletedItem));

_db.Delete(deletedItem);
}

public void UpdateItem(Item updatedItem)
{
Guard.IsNotNull(updatedItem, nameof(updatedItem));

_db.Update(updatedItem);
}
}
Expand Down
12 changes: 12 additions & 0 deletions ShoppingList/Services/KrogerAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public async Task<bool> SetAuthTokens(ApiConfig apiConfig)
if (accessToken is not null && DateTime.Now < expireTime)
return false;

Guard.IsNotNull(accessToken, nameof(accessToken));

var authData = $"{apiConfig.ClientId}:{apiConfig.ClientSecret}";
var authHeaderValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(authData));

Expand All @@ -57,8 +59,13 @@ public async Task<bool> SetAuthTokens(ApiConfig apiConfig)

public async Task<Dictionary<string, string>> GetLocationNearZip(string zip, ApiConfig apiConfig)
{

Guard.IsNotNullOrEmpty(zip, nameof(zip));
Guard.IsNotNull(apiConfig, nameof(apiConfig));

Dictionary<string, string> closeKrogerNames = new Dictionary<string, string>();


string zipQuery = $"?filter.zipCode.near={zip}&filter.radiusInMiles=25&filter.chain=KROGER";
Uri uri = new($"{apiConfig.KrogerUrl}locations{zipQuery}");

Expand Down Expand Up @@ -95,6 +102,11 @@ public async Task<Dictionary<string, string>> GetLocationNearZip(string zip, Api

public async Task<(ItemLocationData, Item)> GetProductLocationData(string term, string locationId, ApiConfig apiConfig)
{
Guard.IsNotNullOrEmpty(term, nameof(term));
Guard.IsNotNullOrEmpty(locationId, nameof(locationId));
Guard.IsNotNull(apiConfig, nameof(apiConfig));


string productQuery = $"?filter.locationId={locationId}&filter.term={term}&filter.fulfillment=ais&filter.limit=50";

Uri uri = new($"{apiConfig.KrogerUrl}products{productQuery}");
Expand Down
180 changes: 88 additions & 92 deletions ShoppingList/Services/ListSorter.cs
Original file line number Diff line number Diff line change
@@ -1,126 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShoppingList.Services;

namespace ShoppingList.Services
public class ListSorter
{
public class ListSorter
{

public static bool FrozenFoodLast { get; set; }
public static bool FrozenFoodLast { get; set; }

public static bool StartAtBackOfStore { get; set; }
public static bool StartAtBackOfStore { get; set; }


public static List<Item> SortUserListItems(UserList userList)
{
public static List<Item> SortUserListItems(UserList userList)
{

SetPreferences();
Guard.IsNotNull(userList, nameof(userList));
Guard.IsNotNull(userList.Items, nameof(userList.Items));

SetPreferences();


List<Item> list = userList.Items;
List<Item> meatList = new();
List<Item> produceList = new();
List<Item> aisleList = new();
List<Item> dairyList = new();
List<Item> frozenList = new();


List<Item> list = userList.Items;
List<Item> meatList = new();
List<Item> produceList = new();
List<Item> aisleList = new();
List<Item> dairyList = new();
List<Item> frozenList = new();

var sortedItems = new List<Item>();

var sortedItems = new List<Item>();
bool wasSorted = false;

bool wasSorted = false;
foreach(var item in list)
{
switch (item.Aisle)
{
case "Meat":
meatList.Add(item);
wasSorted = true;
break;
case "Seafood":
meatList.Add(item);
wasSorted = true;
break;
case "Dairy":
dairyList.Add(item);
wasSorted = true;
break;
case "Produce":
produceList.Add(item);
wasSorted = true;
break;
}

foreach(var item in list)
if (!wasSorted)
{
switch (item.Aisle)
if (item.Category.Contains("Frozen") && FrozenFoodLast)
{
case "Meat":
meatList.Add(item);
wasSorted = true;
break;
case "Seafood":
meatList.Add(item);
wasSorted = true;
break;
case "Dairy":
dairyList.Add(item);
wasSorted = true;
break;
case "Produce":
produceList.Add(item);
wasSorted = true;
break;
}

if (!wasSorted)
frozenList.Add(item);
} else
{
if (item.Category.Contains("Frozen") && FrozenFoodLast)
{
frozenList.Add(item);
} else
{
aisleList.Add(item);
}
aisleList.Add(item);
}

wasSorted = false;
}

meatList = meatList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList();
dairyList = dairyList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList();

aisleList = aisleList.OrderBy(x => Int32.Parse(x.LocationData.Number)).ToList();
wasSorted = false;
}

meatList = meatList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList();
dairyList = dairyList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList();

//research bay number order on aisles (do we want to do alternating asc/desc to form a 'route'?
aisleList = aisleList.OrderBy(x => Int32.Parse(x.LocationData.Number)).ToList();


foreach(var item in produceList)
{
sortedItems.Add(item);
}
//research bay number order on aisles (do we want to do alternating asc/desc to form a 'route'?


foreach(var item in aisleList)
{
sortedItems.Add(item);
}

foreach(var item in dairyList)
{
sortedItems.Add(item);
}

foreach(var item in meatList)
{
sortedItems.Add(item);
}

foreach(var item in frozenList)
{
sortedItems.Add(item);
}
foreach(var item in produceList)
{
sortedItems.Add(item);
}

if(StartAtBackOfStore)
{
sortedItems.Reverse();
}

foreach(var item in aisleList)
{
sortedItems.Add(item);
}

// Finish with IsCompleted Sort, so completed items are at the bottom of the list
var anotherSort = sortedItems.OrderBy(x => x.IsCompleted).ToList();
foreach(var item in dairyList)
{
sortedItems.Add(item);
}

userList.Items = anotherSort;
foreach(var item in meatList)
{
sortedItems.Add(item);
}

return anotherSort;
foreach(var item in frozenList)
{
sortedItems.Add(item);
}

public static void SetPreferences()
if(StartAtBackOfStore)
{
FrozenFoodLast = Preferences.Get("FrozenFoodLast", true);
StartAtBackOfStore = Preferences.Get("StartAtBackOfStore", false);
sortedItems.Reverse();
}


// Finish with IsCompleted Sort, so completed items are at the bottom of the list
var anotherSort = sortedItems.OrderBy(x => x.IsCompleted).ToList();

userList.Items = anotherSort;

return anotherSort;
}

public static void SetPreferences()
{
FrozenFoodLast = Preferences.Get("FrozenFoodLast", true);
StartAtBackOfStore = Preferences.Get("StartAtBackOfStore", false);
}
}

Loading

0 comments on commit d1dd492

Please sign in to comment.