From 9e4dee1852f6ea0cea1c1497c3f8b8e7640394f6 Mon Sep 17 00:00:00 2001 From: Natasa Kasikovic~ Date: Thu, 28 Mar 2024 22:55:17 +0100 Subject: [PATCH] [Add] Implementation of Repository class --- LangLang/Core/Repository/Repository.cs | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 LangLang/Core/Repository/Repository.cs diff --git a/LangLang/Core/Repository/Repository.cs b/LangLang/Core/Repository/Repository.cs new file mode 100644 index 00000000..86882f9c --- /dev/null +++ b/LangLang/Core/Repository/Repository.cs @@ -0,0 +1,43 @@ +using LangLang.Core.Repository.Serialization; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LangLang.Core.Repository +{ + public class Repository where T : ISerializable, new() + { + private readonly string _fileName = @"../../../../LangLang/Data/{0}"; + private readonly Serializer _serializer = new Serializer(); + + public Repository(string fileName) + { + _fileName = string.Format(_fileName, fileName); + } + public List Load() + { + if (!File.Exists(_fileName)) + { + FileStream fs = File.Create(_fileName); + fs.Close(); + } + + IEnumerable lines = File.ReadLines(_fileName); + List objects = _serializer.FromCSV(lines); + + return objects; + } + + public void Save(List objects) + { + string serializedObjects = _serializer.ToCSV(objects); + using (StreamWriter streamWriter = new StreamWriter(_fileName)) + { + streamWriter.Write(serializedObjects); + } + } + } +}