private async void BtnCreate_Click(object sender, RoutedEventArgs e)
        {
            RealEstate realEstate = RealEstateFactory.addRealEstate(
                (Location)Enum.Parse(typeof(Location), cbxLocation.SelectedItem.ToString()),
                (RealEstateType)Enum.Parse(typeof(RealEstateType), cbxTypes.SelectedItem.ToString()),
                Convert.ToDouble(txtSquaremeter.Text, CultureInfo.InvariantCulture),
                Convert.ToInt32(txtRooms.Text),
                Convert.ToDouble(txtGardenSquaremeter.Text, CultureInfo.InvariantCulture),
                Convert.ToInt32(txtParkinglots.Text));

            if (realEstate != null)
            {
                txtResult.Text = "Result: \n" + realEstate.ToString();
                string stringPayload = await Task.Run(() => JsonConvert.SerializeObject(new RealEstateModel(realEstate.GetType().Name, realEstate.getLocation().ToString(), realEstate.getRooms(), realEstate.getSquaremeter(), realEstate.getGarden_squaremeter(), realEstate.getNum_of_parkinglots())));

                StringContent       httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                HttpResponseMessage response    = await DAL.PostAsync(REST_URL, httpContent);

                if (response.IsSuccessStatusCode)
                {
                    btnShowInList.IsEnabled = true;
                }
                else
                {
                    btnShowInList.IsEnabled = false;
                }
            }
            else
            {
                txtResult.Text = "Not possible to create " + cbxTypes.SelectedItem.ToString() + " in " + cbxLocation.SelectedItem.ToString() + ".";
            }
        }
        public void editSuggestion()
        {
            Console.WriteLine("Enter suggestion ID ");
            int        suggestionId = Convert.ToInt32(Console.ReadLine());
            Suggestion s            = businessLogic.FindSuggestionById(suggestionId);
            Client     c            = businessLogic.FindClientById(s.Client.ClientId);

            Console.WriteLine(c.ToString());
            foreach (RealEstateSuggestion res in s.RealEstateSuggestions)
            {
                RealEstate re = businessLogic.FindRealEstateById(res.RealEstateId);
                Console.WriteLine(re.ToString());
            }
            Console.WriteLine("Enter real estate ID ");
            int reID = Convert.ToInt32(Console.ReadLine());

            foreach (RealEstateSuggestion res in s.RealEstateSuggestions)
            {
                if (res.RealEstateId == reID)
                {
                    businessLogic.DeleteRealEstateSuggestion(res);
                    break;
                }
            }
        }
        public void printSuggestion()
        {
            Console.WriteLine("Enter suggestion ID ");
            int suggestionId = Convert.ToInt32(Console.ReadLine());

            foreach (Suggestion s in businessLogic.FindSuggestionsByClientId(suggestionId))
            {
                Console.WriteLine("Suggestion Id: " + s.SuggestionId);
                Client c = businessLogic.FindClientById(suggestionId);
                Console.WriteLine(c.ToString());
                foreach (RealEstateSuggestion res in s.RealEstateSuggestions)
                {
                    RealEstate re = businessLogic.FindRealEstateById(res.RealEstateId);
                    Console.WriteLine(re.ToString());
                }
            }
        }
Example #4
0
        /// <summary>
        /// Sends newly created RealEstate to backen. Repeats request if no connection
        /// </summary>
        /// <param name="realEstate"></param>
        /// <returns></returns>
        public async Task <RealEstate> PostNewRealEstate(RealEstate realEstate) //Kan behöva optimering men funkar, status code 200
        {
            Console.WriteLine(realEstate.ToString());


            try
            {
                //http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accesstoken);
                HttpResponseMessage response = await http.PostAsJsonAsync("api/Realestates", realEstate);

                string responseContent = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseContent);
                if (response.IsSuccessStatusCode)
                {
                    RealEstate newEstate = JsonSerializer.Deserialize <RealEstate>(responseContent);
                    return(newEstate);
                }
                return(null);
            }
            catch (HttpRequestException)
            {
                Console.WriteLine("An error Occured");
            }
            catch (NotSupportedException)
            {
                Console.WriteLine("Content type is not supported");
            }
            catch (JsonException)
            {
                Console.WriteLine("Invalid Json");
            }
            catch
            {
                CancellationToken cancellationToken = new CancellationToken();
                RepeatPOST(
                    () => http.PostAsJsonAsync("api/Realestates", realEstate), // Request that will be sent offline
                    5,                                                         // time intervall between tries
                    realEstate.Address,                                        // Description to show for user
                    cancellationToken);                                        // A way to instantly abort this operation
            }
            return(null);
        }