You've already forked AstralRinth
forked from didirus/AstralRinth
Finish database code
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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<String>,
|
||||
@@ -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<String>,
|
||||
pub date_published: String,
|
||||
pub date_published: NaiveDate,
|
||||
pub author: String,
|
||||
pub downloads: i32,
|
||||
pub dependencies: Vec<String>,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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("/")]
|
||||
|
||||
@@ -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<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 mut docs_to_add = vec![];
|
||||
|
||||
for result in results {
|
||||
let versions = versions.filter(mod_id.eq(result.id)).load::<Version>(&conn).expect("Error loading versions!");
|
||||
for result in results {getting confused
|
||||
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 {
|
||||
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();
|
||||
}
|
||||
@@ -13,8 +13,8 @@ table! {
|
||||
}
|
||||
|
||||
table! {
|
||||
versions (id) {
|
||||
id -> Int4,
|
||||
versions (version_id) {
|
||||
version_id -> Int4,
|
||||
mod_id -> Int4,
|
||||
title -> Varchar,
|
||||
changelog_path -> Varchar,
|
||||
|
||||
Reference in New Issue
Block a user