forked from PerpetuumOnline/PerpetuumServer
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathChannelRepository.cs
More file actions
66 lines (56 loc) · 2.64 KB
/
ChannelRepository.cs
File metadata and controls
66 lines (56 loc) · 2.64 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
using Perpetuum.Data;
using System.Collections.Generic;
using System.Linq;
namespace Perpetuum.Services.Channels
{
public class ChannelRepository : IChannelRepository
{
private readonly ChannelLoggerFactory _channelLoggerFactory;
public ChannelRepository(ChannelLoggerFactory channelLoggerFactory)
{
_channelLoggerFactory = channelLoggerFactory;
}
public Channel Insert(Channel channel)
{
const string cmd = "insert into channels (name,type) values (@name,@type);select id from channels where id = scope_identity()";
int id = Db.Query().CommandText(cmd)
.SetParameter("@name", channel.Name)
.SetParameter("@type", (int)channel.Type)
.ExecuteScalar<int>().ThrowIfEqual(0, ErrorCodes.SQLInsertError);
return channel.SetId(id);
}
public void Delete(Channel channel)
{
Db.Query().CommandText("delete channels where id = @channelId")
.SetParameter("@channelId", channel.Id)
.ExecuteNonQuery().ThrowIfZero(ErrorCodes.SQLDeleteError);
}
public void Update(Channel channel)
{
Db.Query().CommandText("update channels set topic = @topic, password = @password, type=@type where id = @channelid")
.SetParameter("@topic", channel.Topic)
.SetParameter("@password", channel.Password)
.SetParameter("@type", channel.Type)
.SetParameter("@channelid", channel.Id)
.ExecuteNonQuery().ThrowIfEqual(0, ErrorCodes.SQLUpdateError);
}
public IEnumerable<Channel> GetAll()
{
return Db.Query().CommandText("select * from channels").Execute().Select(record =>
{
int id = record.GetValue<int>("id");
ChannelType type = (ChannelType)record.GetValue<int>("type");
string name = record.GetValue<string>("name");
string topic = record.GetValue<string>("topic");
string password = record.GetValue<string>("password");
bool isForcedJoin = record.GetValueOrDefault<bool>("isForcedJoin");
string discordIdString = record.GetValueOrDefault<string>("DiscordId");
ulong? discordId = ulong.TryParse(discordIdString, out ulong parsedId)
? parsedId
: (ulong?)null;
IChannelLogger logger = _channelLoggerFactory(name);
return new Channel(id, type, name, topic, password, isForcedJoin, discordId, logger);
}).ToArray();
}
}
}