Refactor command message deletion
This commit is contained in:
		| @@ -1,17 +1,10 @@ | ||||
| -- SCHEMA VERSION 11: (insert date) -- | ||||
| -- SCHEMA VERSION 11: 2020-10-23  -- | ||||
| -- Create command message table -- | ||||
|  | ||||
| create table command_message | ||||
| create table command_messages | ||||
| ( | ||||
| 	message_id bigint primary key, | ||||
| 	author_id bigint not null, | ||||
| 	timestamp timestamp not null default now() | ||||
| 	message_id bigint primary key not null, | ||||
| 	author_id bigint not null | ||||
| ); | ||||
|  | ||||
| create function cleanup_command_message() returns void as $$ | ||||
| begin | ||||
|     delete from command_message where timestamp < now() - interval '2 hours'; | ||||
| end; | ||||
| $$ language plpgsql; | ||||
|  | ||||
| update info set schema_version = 11; | ||||
|   | ||||
| @@ -1,5 +1,3 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Data; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| using Dapper; | ||||
| @@ -8,17 +6,22 @@ namespace PluralKit.Core | ||||
| { | ||||
|     public partial class ModelRepository | ||||
| 	{ | ||||
| 		public Task SaveCommandMessage(IPKConnection conn, ulong message_id, ulong author_id) => | ||||
| 			conn.QueryAsync("insert into command_message (message_id, author_id) values (@Message, @Author)", | ||||
| 				new {Message = message_id, Author = author_id }); | ||||
| 		public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) => | ||||
| 			conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)", | ||||
| 				new {Message = messageId, Author = authorId }); | ||||
|  | ||||
| 		public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong message_id) => | ||||
| 			conn.QuerySingleOrDefaultAsync<CommandMessage>("select message_id, author_id from command_message where message_id = @Message",  | ||||
| 				new {Message = message_id}); | ||||
| 	} | ||||
| 		public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) => | ||||
| 			conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",  | ||||
| 				new {Message = messageId}); | ||||
|  | ||||
|         public Task<int> DeleteCommandMessagesBefore(IPKConnection conn, ulong messageIdThreshold) => | ||||
|             conn.ExecuteAsync("delete from command_messages where message_id < @Threshold", | ||||
|                 new {Threshold = messageIdThreshold}); | ||||
|     } | ||||
|  | ||||
| 	public class CommandMessage | ||||
| 	{ | ||||
| 		public ulong author_id { get; set; } | ||||
|         public ulong AuthorId { get; set; } | ||||
|         public ulong MessageId { get; set; } | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user