コード例 #1
0
        public Message GetMessage(Guid messageId)
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "GetMessage";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddWithValue("@MessageId", messageId);
                    var reader = command.ExecuteReader();
                    if (!reader.HasRows)
                        throw new ArgumentException($"Message with Id: {messageId} has not been found");

                    reader.Read();
                    var message = new Message
                    {
                        Id = reader.GetGuid(reader.GetOrdinal("Id")),
                        Date = reader.GetDateTime(reader.GetOrdinal("Date")),
                        IsSelfDestructing = reader.GetBoolean(reader.GetOrdinal("IsSelfDestructing")),
                        Text = reader.GetString(reader.GetOrdinal("Text")),
                        Chat = _chatsRepository.GetChatInfo(reader.GetGuid(reader.GetOrdinal("ChatId"))),
                        User = _usersRepository.GetUser(reader.GetGuid(reader.GetOrdinal("UserId"))),
                        Attachments =
                            reader["File"] == DBNull.Value
                                ? null
                                : new List<byte[]> {reader.GetSqlBinary(reader.GetOrdinal("File")).Value}
                    };

                    while (reader.Read())
                    {
                        if(reader["File"] != DBNull.Value)
                            message.Attachments.Add(reader.GetSqlBinary(reader.GetOrdinal("File")).Value);
                    }

                    return message;
                }
            }
        }