Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

44 build media as a generic with union type #57

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion migrations/2022-04-16-153823_create_media/down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
-- This file should undo anything in `up.sql`
DROP TABLE "media";
DROP TABLE "media";
DROP TABLE "file";
DROP TYPE "media_type";
43 changes: 41 additions & 2 deletions migrations/2022-04-16-153823_create_media/up.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
-- Your SQL goes here-- Your SQL goes here
CREATE TYPE media_type AS ENUM ('Default', 'Audio', 'Video','Pdf','Epub','Image');

CREATE TABLE IF NOT EXISTS "file" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL,
"absolute_path" TEXT NOT NULL,
"uploaded_by" uuid NOT NULL,
"checksum" TEXT NOT NULL,
"size" INT NOT NULL DEFAULT 0,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS "media" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL,
"title" TEXT NOT NULL,
"description" TEXT,
"absolute_path" TEXT NOT NULL,
"price" INT NOT NULL DEFAULT 0,
"published" boolean NOT NULL DEFAULT false,
"file_uuid" uuid UNIQUE NOT NULL,
"type" media_type NOT NULL DEFAULT 'Default',
"metadata" uuid UNIQUE NOT NULL,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
"updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT media FOREIGN KEY("file_uuid") REFERENCES "file"(uuid)
);

CREATE TABLE IF NOT EXISTS "audio_metadata" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL,
"codec" TEXT DEFAULT NULL,
"length" TEXT DEFAULT NULL,
"artist" TEXT DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS "video_metadata" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL,
"codec" TEXT DEFAULT NULL,
"length" TEXT DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS "image_metadata" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL
);

CREATE TABLE IF NOT EXISTS "epub_metadata" (
"uuid" uuid UNIQUE PRIMARY KEY NOT NULL
);



12 changes: 12 additions & 0 deletions src/db/media_type_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use diesel_derive_enum::DbEnum;

// define your enum
#[derive(Debug, DbEnum, PartialEq)]
pub enum MediaTypeEnum {
Default,
Audio,
Video,
Pdf,
Epub,
Image,
}
1 change: 1 addition & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rocket_sync_db_pools::{database, diesel};
use self::models::api_payment::ApiPayment;

pub mod igniter;
pub mod media_type_enum;
pub mod models;
pub mod schema;

Expand Down
51 changes: 51 additions & 0 deletions src/db/models/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pub use crate::db::schema::file;

use chrono::NaiveDateTime;
use diesel;
use diesel::prelude::*;
use diesel::PgConnection;

#[derive(Identifiable, Queryable, PartialEq, Debug)]
#[primary_key(uuid)]
#[table_name = "file"]
pub struct File {
pub uuid: uuid::Uuid,
pub absolute_path: String,
pub uploaded_by: uuid::Uuid,
pub checksum: String,
pub size: i32,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

#[derive(Debug, Insertable)]
#[table_name = "file"]
pub struct NewFile {
pub absolute_path: String,
pub uploaded_by: uuid::Uuid,
pub checksum: String,
pub size: i32,
}

impl File {
pub fn create(new_file: NewFile, connection: &PgConnection) -> QueryResult<File> {
use crate::db::schema::file::dsl::*;

diesel::insert_into::<file>(file)
.values(&new_file)
.get_result(connection)
}

pub fn delete(uuid: uuid::Uuid) -> () {}

pub fn find_one_by_uuid(
file_uuid: uuid::Uuid,
connection: &PgConnection,
) -> QueryResult<Option<File>> {
use crate::db::schema::file::dsl::*;

file.filter(uuid.eq(file_uuid))
.first::<File>(connection)
.optional()
}
}
73 changes: 68 additions & 5 deletions src/db/models/media.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use super::file::File;
use crate::db::media_type_enum::MediaTypeEnum;
use crate::db::media_type_enum::MediaTypeEnumMapping;
pub use crate::db::schema::media;

use crate::graphql::types::input::file::FileInput;
use chrono::NaiveDateTime;
use diesel;
use diesel::prelude::*;
use diesel::PgConnection;
use std::path::PathBuf;
use uuid::Uuid;

#[derive(Identifiable, Queryable, PartialEq, Debug)]
#[primary_key(uuid)]
Expand All @@ -14,20 +17,53 @@ pub struct Media {
pub uuid: uuid::Uuid,
pub title: String,
pub description: Option<String>,
pub absolute_path: String,
pub price: i32,
pub published: bool,
pub file_uuid: uuid::Uuid,
pub type_: MediaTypeEnum,
pub metadata: uuid::Uuid,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

#[derive(Identifiable, Queryable, PartialEq, Debug)]
#[primary_key(uuid)]
#[table_name = "media"]
pub struct MediaDbModel {
pub uuid: uuid::Uuid,
pub title: String,
pub description: Option<String>,
pub price: i32,
pub published: bool,
pub file_uuid: uuid::Uuid,
pub type_: MediaTypeEnum,
pub metadata: uuid::Uuid,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}

#[derive(Debug, Insertable)]
#[derive(Debug, Insertable, Associations)]
#[table_name = "media"]
#[belongs_to(parent = File, foreign_key = "file_uuid")]
// #[belongs_to(parent = Metadata, foreign_key = "file_uuid")]
pub struct NewMedia {
pub uuid: uuid::Uuid,
pub title: String,
pub description: Option<String>,
pub absolute_path: String,
pub published: bool,
pub price: i32,
pub file_uuid: uuid::Uuid,
pub type_: MediaTypeEnum,
pub metadata: uuid::Uuid,
}

#[derive(Debug, Queryable, Identifiable, PartialEq, AsChangeset)]
#[primary_key(uuid)]
#[table_name = "media"]
pub struct EditMedia {
pub uuid: uuid::Uuid,
pub title: String,
pub description: Option<String>,
pub published: bool,
pub price: i32,
}
Expand All @@ -38,13 +74,21 @@ impl From<(&PathBuf, FileInput)> for NewMedia {
uuid: uuid::Uuid::new_v4(),
title: file_data.1.title,
description: file_data.1.description,
absolute_path: file_data.0.to_string_lossy().to_owned().to_string(),
price: file_data.1.price,
published: file_data.1.published,
file_uuid: uuid::Uuid::new_v4(),
type_: MediaTypeEnum::Default,
metadata: uuid::Uuid::new_v4(),
}
}
}

// impl From<MediaDbModel> for Media {
// fn from(media: MediaDbModel) -> Self {
// Self { uuid: (), title: (), description: (), price: (), published: (), file_uuid: (), type_: (), metadata: (), created_at: (), updated_at: () }
// }
// }

impl Media {
pub fn create(new_media: NewMedia, connection: &PgConnection) -> QueryResult<Media> {
use crate::db::schema::media::dsl::*;
Expand All @@ -54,11 +98,30 @@ impl Media {
.get_result(connection)
}

pub fn edit(edit_media: EditMedia, connection: &PgConnection) -> QueryResult<Media> {
use crate::db::schema::media::dsl::*;
diesel::update(media)
.set(&edit_media)
.get_result(connection)
}

pub fn delete() -> () {
use crate::db::schema::media::dsl::*;

// diesel::delete<media>(media.filter(id))
()
}

pub fn find_all_published(connection: &PgConnection) -> Vec<Media> {
use crate::db::schema::media::dsl::*;
media.filter(published.eq(true)).load(connection).unwrap()
}

pub fn find_all(connection: &PgConnection) -> Vec<Media> {
use crate::db::schema::media::dsl::*;
media.filter(published.eq(true)).load(connection).unwrap()
}

pub fn find_one_by_uuid(
media_uuid: uuid::Uuid,
connection: &PgConnection,
Expand Down
7 changes: 7 additions & 0 deletions src/db/models/medias/audio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Debug, Clone)]
pub struct Audio{
length: i64,
codec: String,
cover: String,

}
Empty file added src/db/models/medias/image.rs
Empty file.
7 changes: 7 additions & 0 deletions src/db/models/medias/video.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[derive(Debug, Clone)]
pub struct Video{
length: i64,
codec: String,
cover: String,

}
Empty file added src/db/models/metadata.rs
Empty file.
1 change: 1 addition & 0 deletions src/db/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod api_payment;
pub mod file;
pub mod media;
pub mod media_payment;
pub mod session;
Expand Down
63 changes: 61 additions & 2 deletions src/db/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,52 @@ table! {
}

table! {
audio_metadata (uuid) {
uuid -> Uuid,
codec -> Nullable<Text>,
length -> Nullable<Text>,
artist -> Nullable<Text>,
}
}

table! {
epub_metadata (uuid) {
uuid -> Uuid,
}
}

table! {
file (uuid) {
uuid -> Uuid,
absolute_path -> Text,
uploaded_by -> Uuid,
checksum -> Text,
size -> Int4,
created_at -> Timestamptz,
updated_at -> Timestamptz,
}
}

table! {
image_metadata (uuid) {
uuid -> Uuid,
}
}

table! {
use diesel::sql_types::*;
use crate::db::media_type_enum::MediaTypeEnumMapping;

media (uuid) {
uuid -> Uuid,
title -> Text,
description -> Nullable<Text>,
absolute_path -> Text,
price -> Int4,
published -> Bool,
file_uuid -> Uuid,
#[sql_name = "type"]
type_ -> MediaTypeEnumMapping,
metadata -> Uuid,
created_at -> Timestamptz,
updated_at -> Timestamptz,
}
Expand Down Expand Up @@ -53,7 +92,27 @@ table! {
}
}

table! {
video_metadata (uuid) {
uuid -> Uuid,
codec -> Nullable<Text>,
length -> Nullable<Text>,
}
}

joinable!(media -> file (file_uuid));
joinable!(media_payment -> media (media_uuid));
joinable!(session -> user (user_uuid));

allow_tables_to_appear_in_same_query!(api_payment, media, media_payment, session, user,);
allow_tables_to_appear_in_same_query!(
api_payment,
audio_metadata,
epub_metadata,
file,
image_metadata,
media,
media_payment,
session,
user,
video_metadata,
);
Loading