Exemplo n.º 1
0
 public ActionResult <bool> Update(int id, StarshipsModel 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(StarshipsModel 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 bool Delete(int id)
        {
            ICollection <StarshipsModel> starships = ReadAll();
            StarshipsModel model = starships.FirstOrDefault(x => x.Id == id);

            if (model != default)
            {
                bool remove = starships.Remove(model);
                if (remove)
                {
                    File.WriteAllText(_path, JsonSerializer.Serialize(starships));
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        private async Task <StarshipsModel> GetStarship(Uri uri)
        {
            StarshipsModel starship = new StarshipsModel();

            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)
                {
                    starship = await JsonSerializer.DeserializeAsync <StarshipsModel>(await httpResponse.Content.ReadAsStreamAsync());

                    starship.Id = id;
                }
            }
            return(starship);
        }
Exemplo n.º 5
0
        private async Task <List <StarshipsModel> > GetAllStarships(List <StarshipsModel> starships, Result result)
        {
            if (result?.Starships != default)
            {
                foreach (Uri uri in result.Starships)
                {
                    Thread.Sleep(2000);
                    StarshipsModel starship = await GetStarship(uri);

                    if (starship != default)
                    {
                        starships.Add(starship);
                    }
                }
            }
            return(starships);
        }
Exemplo n.º 6
0
        public bool Create(StarshipsModel entity)
        {
            if (entity == default)
            {
                return(false);
            }
            ICollection <StarshipsModel> starships = ReadAll();
            StarshipsModel model = starships.FirstOrDefault(x => x.Id == entity.Id);

            if (model == default)
            {
                starships.Add(entity);
                File.WriteAllText(_path, JsonSerializer.Serialize(starships));
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        public bool Update(int id, StarshipsModel entity)
        {
            if (entity is null)
            {
                return(false);
            }

            ICollection <StarshipsModel> starships = ReadAll();
            StarshipsModel model = starships.FirstOrDefault(x => x.Id == id);

            if (model != default)
            {
                bool remove = starships.Remove(model);
                if (remove)
                {
                    starships.Add(entity);
                    File.WriteAllText(_path, JsonSerializer.Serialize(starships));
                    return(true);
                }
            }
            return(false);
        }