You've already forked AstralRinth
forked from didirus/AstralRinth
Switch to Postgres (#39)
* WIP Switch to Postgres * feat(postgres): more work on porting to postgres, now compiles * feat(docker-compose): Changed the docker-compose.yml file to use postgres. * Update docker, documentation, gh actions... * Remove bson dependency * Remove bson import * feat: move mock filehost to trait rather than cargo feature * feat(postgres): transactions for mod creation, multipart refactor * fix: Add Cargo.lock so that sqlx functions * Update sqlx offline build data * fix: Use SQLX_OFFLINE to force sqlx into offline mode for CI * Default release channels * feat(postgres): refactor database models to fit postgres models * fix: Fix sqlx prepare, fix double allocation in indexing * Add dockerfile (#40) Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com> Co-authored-by: Aeledfyr <aeledfyr@gmail.com> Co-authored-by: redblueflame <contact@redblueflame.com> Co-authored-by: Jai A <jai.a@tuta.io> Co-authored-by: Valentin Ricard <redblueflame1@gmail.Com> Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com>
This commit is contained in:
@@ -1,36 +1,108 @@
|
||||
use crate::database::models::team_item::Team;
|
||||
use crate::database::models::Item;
|
||||
use crate::database::Result;
|
||||
use bson::{Bson, Document};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use super::ids::*;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Mod {
|
||||
/// The ID for the mod, must be serializable to base62
|
||||
pub id: i32,
|
||||
//Todo: Move to own table
|
||||
/// The team that owns the mod
|
||||
pub team: Team,
|
||||
pub struct ModBuilder {
|
||||
pub mod_id: ModId,
|
||||
pub team_id: TeamId,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub body_url: String,
|
||||
pub published: String,
|
||||
pub icon_url: Option<String>,
|
||||
pub issues_url: Option<String>,
|
||||
pub source_url: Option<String>,
|
||||
pub wiki_url: Option<String>,
|
||||
pub categories: Vec<CategoryId>,
|
||||
pub initial_versions: Vec<super::version_item::VersionBuilder>,
|
||||
}
|
||||
|
||||
impl ModBuilder {
|
||||
pub async fn insert(
|
||||
self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<ModId, super::DatabaseError> {
|
||||
let mod_struct = Mod {
|
||||
id: self.mod_id,
|
||||
team_id: self.team_id,
|
||||
title: self.title,
|
||||
description: self.description,
|
||||
body_url: self.body_url,
|
||||
published: chrono::Utc::now(),
|
||||
downloads: 0,
|
||||
icon_url: self.icon_url,
|
||||
issues_url: self.issues_url,
|
||||
source_url: self.source_url,
|
||||
wiki_url: self.wiki_url,
|
||||
};
|
||||
mod_struct.insert(&mut *transaction).await?;
|
||||
|
||||
for mut version in self.initial_versions {
|
||||
version.mod_id = self.mod_id;
|
||||
version.insert(&mut *transaction).await?;
|
||||
}
|
||||
|
||||
for category in self.categories {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO mod_categories (joining_mod_id, joining_category_id)
|
||||
VALUES ($1, $2)
|
||||
",
|
||||
)
|
||||
.bind(self.mod_id)
|
||||
.bind(category)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(self.mod_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Mod {
|
||||
pub id: ModId,
|
||||
pub team_id: TeamId,
|
||||
pub title: String,
|
||||
pub description: String,
|
||||
pub body_url: String,
|
||||
pub published: chrono::DateTime<chrono::Utc>,
|
||||
pub downloads: i32,
|
||||
pub categories: Vec<String>,
|
||||
///A vector of Version IDs specifying the mod version of a dependency
|
||||
pub version_ids: Vec<i32>,
|
||||
pub icon_url: Option<String>,
|
||||
pub issues_url: Option<String>,
|
||||
pub source_url: Option<String>,
|
||||
pub wiki_url: Option<String>,
|
||||
}
|
||||
impl Item for Mod {
|
||||
fn get_collection() -> &'static str {
|
||||
"mods"
|
||||
}
|
||||
|
||||
fn from_doc(elem: Document) -> Result<Box<Mod>> {
|
||||
let result: Mod = bson::from_bson(Bson::from(elem))?;
|
||||
Ok(Box::from(result))
|
||||
impl Mod {
|
||||
pub async fn insert(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), sqlx::error::Error> {
|
||||
sqlx::query(
|
||||
"
|
||||
INSERT INTO mods (
|
||||
id, team_id, title, description, body_url,
|
||||
published, downloads, icon_url, issues_url,
|
||||
source_url, wiki_url
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5,
|
||||
$6, $7, $8, $9,
|
||||
$10, $11
|
||||
)
|
||||
",
|
||||
)
|
||||
.bind(self.id)
|
||||
.bind(self.team_id)
|
||||
.bind(&self.title)
|
||||
.bind(&self.description)
|
||||
.bind(&self.body_url)
|
||||
.bind(self.published)
|
||||
.bind(self.downloads)
|
||||
.bind(self.icon_url.as_ref())
|
||||
.bind(self.issues_url.as_ref())
|
||||
.bind(self.source_url.as_ref())
|
||||
.bind(self.wiki_url.as_ref())
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user