Skip to content

Commit

Permalink
Merge pull request #12 from kzi-nastava/feat/Repository
Browse files Browse the repository at this point in the history
[Add] Implementation of Repository class
  • Loading branch information
anasinik authored Mar 28, 2024
2 parents c699bc1 + 9e4dee1 commit bac44fa
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions LangLang/Core/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -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<T> where T : ISerializable, new()
{
private readonly string _fileName = @"../../../../LangLang/Data/{0}";
private readonly Serializer<T> _serializer = new Serializer<T>();

public Repository(string fileName)
{
_fileName = string.Format(_fileName, fileName);
}
public List<T> Load()
{
if (!File.Exists(_fileName))
{
FileStream fs = File.Create(_fileName);
fs.Close();
}

IEnumerable<string> lines = File.ReadLines(_fileName);
List<T> objects = _serializer.FromCSV(lines);

return objects;
}

public void Save(List<T> objects)
{
string serializedObjects = _serializer.ToCSV(objects);
using (StreamWriter streamWriter = new StreamWriter(_fileName))
{
streamWriter.Write(serializedObjects);
}
}
}
}

0 comments on commit bac44fa

Please sign in to comment.