public async Task <IEnumerable <Post> > GetPostsAsync(int threadId) { List <Post> posts = new List <Post>(); using (var cmd = _repo.CreateCommand()) { cmd.CommandText = $"SELECT Id,Text,ThreadId,UserId,UserName FROM Posts Where ThreadId={threadId}"; using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { int ID = (int)reader["Id"]; posts.Add(new Post() { Id = ID, Text = reader["Text"].ToString(), ThreadId = int.Parse(reader["ThreadId"].ToString()), UserId = Guid.Parse(reader["UserId"].ToString()), UserName = reader["UserName"].ToString() }); } } } return(posts); }
public async Task <IEnumerable <Thread> > GetThreadsAsync(int topicId) { List <Thread> threads = new List <Thread>(); using (var cmd = _repo.CreateCommand()) { cmd.CommandText = $"SELECT Id,[Name],TextDescription,TopicId,CreatedAt,UserId,UserName FROM Threads Where TopicId={topicId}"; using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { int ID = (int)reader["Id"]; threads.Add(new Thread() { Id = ID, Name = reader["Name"].ToString(), CreatedAt = DateTime.Parse(reader["CreatedAt"].ToString()), TextDescription = reader["TextDescription"].ToString(), TopicId = Int32.Parse(reader["TopicId"].ToString()), UserId = Guid.Parse(reader["UserId"].ToString()), UserName = reader["UserName"].ToString() }); } } } foreach (var thread in threads) { thread.ThreadPosts = await GetThreadPostsAsync(thread.Id); } return(threads); }
public async Task <IEnumerable <Topic> > GetTopics() { List <Topic> topics = new List <Topic>(); using (var cmd = _repo.CreateCommand()) { cmd.CommandText = "SELECT Id,Name FROM Topics"; using (var reader = await cmd.ExecuteReaderAsync()) { while (reader.Read()) { int ID = (int)reader["Id"]; topics.Add(new Topic() { Id = ID, Name = (string)reader["Name"], }); } } foreach (var topic in topics) { topic.TopicThreads = await GetTopicThreads(topic.Id); } } return(topics); }