Exemplo n.º 1
0
 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 }));
     }
 }
Exemplo n.º 2
0
 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 }));
     }
 }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }