public ActionResult <bool> Update(int id, PlanetsModel entity) { try { return(Ok(_repository.Update(id, entity))); } catch (Exception ex) { _logger.LogError($"Error: Controller exception on Update(int id, T entity)"); return(StatusCode((int)HttpStatusCode.InternalServerError, new { Error = ex.Message })); } }
public ActionResult <bool> Create(PlanetsModel input) { try { return(Ok(_repository.Create(input))); } catch (Exception ex) { _logger.LogError($"Error: Controller exception on Create(T input)"); return(StatusCode((int)HttpStatusCode.InternalServerError, new { Error = ex.Message })); } }
public IActionResult Index() { Console.WriteLine("Indiquez le numéro de la planète souhaitée"); Int32 planetNumber = Convert.ToInt32(Console.ReadLine()); WebClient client = new WebClient(); String planetSelectedResponse = client.DownloadString("https://swapi.dev/api/planets/" + planetNumber + "/"); PlanetsModel planetSelected = JsonConvert.DeserializeObject <PlanetsModel>(planetSelectedResponse); return(View(planetSelected)); }
public bool Delete(int id) { ICollection <PlanetsModel> planets = ReadAll(); PlanetsModel model = planets.FirstOrDefault(x => x.Id == id); if (model != default) { bool remove = planets.Remove(model); if (remove) { File.WriteAllText(_path, JsonSerializer.Serialize(planets)); return(true); } } return(false); }
private async Task <PlanetsModel> GetPlanet(Uri uri) { PlanetsModel planetModel = new PlanetsModel(); if (int.TryParse(uri.ToString().Split('/')[5], out int id)) { using HttpResponseMessage httpResponse = await _httpClient.GetAsync(uri); if (httpResponse.StatusCode == HttpStatusCode.OK && httpResponse.Content != null) { planetModel = await JsonSerializer.DeserializeAsync <PlanetsModel>(await httpResponse.Content.ReadAsStreamAsync()); planetModel.Id = id; } } return(planetModel); }
private async Task <List <PlanetsModel> > GetAllPlanets(List <PlanetsModel> planets, Result result) { if (result?.Planets != default) { foreach (Uri uri in result.Planets) { Thread.Sleep(2000); PlanetsModel planet = await GetPlanet(uri); if (planet != default) { planets.Add(planet); } } } return(planets); }
public bool Create(PlanetsModel entity) { if (entity == default) { return(false); } ICollection <PlanetsModel> planets = ReadAll(); PlanetsModel model = planets.FirstOrDefault(x => x.Id == entity.Id); if (model == default) { planets.Add(entity); File.WriteAllText(_path, JsonSerializer.Serialize(planets)); return(true); } return(false); }
public bool Update(int id, PlanetsModel entity) { if (entity is null) { return(false); } ICollection <PlanetsModel> planets = ReadAll(); PlanetsModel model = planets.FirstOrDefault(x => x.Id == id); if (model != default) { bool remove = planets.Remove(model); if (remove) { planets.Add(entity); File.WriteAllText(_path, JsonSerializer.Serialize(planets)); return(true); } } return(false); }