From 4b4889d5f21ce457a8cdf537d168d595cbf56595 Mon Sep 17 00:00:00 2001 From: Jai A Date: Fri, 15 May 2020 22:00:47 -0700 Subject: [PATCH] Finish database code --- Cargo.lock | 3 ++ Cargo.toml | 3 +- .../2020-05-15-055207_create_versions/up.sql | 2 +- src/database/database.rs | 2 +- src/database/models.rs | 10 +++--- src/main.rs | 4 +-- src/routes/index.rs | 2 +- src/routes/search.rs | 20 ++++++------ src/schema.rs | 4 +-- templates/mod_editor.hbs | 31 +++++++++++++++++++ 10 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 templates/mod_editor.hbs diff --git a/Cargo.lock b/Cargo.lock index 4b1d7e4c3..ed27c3027 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -517,8 +517,10 @@ checksum = "33d7ca63eb2efea87a7f56a283acc49e2ce4b2bd54adf7465dc1d81fef13d8fc" dependencies = [ "bitflags", "byteorder", + "chrono", "diesel_derives", "pq-sys", + "serde_json", ] [[package]] @@ -590,6 +592,7 @@ dependencies = [ "actix-files", "actix-rt", "actix-web", + "chrono", "diesel", "dotenv", "handlebars", diff --git a/Cargo.toml b/Cargo.toml index dcfc13b7f..3ec219407 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,6 @@ serde_derive = "1.0.107" serde = {version="1.0", features=["derive"]} meilisearch-sdk = "0.1.1" actix-files = "0.2.1" -diesel = { version = "1.0.0", features = ["postgres"] } +diesel = { version = "1.4.4", features = ["postgres", "serde_json", "chrono"] } dotenv = "0.9.0" +chrono = "0.4.11" diff --git a/migrations/2020-05-15-055207_create_versions/up.sql b/migrations/2020-05-15-055207_create_versions/up.sql index 5db90b30a..ccd926f56 100644 --- a/migrations/2020-05-15-055207_create_versions/up.sql +++ b/migrations/2020-05-15-055207_create_versions/up.sql @@ -1,5 +1,5 @@ CREATE TABLE versions ( - id SERIAL PRIMARY KEY, + version_id SERIAL PRIMARY KEY, mod_id SERIAL NOT NULL, title VARCHAR NOT NULL, changelog_path VARCHAR NOT NULL, diff --git a/src/database/database.rs b/src/database/database.rs index 6625fcfc3..505d02fbd 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -4,7 +4,7 @@ use dotenv::dotenv; use std::env; pub fn connect() -> PgConnection{ - dotenv.ok(); + dotenv().ok(); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set!"); PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) diff --git a/src/database/models.rs b/src/database/models.rs index 13d1a9f88..6cc48c144 100644 --- a/src/database/models.rs +++ b/src/database/models.rs @@ -1,13 +1,11 @@ -use serde::{Deserialize, Serialize}; - -use crate::schema::mods; +use chrono::NaiveDate; #[derive(Queryable)] pub struct Mod { pub id: i32, pub title: String, pub description: String, - pub published: String, + pub published: NaiveDate, pub author: String, pub downloads: i32, pub categories: Vec, @@ -17,12 +15,12 @@ pub struct Mod { #[derive(Queryable)] pub struct Version { - pub id: i32, + pub version_id: i32, pub mod_id: i32, pub title: String, pub changelog_path: String, pub files_path: Vec, - pub date_published: String, + pub date_published: NaiveDate, pub author: String, pub downloads: i32, pub dependencies: Vec, diff --git a/src/main.rs b/src/main.rs index 4e42ea76d..caeb27cd8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,8 @@ extern crate serde_json; #[macro_use] extern crate diesel; -use actix_web::{web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder, get, post}; +use actix_web::{web, App, HttpServer}; use handlebars::*; -use meilisearch_sdk::{document::*, indexes::*, client::*, search::*}; -use serde::{Serialize, Deserialize}; use actix_files as fs; mod schema; diff --git a/src/routes/index.rs b/src/routes/index.rs index 2b13d0968..0ca4ed937 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,4 +1,4 @@ -use actix_web::{web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder, get, post}; +use actix_web::{web, HttpResponse, get, post}; use handlebars::*; #[get("/")] diff --git a/src/routes/search.rs b/src/routes/search.rs index 495e51c39..9e091f62e 100644 --- a/src/routes/search.rs +++ b/src/routes/search.rs @@ -1,13 +1,12 @@ extern crate diesel; -use actix_web::{web, web::Data, App, HttpRequest, HttpResponse, HttpServer, Responder, get, post}; +use actix_web::{web, web::Data, HttpRequest, HttpResponse, get, post}; use handlebars::*; use meilisearch_sdk::{document::*, indexes::*, client::*, search::*}; use serde::{Serialize, Deserialize}; use crate::database::*; use diesel::prelude::*; -use std::ptr::eq; #[derive(Serialize, Deserialize, Debug)] struct SearchMod { @@ -26,7 +25,7 @@ impl Document for SearchMod { } } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize)] pub struct SearchRequest { q: Option, f: Option, @@ -102,13 +101,13 @@ pub fn index_mods(conn : PgConnection) { let results = mods.load::(&conn).expect("Error loading mods!"); let mut docs_to_add = vec![]; - for result in results { - let versions = versions.filter(mod_id.eq(result.id)).load::(&conn).expect("Error loading versions!"); + for result in results {getting confused + let mod_versions = versions.filter(mod_id.eq(result.id)).load::(&conn).expect("Error loading versions!"); - let mut mod_versions = vec![]; + let mut mod_game_versions = vec![]; - for version in versions { - mod_versions.append(version.game_versions()) + for version in mod_versions { + mod_game_versions.extend(version.game_versions.clone()) } docs_to_add.push(SearchMod { @@ -116,10 +115,9 @@ pub fn index_mods(conn : PgConnection) { title: result.title, description: result.description, keywords: result.categories, - versions: vec![] + versions: mod_game_versions }); } - mods_index.add_documents(docs_to_add, Some("mod_id")); - + mods_index.add_documents(docs_to_add, Some("mod_id")).unwrap(); } \ No newline at end of file diff --git a/src/schema.rs b/src/schema.rs index b74a7ca58..aa0039f25 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -13,8 +13,8 @@ table! { } table! { - versions (id) { - id -> Int4, + versions (version_id) { + version_id -> Int4, mod_id -> Int4, title -> Varchar, changelog_path -> Varchar, diff --git a/templates/mod_editor.hbs b/templates/mod_editor.hbs new file mode 100644 index 000000000..7432f9112 --- /dev/null +++ b/templates/mod_editor.hbs @@ -0,0 +1,31 @@ + + + + + + + + + + + Search + + + + + + + + + +