// (C) Copyright 2019 C-xC-c // This file is part of BantFlags. // BantFlags is licensed under the GNU AGPL Version 3.0 or later. // see the LICENSE file or using System.Collections.Generic; using System.Data; using System.Linq; using System.Threading.Tasks; namespace BantFlags.Data.Database { public partial class DatabaseService { // Maybe this could be better but I don't know SQL lol private readonly string GetPostsQuery = @"SELECT posts.post_nr, flags.flag FROM flags LEFT JOIN (postflags) ON (postflags.flag = flags.id) LEFT JOIN (posts) ON (postflags.post_nr = posts.id) WHERE FIND_IN_SET(posts.post_nr, (@posts)) AND posts.board = @board"; /// /// Returns the post numbers and their flags from the post numbers in the input. /// /// List of post numbers on the page. public async Task>> GetPosts(string post_nr, string board) { using var rentedConnection = await ConnectionPool.RentConnectionAsync(); DataTable table = await rentedConnection.Object.CreateQuery(GetPostsQuery) .SetParam("@posts", post_nr) .SetParam("@board", board) .ExecuteTableAsync(); return table.AsEnumerable() .GroupBy(x => x.GetValue("post_nr")); } public async Task>> GetPosts_V1(string post_nr, string board) { List> posts = new List>(); var x = await GetPosts(post_nr, board); x.ForEach(x => posts.Add(new Dictionary { {"post_nr", x.Key.ToString() }, {"region", string.Join("||", x.AsEnumerable().Select(y => y.GetValue("flag")))} })); return posts; } public async Task>> GetPosts_V2(string post_nr, string board) { var posts = await GetPosts(post_nr, board); return posts .ToDictionary( x => x.Key, x => x.AsEnumerable().Select(x => x.GetValue("flag")) ); } } }