Ejemplo n.º 1
0
        public async Task <bool> addMeal(string date, string mealtype, string location, string soup, string maindish, string description, string accessToken)
        {
            //POST
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);


            MealData meal = new MealData();

            meal.date        = date;
            meal.meal_type   = mealtype;
            meal.location    = location;
            meal.soup        = soup;
            meal.main_dish   = maindish;
            meal.description = description;



            var json = JsonConvert.SerializeObject(meal);



            var buffer      = System.Text.Encoding.UTF8.GetBytes(json);
            var byteContent = new ByteArrayContent(buffer);


            var httpContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");

            byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //URI
            HttpResponseMessage response = await client.PostAsync("http://ticket-now.ddns.net:5000/api/meal", byteContent);

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


            JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(r);

            string s = jObject.Value <string>("error");



            if (response.IsSuccessStatusCode)
            {
                return(true);
            }


            else if (s.Equals("Meal type does not exists"))
            {
                await DisplayAlert("", "Please choose a meal type between Lunch, Dinner, Lunch_veg, Dinner_veg", "Ok");
            }
            else if (s.Equals("Location does not exists"))
            {
                await DisplayAlert("", "Please choose a location between Gualtar and Azurém", "Ok");
            }
            else if (s.Equals("Invalid date format (ISO Date format required)!"))
            {
                await DisplayAlert("", "Please insert a correct data format (YYYY-MM-DD)", "Ok");
            }
            else if (s.Equals("Meal already exists"))
            {
                await DisplayAlert("", "This meal already exists", "Ok");
            }

            return(false);
        }
Ejemplo n.º 2
0
        public async Task <bool> deleteMeal(string date, string mealtype, string location, string accessToken)
        {
            //Delete
            var client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);


            var request = new HttpRequestMessage(HttpMethod.Delete, "http://ticket-now.ddns.net:5000/api/meal");


            MealData meal = new MealData();

            meal.date      = date;
            meal.meal_type = mealtype;
            meal.location  = location;



            var json = JsonConvert.SerializeObject(meal);

            var buffer      = System.Text.Encoding.UTF8.GetBytes(json);
            var byteContent = new ByteArrayContent(buffer);


            request.Content = new StringContent(json, UnicodeEncoding.UTF8, "application/json");



            //URI
            HttpResponseMessage response = await client.SendAsync(request);

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


            JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(r);

            string s = jObject.Value <string>("error");

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }


            else if (s.Equals("Meal does not exists"))
            {
                await DisplayAlert("", "Meal does not exists", "Ok");
            }
            else if (s.Equals("Meal type does not exists"))
            {
                await DisplayAlert("", "Please choose a meal type between Lunch, Dinner, Lunch_veg, Dinner_veg", "Ok");
            }
            else if (s.Equals("Location does not exists"))
            {
                await DisplayAlert("", "Please choose a location between Gualtar and Azurém", "Ok");
            }
            else if (s.Equals("Arguments required"))
            {
                await DisplayAlert("", "Arguments required", "Ok");
            }


            return(false);
        }