Exemplo n.º 1
0
        // GET: War/Create
        public async Task <ActionResult> Create()
        {
            CreateWarModel warModel = new CreateWarModel();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:62313");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("api/houses/");

                if (response.IsSuccessStatusCode)
                {
                    string temp = await response.Content.ReadAsStringAsync();

                    warModel.Houses = JsonConvert.DeserializeObject <List <HouseModel> >(temp);
                }
                else
                {
                    return(HttpNotFound());
                }
            }

            return(View(warModel));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create(CreateWarModel fight)
        {
            WarModel        warModel    = new WarModel();
            HttpWebResponse webResponse = null;
            int             idWar;

            if (fight.SelectedAllyHouse == fight.SelectedEnnemyHouse)
            {
                ModelState.AddModelError(string.Empty, "Vous devez sélectionner deux maisons différentes !");

                return(View("Create"));
            }

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:62313");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                /* Récupération première maison sélectionnée via API */
                HttpResponseMessage response = await client.GetAsync("api/houses/" + fight.SelectedAllyHouse);

                if (response.IsSuccessStatusCode)
                {
                    string temp = await response.Content.ReadAsStringAsync();

                    warModel.FirstHouse = JsonConvert.DeserializeObject <HouseModel>(temp);
                }
                else
                {
                    return(HttpNotFound());
                }

                /* Récupération deuxième maison sélectionnée via API */
                response = await client.GetAsync("api/houses/" + fight.SelectedEnnemyHouse);

                if (response.IsSuccessStatusCode)
                {
                    string temp = await response.Content.ReadAsStringAsync();

                    warModel.SecondHouse = JsonConvert.DeserializeObject <HouseModel>(temp);
                }
                else
                {
                    return(HttpNotFound());
                }

                /* Envoie de l'objet WarModel via API POST */
                var request = (HttpWebRequest)WebRequest.Create("http://localhost:62313/api/wars");
                request.Accept = "application/json; charset=utf-8";
                request.Method = "POST";

                var content = new JavaScriptSerializer().Serialize(warModel);
                var data    = Encoding.ASCII.GetBytes(content);

                request.ContentType   = "application/json";
                request.ContentLength = data.Length;

                using (var stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                webResponse = (HttpWebResponse)request.GetResponse();

                /* Récupération du nombre de War pour avoir l'ID pour redirection sur la page de Détails de la Guerre créee */
                response = await client.GetAsync("api/wars/");

                if (response.IsSuccessStatusCode)
                {
                    string temp = await response.Content.ReadAsStringAsync();

                    idWar = JsonConvert.DeserializeObject <List <WarModel> >(temp).Count;
                }
                else
                {
                    return(HttpNotFound());
                }
            }

            return(RedirectToAction("Details", new { id = idWar }));
        }