-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVFileGroupsRepository.cs
More file actions
84 lines (70 loc) · 2.61 KB
/
CSVFileGroupsRepository.cs
File metadata and controls
84 lines (70 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace MilkshakeCup
{
using System;
using System.IO;
using System.Linq;
using MilkshakeCup.Models;
public class CSVFileGroupsRepository : IGroupsRepository
{
private const char comma = ',';
private const string csvExtension = ".csv";
private readonly string _folderPath;
public CSVFileGroupsRepository(string folderPath)
{
_folderPath = folderPath;
}
public Group[] Groups() => GroupNames().Select(Group).ToArray();
public Group Group(string groupName)
{
Group group = null;
if (File.Exists(FilePath(groupName)))
{
group = new Group(groupName);
Array.ForEach(File.ReadAllLines(FilePath(groupName)), csv => group.Add(Player(csv)));
}
return group;
}
public void Save(Group group) =>
File.WriteAllLines(FilePath(group.Name), group.Players.Select(CSV).ToArray());
/// <summary>
/// Returns the file names of all .csv files in the _folderPath.
/// They will be used as group names.
/// E.g. for this structure: (_folderPath = "Groups")
/// - Groups
/// - a.csv
/// - b.csv
/// will return "a" and "b" ordered alphabetically.
/// </summary>
private IOrderedEnumerable<string> GroupNames() =>
Directory
.GetFiles(_folderPath, "*" + csvExtension)
.Select(Path.GetFileNameWithoutExtension)
.OrderBy(groupName => groupName);
/// <summary>
/// Returns the FilePath for a group based on its name
/// E.g. "a" will return "Groups/a.csv" (with _folderPath = "Groups")
/// </summary>
private string FilePath(string groupName) =>
Path.Combine(_folderPath, groupName + csvExtension);
private static Player Player(string csv) => Player(csv.Split(comma));
private static Player Player(string[] columns) =>
new Player(
columns[0],
columns[1],
ushort.Parse(columns[2]),
ushort.Parse(columns[3]),
ushort.Parse(columns[4]),
ushort.Parse(columns[5]),
ushort.Parse(columns[6]));
private static string CSV(Player player) =>
string.Join(
comma,
player.Name,
player.Team,
player.Won,
player.Draw,
player.Lost,
player.GoalsInFavor,
player.GoalsAgainst);
}
}