Finish database code

This commit is contained in:
Jai A
2020-05-15 22:00:47 -07:00
parent fee34ba257
commit 4b4889d5f2
10 changed files with 55 additions and 26 deletions

3
Cargo.lock generated
View File

@@ -517,8 +517,10 @@ checksum = "33d7ca63eb2efea87a7f56a283acc49e2ce4b2bd54adf7465dc1d81fef13d8fc"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"byteorder", "byteorder",
"chrono",
"diesel_derives", "diesel_derives",
"pq-sys", "pq-sys",
"serde_json",
] ]
[[package]] [[package]]
@@ -590,6 +592,7 @@ dependencies = [
"actix-files", "actix-files",
"actix-rt", "actix-rt",
"actix-web", "actix-web",
"chrono",
"diesel", "diesel",
"dotenv", "dotenv",
"handlebars", "handlebars",

View File

@@ -15,5 +15,6 @@ serde_derive = "1.0.107"
serde = {version="1.0", features=["derive"]} serde = {version="1.0", features=["derive"]}
meilisearch-sdk = "0.1.1" meilisearch-sdk = "0.1.1"
actix-files = "0.2.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" dotenv = "0.9.0"
chrono = "0.4.11"

View File

@@ -1,5 +1,5 @@
CREATE TABLE versions ( CREATE TABLE versions (
id SERIAL PRIMARY KEY, version_id SERIAL PRIMARY KEY,
mod_id SERIAL NOT NULL, mod_id SERIAL NOT NULL,
title VARCHAR NOT NULL, title VARCHAR NOT NULL,
changelog_path VARCHAR NOT NULL, changelog_path VARCHAR NOT NULL,

View File

@@ -4,7 +4,7 @@ use dotenv::dotenv;
use std::env; use std::env;
pub fn connect() -> PgConnection{ pub fn connect() -> PgConnection{
dotenv.ok(); dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set!"); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set!");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))

View File

@@ -1,13 +1,11 @@
use serde::{Deserialize, Serialize}; use chrono::NaiveDate;
use crate::schema::mods;
#[derive(Queryable)] #[derive(Queryable)]
pub struct Mod { pub struct Mod {
pub id: i32, pub id: i32,
pub title: String, pub title: String,
pub description: String, pub description: String,
pub published: String, pub published: NaiveDate,
pub author: String, pub author: String,
pub downloads: i32, pub downloads: i32,
pub categories: Vec<String>, pub categories: Vec<String>,
@@ -17,12 +15,12 @@ pub struct Mod {
#[derive(Queryable)] #[derive(Queryable)]
pub struct Version { pub struct Version {
pub id: i32, pub version_id: i32,
pub mod_id: i32, pub mod_id: i32,
pub title: String, pub title: String,
pub changelog_path: String, pub changelog_path: String,
pub files_path: Vec<String>, pub files_path: Vec<String>,
pub date_published: String, pub date_published: NaiveDate,
pub author: String, pub author: String,
pub downloads: i32, pub downloads: i32,
pub dependencies: Vec<String>, pub dependencies: Vec<String>,

View File

@@ -4,10 +4,8 @@ extern crate serde_json;
#[macro_use] #[macro_use]
extern crate diesel; 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 handlebars::*;
use meilisearch_sdk::{document::*, indexes::*, client::*, search::*};
use serde::{Serialize, Deserialize};
use actix_files as fs; use actix_files as fs;
mod schema; mod schema;

View File

@@ -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::*; use handlebars::*;
#[get("/")] #[get("/")]

View File

@@ -1,13 +1,12 @@
extern crate diesel; 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 handlebars::*;
use meilisearch_sdk::{document::*, indexes::*, client::*, search::*}; use meilisearch_sdk::{document::*, indexes::*, client::*, search::*};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::database::*; use crate::database::*;
use diesel::prelude::*; use diesel::prelude::*;
use std::ptr::eq;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct SearchMod { struct SearchMod {
@@ -26,7 +25,7 @@ impl Document for SearchMod {
} }
} }
#[derive(Deserialize)] #[derive(Serialize, Deserialize)]
pub struct SearchRequest { pub struct SearchRequest {
q: Option<String>, q: Option<String>,
f: Option<String>, f: Option<String>,
@@ -102,13 +101,13 @@ pub fn index_mods(conn : PgConnection) {
let results = mods.load::<Mod>(&conn).expect("Error loading mods!"); let results = mods.load::<Mod>(&conn).expect("Error loading mods!");
let mut docs_to_add = vec![]; let mut docs_to_add = vec![];
for result in results { for result in results {getting confused
let versions = versions.filter(mod_id.eq(result.id)).load::<Version>(&conn).expect("Error loading versions!"); let mod_versions = versions.filter(mod_id.eq(result.id)).load::<Version>(&conn).expect("Error loading versions!");
let mut mod_versions = vec![]; let mut mod_game_versions = vec![];
for version in versions { for version in mod_versions {
mod_versions.append(version.game_versions()) mod_game_versions.extend(version.game_versions.clone())
} }
docs_to_add.push(SearchMod { docs_to_add.push(SearchMod {
@@ -116,10 +115,9 @@ pub fn index_mods(conn : PgConnection) {
title: result.title, title: result.title,
description: result.description, description: result.description,
keywords: result.categories, 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();
} }

View File

@@ -13,8 +13,8 @@ table! {
} }
table! { table! {
versions (id) { versions (version_id) {
id -> Int4, version_id -> Int4,
mod_id -> Int4, mod_id -> Int4,
title -> Varchar, title -> Varchar,
changelog_path -> Varchar, changelog_path -> Varchar,

31
templates/mod_editor.hbs Normal file
View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/css/main.css" rel="stylesheet" type="text/css">
<link href="static/css/search.css" rel="stylesheet" type="text/css">
<title>Search</title>
</head>
<body>
<header class="site-header">
<div class="temp-circle-logo"></div>
<h2>Site Name</h2>
<div class="links-container">
<a href="home">home</a>
<a href="mods">mods</a>
<a href="modpacks">modpacks</a>
<a href="about">about</a>
</div>
</header>
</body>
</html>