-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeamsRepository.cs
102 lines (87 loc) · 2.91 KB
/
TeamsRepository.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using BaBTeams.Model;
using SQLite;
namespace BaBTeams.Data
{
public class TeamsRepository
{
string _dbPath;
SQLiteConnection connection;
public string StatusMessage { get; set; }
// TODO: acceso a DB con sqlite-net-pcl
private void Init()
{
if (connection != null) return;
connection = new SQLiteConnection(_dbPath);
connection.CreateTable<Team>();
if (connection.Table<Team>().ToList().Count == 0)
{
SeedData();
}
}
public TeamsRepository(string dbPath)
{
_dbPath = dbPath;
Init();
}
public List<Team> GetAllTeams()
{
try
{
Init();
return connection.Table<Team>().ToList();
}
catch (Exception ex)
{
StatusMessage = string.Format("Failed to retrieve data. {0}", ex.Message);
}
return new List<Team>();
}
// public void AddNewTeam(string name)
// {
// int result = 0;
// try
// {
// Init();
// if (string.IsNullOrEmpty(name)) throw new Exception("Valid name required");
// result = connection.Insert(new Team { Name = name });
// result = 0;
// StatusMessage = string.Format("{0} record(s) added (Name: {1})", result, name);
// }
// catch (Exception ex)
// {
// StatusMessage = string.Format("Failed to add {0}. Error: {1}", name, ex.Message);
// }
// }
private void SeedData()
{
var list = new List<Team>
{
new Team
{
Name = "Astros de Granada",
Location = "Granada",
Category = "Softball",
FoundationYear = 2004,
Image = "https://th.bing.com/th/id/OIP.FoFYITBIc5lhWHrI1kny4wHaE7?pid=ImgDet&w=2420&h=1613&rs=1"
},
new Team
{
Name = "Kyrios",
Location = "Córdoba",
Category = "Baseball and softball",
FoundationYear = 1990,
Image = "https://th.bing.com/th/id/OIP.FoFYITBIc5lhWHrI1kny4wHaE7?pid=ImgDet&w=2420&h=1613&rs=1"
},
new Team
{
Name = "Orioles de Oviedo",
Location = "Oviedo",
Category = "Baseball and softball",
FoundationYear = 2010,
Image = "https://th.bing.com/th/id/OIP.FoFYITBIc5lhWHrI1kny4wHaE7?pid=ImgDet&w=2420&h=1613&rs=1"
}
};
connection.InsertAll(list);
}
}
}