about summary refs log tree commit diff
path: root/src/models.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-14T18·28+0200
committerVincent Ambo <github@tazj.in>2018-04-14T20·21+0200
commit31b0a550f2b96a1de5de65308420788c9a6aa5df (patch)
tree27f6396644f753aef6a8ebf48ba0bd6438e71029 /src/models.rs
parent2d8db520107c25c5bc7d1ba861047fa9b7eaea5f (diff)
feat(db): Implement handling of 'SearchPosts' message
Adds support for executing full-text search across a forum instance by
sending the `SearchPosts` message with a search query to the DB actor.

The struct used for results is mapped manually to the expected query
result as the query is embedded via raw SQL.
Diffstat (limited to 'src/models.rs')
-rw-r--r--src/models.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/models.rs b/src/models.rs
index 9d3405e154..927a785136 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -16,6 +16,7 @@
 
 use chrono::prelude::{DateTime, Utc};
 use schema::{threads, posts};
+use diesel::sql_types::{Text, Integer};
 
 #[derive(Identifiable, Queryable, Serialize)]
 pub struct Thread {
@@ -69,3 +70,23 @@ pub struct NewPost {
     pub author_name: String,
     pub author_email: String,
 }
+
+/// This struct models the response of a full-text search query. It
+/// does not use a table/schema definition struct like the other
+/// tables, as no table of this type actually exists.
+#[derive(QueryableByName, Debug)]
+pub struct SearchResult {
+    #[sql_type = "Integer"]
+    pub post_id: i32,
+    #[sql_type = "Integer"]
+    pub thread_id: i32,
+    #[sql_type = "Text"]
+    pub author: String,
+    #[sql_type = "Text"]
+    pub title: String,
+
+    /// Headline represents the result of Postgres' ts_headline()
+    /// function, which highlights search terms in the search results.
+    #[sql_type = "Text"]
+    pub headline: String,
+}