Skip to content
Snippets Groups Projects
Select Git revision
  • a54fa5527ed64c9532a19d5c9647e3574f30f7cc
  • master default
  • dev
3 results

FightController.cs

Blame
  • user avatar
    Clément authored
    a54fa552
    History
    FightController.cs 10.19 KiB
    using System;
    using System.Net;
    using System.Collections.Specialized;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVC.Models;
    using System.Net.Http;
    using Newtonsoft.Json;
    using System.Threading.Tasks;
    using System.Net.Http.Headers;
    
    namespace MVC.Controllers
    {
        public class FightController : Controller
        {
            // GET: Fight
            public async Task<ActionResult> Index()
            {
                IEnumerable<FightModels> Fights = new List<FightModels>();
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/Fight");
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        Fights = JsonConvert.DeserializeObject<List<FightModels>>(temp);
                    }
                }
                return View(Fights);
            }
    
            // GET: Fght/Details/1
            public async Task<ActionResult> Details(int id)
            {
                FightModels character = new FightModels();
                IEnumerable<FightModels> relationships = new List<FightModels>();
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/Fight/" + id);
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        character = JsonConvert.DeserializeObject<FightModels>(temp);
                    }
                }
                return View(character);
            }
    
            // GET: Fght/Create
            public async Task<ActionResult> Create()
            {
                List<HouseModels> houses = new List<HouseModels>();
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/house");
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        houses = JsonConvert.DeserializeObject<List<HouseModels>>(temp);
                    }
                }
                ViewBag.ListHouses = houses.Select(h => new SelectListItem()
                {
                    Text = h.Name,
                    Value = h.ID.ToString()
                });
                List<WarModels> wars = new List<WarModels>();
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/war");
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        wars = JsonConvert.DeserializeObject<List<WarModels>>(temp);
                    }
                }
                ViewBag.ListWar = wars.Select(w => new SelectListItem()
                {
                    Text = w.Name,
                    Value = w.ID.ToString()
                });
                return View();
            }
    
            // POST: Fights/Create
            [HttpPost]
            public async Task<ActionResult> Create(FightModels collection)
            {
                FightModels fight = new FightModels();
                fight.Name = collection.Name;
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/house/" + collection.Challenger1.ID);
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        fight.Challenger1 = JsonConvert.DeserializeObject<HouseModels>(temp);
                    }
                }
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/house/" + collection.Challenger2.ID);
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        fight.Challenger2 = JsonConvert.DeserializeObject<HouseModels>(temp);
                    }
                }
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/war/" + collection.War.ID);
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        fight.War = JsonConvert.DeserializeObject<WarModels>(temp);
                    }
                }
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    String jsonString = JsonConvert.SerializeObject(fight);
                    StringContent content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json");
                    HttpResponseMessage response = await client.PostAsync("api/fight", content);
                }
    
                IEnumerable<FightModels> Fights = new List<FightModels>();
                IEnumerable<FightModels> Fi = new List<FightModels>();
                FightModels F = new FightModels();
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/Fight");
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        Fights = JsonConvert.DeserializeObject<List<FightModels>>(temp);
                    }
                }
                Fi=Fights.Where(c => c.Name.Trim() == fight.Name).ToList();
                F = Fi.First();
                return RedirectToAction("Details/"+F.ID);
            }
    
            // GET: Fight/Edit/1
            public ActionResult Edit(int id)
            {
                return View();
            }
    
            // POST: Fight/Edit/1
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            // GET: Fight/Delete/1
            public async Task<ActionResult> Delete(int id)
            {
                FightModels fight = new FightModels();
    
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://localhost:13666/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    HttpResponseMessage response = await client.GetAsync("api/fight/" + id);
                    if (response.IsSuccessStatusCode)
                    {
                        string temp = await response.Content.ReadAsStringAsync();
                        fight = JsonConvert.DeserializeObject<FightModels>(temp);
                    }
                }
    
                return View(fight);
            }
    
            // POST: Fight/Delete/2
            [HttpPost]
            public async Task<ActionResult> Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri("http://localhost:13666/");
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(
                            new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                        HttpResponseMessage response = await client.DeleteAsync("api/fight/" + id);
                    }
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }