71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
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<Jump> GetAllJumps()
|
|
{
|
|
IEnumerable<Jump> result = new List<Jump>();
|
|
|
|
using (StreamReader file = File.OpenText(@"Data/Jump.json"))
|
|
using (JsonTextReader reader = new JsonTextReader(file))
|
|
{
|
|
var jsonResult = (JArray)JToken.ReadFrom(reader);
|
|
result = jsonResult.ToObject<IEnumerable<Jump>>();
|
|
}
|
|
|
|
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<IEnumerable<Jump>>();
|
|
result = tmp.SingleOrDefault(t => t.Id == id);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool AddJump(Jump newJump)
|
|
{
|
|
var result = true;
|
|
List<Jump> 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<List<Jump>>();
|
|
}
|
|
|
|
newJump.Id = jumpList.Count() + 1;
|
|
jumpList.Add(newJump);
|
|
|
|
string outputJson = JsonConvert.SerializeObject(jumpList);
|
|
File.WriteAllText(@"Data/Jump.json", outputJson);
|
|
}
|
|
catch
|
|
{
|
|
result = false;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|