private void Favorite_Click(object sender, RoutedEventArgs e) { var product = (Product)((Button)sender).Tag; productid = product.ProductID; if (Login.CustomerID != 0) { var checkbasket = db.Favorites.Where(bs => bs.ProductID == product.ProductID && bs.CustomerID == Login.CustomerID); if (checkbasket.Count() != 0) { MainWindow.Snackbar.IsActive = true; MainWindow.SnackbarMessage.Content = "The product is already in your favorites!"; } else { Model.Favorite favorite = new Model.Favorite() { CustomerID = Login.CustomerID, ProductID = product.ProductID, }; db.Favorites.Add(favorite); db.SaveChanges(); MainWindow.Snackbar.IsActive = true; MainWindow.SnackbarMessage.Content = "Add to favorite!"; } } else if (Login.CustomerID == 0) { MainWindow.Snackbar.IsActive = true; MainWindow.SnackbarMessage.Content = "To add to favorites you need to register or log in to your account!"; } }
public static async Task UpdateFavoriteActive(Model.Favorite Favorite) { var jsdata = await BitKubBot.Services.GoogleAPI.GetFavoriteJSON(Favorite.lineid, Favorite.symbol); if (jsdata.IndexOf("\"" + Favorite.symbol + "\"") > -1) { int start = jsdata.IndexOf("#"); int end = jsdata.IndexOf("lineid"); var id = jsdata.Substring(start, end - start).Replace("\"", "").Replace(",", "").Replace(":", "").Replace("#", "");//'#":"86bb6f6a30a9","' var url = "https://script.google.com/macros/s/AKfycbxe6QG2n8IRTWyv4nFMl3UMeUKp-6_i0wQlDbIVkN2xOC59f5jB-gYz1Q/exec?path=/favorite2&method=PUT"; var json = "{\"#\" : \"" + id + "\",\"active\" : \"" + Convert.ToInt16(Favorite.active).ToString() + "\",\"lineid\":\"" + Favorite.lineid + "\",\"symbol\":\"" + Favorite.symbol + "\",\"createdate\":\"" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\"}"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(HttpMethod.Post, url)) { request.SetBrowserRequestMode(BrowserRequestMode.NoCors); //var json = JsonConvert.SerializeObject(content); using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json")) { client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); request.Content = stringContent; request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); request.Content.Headers.ContentType = null; using (var response = await client .SendAsync(request, HttpCompletionOption.ResponseHeadersRead) .ConfigureAwait(false)) { response.EnsureSuccessStatusCode(); if (!response.IsSuccessStatusCode) { var errorMessage = response.ReasonPhrase; Console.WriteLine($"Error! {errorMessage}"); //return errorMessage; } } } } } }