Skip to content

Commit

Permalink
Saving ItemLocationData to Database now
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Jul 23, 2022
1 parent ff6fa88 commit fa9689f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
3 changes: 2 additions & 1 deletion ShoppingList/Model/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Item : ObservableObject
[Column("iscompleted")]
public bool IsCompleted { get; set; }

[Ignore]
[OneToOne]
[Column("itemlocationdata")]
public ItemLocationData LocationData { get; set; }
}
22 changes: 22 additions & 0 deletions ShoppingList/Model/ItemLocationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,37 @@

namespace ShoppingList.Model
{
[Table("ItemLocationData")]
public class ItemLocationData
{
[AutoIncrement, PrimaryKey]
[Column("id")]
public int Id { get; set; }

[Column("baynumber")]
public string BayNumber { get; set; }

[Column("description")]
public string Description { get; set; }

[Column("number")]
public string Number { get; set; }

[Column("numberoffacing")]
public string NumberOfFacing { get; set; }

[Column("side")]
public string Side { get; set; }

[Column("shelfnumber")]
public string ShelfNumber { get; set; }

[Column("shelfpositioninbay")]
public string ShelfPositionInBay { get; set; }

[ForeignKey(typeof(Item))]
[Column("parentid")]
public int ParentId { get; set; }

}
}
2 changes: 2 additions & 0 deletions ShoppingList/Services/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DatabaseHandler()
_pathToDb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "shoppinglist_sqlite.db");
_db = new SQLiteConnection(_pathToDb);
_db.CreateTable<Item>();
_db.CreateTable<ItemLocationData>();
_db.CreateTable<UserList>();
}

Expand All @@ -34,6 +35,7 @@ public DatabaseHandler(string newDBName)
_pathToDb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), newDBName);
_db = new SQLiteConnection(_pathToDb);
_db.CreateTable<Item>();
_db.CreateTable<ItemLocationData>();
_db.CreateTable<UserList>();
}

Expand Down
6 changes: 5 additions & 1 deletion ShoppingList/Services/ItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public List<Item> GetItemByParentId(UserList ul)

public Item CreateItem(Item newItem)
{
return _db.Insert(newItem);
var item = _db.Insert(newItem);
newItem.LocationData.ParentId = item.Id;
newItem.Id = item.Id;
_db.Insert<ItemLocationData>(newItem.LocationData);
return newItem;

}

Expand Down
6 changes: 4 additions & 2 deletions ShoppingList/Services/ListSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public static List<Item> SortUserListItems(UserList userList)
{
// Code to sort the Aisles here, let's do aphabetical for now

List<Item> list = userList.Items;
List<Item> list = userList.Items;
//var sortedItems = List<Item>();

var sortedItems = list.OrderBy(x => x.Aisle).ToList();

var sortedItems = list.OrderBy(x => x.LocationData?.Number).ToList();

var anotherSort = sortedItems.OrderBy(x => x.IsCompleted).ToList();

Expand Down
11 changes: 1 addition & 10 deletions ShoppingList/ViewModel/ItemInputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public partial class ItemInputViewModel : BaseViewModel
[ObservableProperty]
UserList userList;

public ObservableCollection<Item> Items { get; set; } = new();

[ObservableProperty]
public string itemName;

Expand Down Expand Up @@ -71,19 +69,12 @@ public async void OnItemEntryCompleted()

newItem = _itemService.CreateItem(newItem);


newItem.LocationData.ParentId = newItem.Id;
UserList.Items.Add(newItem);

ListSorter.SortUserListItems(UserList);
ListSorter.SortUserListItems(userList);


Items.Clear();
foreach (var item in UserList.Items)
{
Items.Add(item);
}

await Shell.Current.GoToAsync($"{nameof(UserListDetails)}?", true,
new Dictionary<string, object>
{
Expand Down

0 comments on commit fa9689f

Please sign in to comment.