public ActionResult AddElephant([FromBody] ElephantRequest addElephantRequest) { var addedElephant = _elephantService.AddElephant(addElephantRequest, FILEPATH); if (addedElephant != null) { return(Ok(addedElephant)); } else { return(NotFound()); } }
public Elephant AddElephant(ElephantRequest elephantToAdd, string filepath) { // Convert to Model var elephant = _mapper.Map <Elephant>(elephantToAdd); elephant.Id = Guid.NewGuid(); // Add to List var elephants = GetElephants(filepath).Result; elephants.Add(elephant); // Conver to Json & Write to file var json = JsonSerializer.Serialize(elephants); File.WriteAllText(filepath, json); return(elephant); }