Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Bug_Lib.BugType;
- using System.IO;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Xml;
- namespace Bug_Lib.Repository
- {
- public interface IStorage
- {
- void WriteFile(int sprint, List<TaskBase> lst, string fileName); //пишем в файл
- bool ReadFromFile(out int sprint, List<TaskBase> lst, string fileName); //читаем из файла
- }
- public class Storage
- {
- private JObject _jData;
- private XmlDocument _doc;
- public class Json : Storage, IStorage
- {
- public void WriteFile(int sprint, List<TaskBase> lsBases, string fileName)
- {
- #region JSON FORMATTER
- JObject jData = new JObject(
- new JProperty("Sprint", sprint),
- new JProperty("ChangesDate", DateTime.Now.ToString("MM-dd-yyyy-HH:mm")),
- new JProperty("Tasks",
- new JArray(
- from task in lsBases
- orderby lsBases.Count // порядок сортировки
- select new JObject(
- new JProperty("ID", task.ID),
- new JProperty("Type", task.Type),
- new JProperty("Name", task.Name),
- new JProperty("Description", task.Description),
- new JProperty("items",
- new JObject(
- new JProperty("SprintNumber", task.SprintNumber),
- new JProperty("Priority", task.Priority),
- new JProperty("Difficulty", task.Dificulty),
- new JProperty("Iterations", task.Iterations),
- new JProperty("Status", task.Status))
- )))));
- #endregion
- File.WriteAllText($@"{fileName}.json", jData.ToString());
- }
- public bool ReadFromFile(out int sprint, List<TaskBase> tasks, string fileName)
- {
- sprint = 1;
- if (!File.Exists($@"{fileName}.json") || new FileInfo($@"{fileName}.json").Length == 0)
- {
- return false;
- }
- else
- {
- tasks.Clear();
- }
- _jData = JsonConvert.DeserializeObject<JObject>(File.ReadAllText($@"{fileName}.json"));
- sprint = (int)_jData.SelectToken("Sprint");
- foreach (var task in _jData.SelectToken("Tasks"))
- {
- switch (task["Type"].ToString())
- {
- case "Bug":
- tasks.Add(JsonConvert.DeserializeObject<Bug>(task.ToString()));
- break;
- case "Feature":
- tasks.Add(JsonConvert.DeserializeObject<Feature>(task.ToString()));
- break;
- case "Technical debt":
- tasks.Add(JsonConvert.DeserializeObject<TechDebt>(task.ToString()));
- break;
- }
- }
- return true;
- }
- }
- public class Xml : Storage, IStorage
- {
- public void WriteFile(int sprint, List<TaskBase> lsBases, string fileName)
- {
- #region JSON FORMATTER
- JObject jData = new JObject(
- new JProperty("Sprint", sprint),
- new JProperty("ChangesDate", DateTime.Now.ToString("MM-dd-yyyy-HH:mm")),
- new JProperty("Tasks",
- new JArray(
- from task in lsBases
- orderby lsBases.Count // порядок сортировки
- select new JObject(
- new JProperty("ID", task.ID),
- new JProperty("Type", task.Type),
- new JProperty("Name", task.Name),
- new JProperty("Description", task.Description),
- new JProperty("items",
- new JObject(
- new JProperty("SprintNumber", task.SprintNumber),
- new JProperty("Priority", task.Priority),
- new JProperty("Difficulty", task.Difficulty),
- new JProperty("Iterations", task.Iterations),
- new JProperty("Status", task.Status))
- )))));
- #endregion
- _doc = JsonConvert.DeserializeXmlNode(jData.ToString(), "Root");
- _doc.Save($@"{fileName}.xml");
- }
- public bool ReadFromFile(out int sprint, List<TaskBase> tasks, string fileName)
- {
- sprint = 1;
- if (!File.Exists($@"{fileName}.xml") || new FileInfo($@"{fileName}.xml").Length == 0)
- {
- return false;
- }
- else
- {
- tasks.Clear();
- }
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(File.ReadAllText($@"{fileName}.xml"));
- _jData = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeXmlNode(doc));
- sprint = (int)_jData.SelectToken("Root.Sprint");
- foreach (var task in _jData.SelectToken("Root.Tasks"))
- {
- switch (task["Type"].ToString())
- {
- case "Bug":
- tasks.Add(JsonConvert.DeserializeObject<Bug>(task.ToString()));
- break;
- case "Feature":
- tasks.Add(JsonConvert.DeserializeObject<Feature>(task.ToString()));
- break;
- case "Technical debt":
- tasks.Add(JsonConvert.DeserializeObject<TechDebt>(task.ToString()));
- break;
- }
- }
- return true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement