示例#1
0
        public void UpdateRebel(RebelParams rebel)
        {
            var path        = "Data/RebelsRepository.json";
            var list        = GetRebels(path);
            var rebelUpdate = list.First(x => x.Name == rebel.Name);

            if (rebelUpdate != null)
            {
                rebelUpdate.Name   = rebel.Name;
                rebelUpdate.Planet = rebel.Planet;
                rebelUpdate.Date   = DateTime.Now;
            }
            var jsonToOutput = JsonConvert.SerializeObject(list, Formatting.Indented);

            SaveList(jsonToOutput, path);
        }
示例#2
0
        public void AddRebel(RebelParams rebel)
        {
            try
            {
                var path = "Data/RebelsRepository.json";
                var json = GetRepository(path);
                var list = JsonConvert.DeserializeObject <List <Rebel> >(json);
                list.Add(new Rebel(rebel.Name, rebel.Planet));

                var jsonToOutput = JsonConvert.SerializeObject(list, Formatting.Indented);
                SaveList(jsonToOutput, path);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public IActionResult Update([FromBody] RebelParams rebel)
 {
     try
     {
         if (rebel != null)
         {
             _rebelService.UpdateRebel(rebel);
             _logger.LogInformation("Rebel Updated in the system");
             return(Ok());
         }
         else
         {
             return(NotFound());
         }
     }
     catch (Exception ex)
     {
         _logger.LogError($"Failed to update the rebel: {ex}");
     }
     return(BadRequest("Failed to update the rebel"));
 }