public IActionResult CreateTournament(CreateTournamentDto model)
 {
     if (ModelState.IsValid)
     {
         var tournamentEntity = _mapper.Map <TournamentEntity>(model);
         _tournamentService.Add(tournamentEntity);
         return(RedirectToAction("Index", "Home", new { area = String.Empty }));
     }
     return(View());
 }
 public IActionResult Create([FromBody] Tournament tournament)
 {
     try
     {
         _logger.LogInformation($"{tournament} added to database");
         _tournamentService.Add(tournament);
         return(Ok(tournament));
     }
     catch (Exception ex)
     {
         _logger.LogError($"Post Request failed for tornament", ex);
         return(BadRequest($"Invalid tournament object submited {ex}"));
     }
 }
Ejemplo n.º 3
0
        public async Task <ActionResult> Create(TournamentModels item, HttpPostedFileBase idFile)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    #region connection
                    Uri uri = new Uri("https://challonge.com/api/tournaments.json");

                    List <evenement> evenements = eventService.GetMany().ToList();
                    SelectList       dropList   = new SelectList(evenements, "id_Evenement", "name");
                    ViewBag.dropList = dropList;

                    string uniqueName = Guid.NewGuid().ToString() +
                                        System.IO.Path.GetExtension(idFile.FileName);
                    var uploadUrl = Server.MapPath("~/Content/img");

                    idFile.SaveAs(Path.Combine(uploadUrl, uniqueName));

                    item.image_link = "img\\" + uniqueName;
                    tournament t = new tournament()
                    {
                        id_tournament          = item.id_tournament,
                        description            = item.description,
                        adresse                = item.adresse,
                        name                   = item.name,
                        image_link             = item.image_link,
                        nbrPlaces              = item.nbrPlaces,
                        broadcast_link         = item.broadcast_link,
                        latitude               = item.latitude,
                        longitude              = item.longitude,
                        evenement              = item.evenement,
                        evenement_id_Evenement = item.evenement_id_Evenement,
                        participanttournaments = item.participanttournaments,
                        url = "GNation" + item.name
                    };


                    //Create an Http client and set the headers we want
                    HttpClient aClient   = new HttpClient();
                    var        byteArray = Encoding.ASCII.GetBytes("mhaasif1:futMbBLufRuMKaxsIpBhaMxhaevOXsz1JBmH2liV");
                    aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    aClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
                    aClient.DefaultRequestHeaders.Host          = uri.Host;

                    //Create a Json Serializer for our type
                    JsonSerializer jsonSer = JsonSerializer.CreateDefault();

                    var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://challonge.com/api/tournaments.json");
                    httpWebRequest.ContentType = "text/json";
                    httpWebRequest.Method      = "POST";

                    #endregion

                    using (var sw = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        using (JsonWriter writer = new JsonTextWriter(sw))
                        {
                            #region Serialize
                            jsonSer.Serialize(writer, t);

                            string  json = JsonConvert.SerializeObject(t);
                            JObject jObj = JObject.Parse(json);
                            jObj.Property("id_tournament").Remove();
                            jObj.Property("description").Remove();
                            jObj.Property("adresse").Remove();
                            jObj.Property("image_link").Remove();
                            jObj.Property("nbrPlaces").Remove();
                            jObj.Property("broadcast_link").Remove();
                            jObj.Property("latitude").Remove();
                            jObj.Property("longitude").Remove();
                            jObj.Property("evenement").Remove();
                            jObj.Property("evenement_id_Evenement").Remove();
                            jObj.Property("participanttournaments").Remove();
                            string        output     = JsonConvert.SerializeObject(jObj);
                            StringContent theContent = new StringContent(output, Encoding.UTF8, "application/json");
                            #endregion

                            //Post the data
                            HttpResponseMessage aResponse = await aClient.PostAsync(uri, theContent);

                            System.Diagnostics.Debug.WriteLine(aResponse.ToString());
                            if (aResponse.IsSuccessStatusCode)
                            {
                                tournamentService.Add(t);
                            }

                            else
                            {
                                // show the response status code
                                String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " - Reason: " + aResponse.ReasonPhrase;
                                System.Diagnostics.Debug.WriteLine(failureMsg);
                            }
                        }
                    }
                }
                return(RedirectToAction("Index"));
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);

                return(View());
            }
        }
        public async Task <IActionResult> Add([FromBody] TournamentDTO tournamentDTO)
        {
            await _tournamentService.Add(_mapper.Map <Tournament>(tournamentDTO));

            return(new OkObjectResult(true));
        }