using System.Collections.Generic; using skydiveLogs_api.Data.Interface; using skydiveLogs_api.Model; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.IO; using System.Linq; namespace skydiveLogs_api.Data { public class JumpRepository : IJumpRepository { public IEnumerable GetAllJumps() { IEnumerable result = new List(); using (StreamReader file = File.OpenText(@"Data/Jump.json")) using (JsonTextReader reader = new JsonTextReader(file)) { var jsonResult = (JArray)JToken.ReadFrom(reader); result = jsonResult.ToObject>(); } return result; } public Jump GetJumpById(int id) { Jump result; using (StreamReader file = File.OpenText(@"Data/Jump.json")) using (JsonTextReader reader = new JsonTextReader(file)) { var jsonResult = (JArray)JToken.ReadFrom(reader); var tmp = jsonResult.ToObject>(); result = tmp.SingleOrDefault(t => t.Id == id); } return result; } public bool AddJump(Jump newJump) { var result = true; List jumpList; try { using (StreamReader file = File.OpenText(@"Data/Jump.json")) using (JsonTextReader reader = new JsonTextReader(file)) { var jsonResult = (JArray)JToken.ReadFrom(reader); jumpList = jsonResult.ToObject>(); } newJump.Id = jumpList.Count() + 1; jumpList.Add(newJump); string outputJson = JsonConvert.SerializeObject(jumpList); File.WriteAllText(@"Data/Jump.json", outputJson); } catch { result = false; } return result; } } }