diff --git a/.gitignore b/.gitignore index 104b544..e4b72b4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from `dotnet new gitignore` +appsettings.json + # dotenv files .env diff --git a/App.xaml.cs b/App.xaml.cs index 8239297..839c851 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,29 +1,15 @@ -using menu.Data; -using menu.ViewModels; - -namespace menu +namespace menu { public partial class App : Application { public App() { InitializeComponent(); - MainPage = new AppShell(); } protected override void OnStart() { - CheckDeadlinesAsync(); - } - - private async void CheckDeadlinesAsync() - { - var database = new MenuDatabase(); - - var viewModel = new MainViewModel(database); - - await viewModel.CheckDeadlinesAsync(); } protected override void OnSleep() diff --git a/AppShell.xaml b/AppShell.xaml index 51f648b..8a3d72f 100644 --- a/AppShell.xaml +++ b/AppShell.xaml @@ -11,4 +11,8 @@ Title="List" ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage" /> + diff --git a/Data/AzureFunctionService.cs b/Data/AzureFunctionService.cs new file mode 100644 index 0000000..6937a8a --- /dev/null +++ b/Data/AzureFunctionService.cs @@ -0,0 +1,97 @@ +using Microsoft.Extensions.Configuration; + +namespace menu.Data +{ + public class AzureFunctionService + { + private readonly HttpClient httpClient; + private readonly IConfiguration configuration; + + public AzureFunctionService(HttpClient httpClient, IConfiguration configuration) + { + this.httpClient = httpClient; + this.configuration = configuration; + } + + public async Task ShareListAzure(string shareCode, string senderID, string listID, string listData) + { + string shareListUrl = $"https://anonylistfunctions.azurewebsites.net/api/ShareList?shareCode={shareCode}&senderID={senderID}&listID={listID}&listData={listData}"; + string functionKey = configuration["SHARE_LIST_KEY"]; + + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, shareListUrl); + request.Headers.Add("x-functions-key", functionKey); + + var response = await httpClient.SendAsync(request); + + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadAsStringAsync(); + } + else + { + throw new Exception("Error sharing list"); + } + } + + public async Task ReceiveSharedListAzure(string shareCode, string senderID) + { + string receiveSharedListUrl = $"https://anonylistfunctions.azurewebsites.net/api/ReceiveSharedList?shareCode={shareCode}&senderID={senderID}"; + string functionKey = configuration["RECEIVE_SHARED_LIST_KEY"]; + + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, receiveSharedListUrl); + request.Headers.Add("x-functions-key", functionKey); + + var response = await httpClient.SendAsync(request); + + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadAsStringAsync(); + } + else + { + throw new Exception("Error receiving shared list"); + } + } + + public async Task CheckQueueAzure(string userID) + { + string checkQueueUrl = $"https://anonylistfunctions.azurewebsites.net/api/CheckQueue?userID={userID}"; + string functionKey = configuration["CHECK_QUEUE_KEY"]; + + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, checkQueueUrl); + request.Headers.Add("x-functions-key", functionKey); + + var response = await httpClient.SendAsync(request); + + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadAsStringAsync(); + } + else + { + throw new Exception("Error checking the queue for list updates"); + } + } + + public async Task UpdateListAzure(string senderID, string listID, string recipientIDList, string listData) + { + string updateListUrl = $"https://anonylistfunctions.azurewebsites.net/api/UpdateList?senderID={senderID}&listID={listID}&recipientIDList={recipientIDList}&listData={listData}"; + string functionKey = configuration["UPDATE_LIST_KEY"]; + + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, updateListUrl); + request.Headers.Add("x-functions-key", functionKey); + + var response = await httpClient.SendAsync(request); + + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadAsStringAsync(); + } + else + { + throw new Exception("Error updating the list " + listID); + } + + } + } +} diff --git a/Data/MenuDatabase.cs b/Data/MenuDatabase.cs index 947cf2e..c916fbf 100644 --- a/Data/MenuDatabase.cs +++ b/Data/MenuDatabase.cs @@ -5,41 +5,75 @@ namespace menu.Data { public class MenuDatabase { - private SQLiteConnection db; + private readonly SQLiteConnection db; - private static string DatabasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "menu.db"); + private static readonly string DatabasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "menu.db"); public MenuDatabase() { db = new SQLiteConnection(DatabasePath); db.CreateTable(); db.CreateTable(); + db.CreateTable(); + } + + public User GetDefaultUser() + { + return db.Query("SELECT * FROM user WHERE id = 0").FirstOrDefault(); } public List GetUserLists() { - List lists = db.Query("SELECT * FROM user_lists"); + List lists; + + lists = db.Query("SELECT * FROM user_lists WHERE is_in_trash = 0"); if (lists == null || lists.Count == 0) { + db.Insert(defaultUser); + db.Insert(defaultUserList); db.Insert(changeTitleItem); db.Insert(addListItemItem); db.Insert(addListItem); db.Insert(deleteListItemItem); - lists = db.Query("SELECT * FROM user_lists"); + + lists = db.Query("SELECT * FROM user_lists WHERE is_in_trash = 0"); } return lists; } + public List GetTrashUserLists() + { + List trashLists; + + trashLists = db.Query("SELECT * FROM user_lists WHERE is_in_trash = 1"); + + if (trashLists == null || trashLists.Count == 0) + { + db.Insert(defaultUser); + + db.Insert(defaultUserList); + + db.Insert(changeTitleItem); + db.Insert(addListItemItem); + db.Insert(addListItem); + db.Insert(deleteListItemItem); + + trashLists = db.Query("SELECT * FROM user_lists WHERE is_in_trash = 1"); + } + + return trashLists; + } + public int SaveUserList(UserList list) { if (list.Id != 0) { db.Update(list); - return list.Id; + return list.Id; } else { @@ -49,27 +83,41 @@ public int SaveUserList(UserList list) } } - public UserList DeleteUserList(UserList list) + public void MoveToTrash(UserList list) { - int result = db.Delete(list); - if (result == 0) + if (list.Id == 1) { - return null; - } else if (result == 1) - { - return list; - } else + db.Update(list); + } + if (list.Id != 1) { - throw new Exception(string.Format("Error occurred trying to delete UserList {0}", list)); + list.IsInTrash = true; + db.Update(list); } } + public void RestoreFromTrash(UserList list) + { + list.IsInTrash = false; + db.Update(list); + } + + public void DeleteUserListPermanently() + { + db.Execute("DELETE FROM user_lists WHERE is_in_trash = 1"); + } + public List GetListItemsByListId(int id) { return db.Table().Where(li => li.UserListId == id).ToList(); } + public List GetTrashLists() + { + return db.Table().Where(li => li.IsInTrash == true).ToList(); + } + public ListItem GetListItemById(int id) { return db.Table().Where(li => li.Id == id).FirstOrDefault(); @@ -80,7 +128,8 @@ public int SaveListItem(ListItem li) if (li.Id != 0) { return db.Update(li); - } else + } + else { return db.Insert(li); } @@ -92,15 +141,23 @@ public ListItem DeleteListItem(ListItem li) if (result == 0) { return null; - } else if (result == 1) + } + else if (result == 1) { return li; - } else + } + else { throw new Exception(string.Format("Error occurred trying to delete ListItem {0}", li)); } } + private readonly User defaultUser = new() + { + Id = 0, + Uuid = Guid.NewGuid().ToString() + }; + private readonly UserList defaultUserList = new() { Name = "Welcome", diff --git a/Data/SharingService.cs b/Data/SharingService.cs new file mode 100644 index 0000000..b9c1f6d --- /dev/null +++ b/Data/SharingService.cs @@ -0,0 +1,121 @@ +using menu.Models; +using Newtonsoft.Json; + +namespace menu.Data +{ + public class SharingService + { + private readonly AzureFunctionService azureFunctionService; + + public SharingService(AzureFunctionService azureFunctionService) + { + this.azureFunctionService = azureFunctionService; + } + + public async Task ShareList(User user, string shareCode, UserList list) + { + try + { + var result = await azureFunctionService.ShareListAzure(shareCode, user.Uuid, list.UserUuid, list.ToAzureString()); + if (result != null) + { + return true; + } + return false; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } + + public async Task ReceiveSharedList(string userId, string shareCode) + { + try + { + var result = await azureFunctionService.ReceiveSharedListAzure(shareCode, userId); + if (result != null) + { + return JsonConvert.DeserializeObject(result); + } + return null; + } + catch (Exception ex) + { + if (ex is JsonSerializationException) + { + Console.WriteLine("Failure to deserialize the result: " + ex.Message); + return null; + } + else + { + Console.WriteLine(ex.Message); + return null; + } + } + } + + public async Task CheckQueue(string userId) + { + try + { + var result = await azureFunctionService.CheckQueueAzure(userId); + if (result != null) + { + return JsonConvert.DeserializeObject(result); + } + return null; + } + catch (Exception ex) + { + if (ex is JsonSerializationException) + { + Console.WriteLine("Failure to deserialize the result: " + ex.Message); + return null; + } + else + { + Console.WriteLine(ex.Message); + return null; + } + } + } + + public async Task UpdateList(string senderId, UserList list, string recipientIdList) + { + try + { + var result = await azureFunctionService.UpdateListAzure(senderId, list.UserUuid, recipientIdList, list.ToAzureString()); + if (result != null) + { + return true; + } + return false; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } + + public async Task UpdateList(string senderId, UserList list, List recipientIdList) + { + try + { + var result = await azureFunctionService.UpdateListAzure(senderId, list.UserUuid, string.Join(",", recipientIdList), list.ToAzureString()); + if (result != null) + { + return true; + } + return false; + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + return false; + } + } + } +} diff --git a/MainPage.xaml b/MainPage.xaml index 947c9f9..fbd3e8a 100644 --- a/MainPage.xaml +++ b/MainPage.xaml @@ -13,9 +13,11 @@ FontAttributes="Bold" FontSize="Large" Text="View Lists" /> + - + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - -