Skip to content

Commit

Permalink
Allow adding other entities to collections (#401)
Browse files Browse the repository at this point in the history
* feat(backend): add table for entity to collection

* feat(backend): generate entities for new models

* refactor(backend): move common stuff out of dedicated modules

* feat(backend): make fk names better

* feat(backend): migration to drop useless tables

* chore(backend): remove useless associations

* fix(backend): remove useless table code

* feat(backend): mostly migrate to new models

* style(backend): remove useless model variants

* fix(backend): correct usage of entity of collection

* chore(graphql): regenrate types

* chore(frontend): adapt to new gql schema

* fix(backend): skip exercises which we don't have in db

* feat(backend): allow adding other entities to collections

* chore(frontend, graphql): adapt to new gql schema

* feat(backend): allow removing any entity from collection

* chore(frontend): adapt to new gql schema

* refactor(backend): change name of query

* chore(frontend): adapt to new gql schema

* feat(backend): allow getting collections for any entity

* feat(backend): return collections for people

* refactor(graphql): extract common stuff to fragment

* feat(backend): include collections for people

* refactor(backend): rename model to better name

* feat(backend): allow adding exercises to collections

* feat(backend): allow adding/removing exercises to collections

* refactor(backend): change name of mutations

* chore(graphql): adapt to new gql schema

* refactor(backend): extract fn to add entity to collection

* feat(backend): allow custom exercises to collection

* fix(backend): change name of field

* feat(backend): update col_to_entity association

* feat(backend): return entity lot for collection contents

* chore(frontend): adapt to new schema

* feat(frontend): open timer drawer when rest timer enabled

* feat(backend): return all data types for collection contents

* fix(backend): return year for people

* fix(frontend): render correct metadata

* refactor(frontend): extract component to add entity to collection

* feat(frontend): allow adding people to collections

* feat(backend): do not clean up people that have been added to collections

* refactor(frontend): component to display collections

* feat(frontend): display collections for people

* feat(backend): extract fn to get collections for entities

* chore(graphql): use fragments where possible

* feat(backend): resolver to get user's metadata group details

* feat(frontend): UI to add metadata group to collection

* fix(frontend): do not display entire entity lot

* Revert "fix(frontend): do not display entire entity lot"

This reverts commit aa8118e.

* feat(backend): return collections for exercises

* feat(backend): return exercise collections data

* fix(frontend): adapt to new gql schema

* feat(frontend): allow adding exercise to collection

* fix(frontend): do not display useless tabs

* fix(frontend): adapt to new gql schema

* fix(backend): change name of column

* build(backend): bump version
  • Loading branch information
IgnisDa authored Oct 17, 2023
1 parent f36f967 commit 70c3807
Show file tree
Hide file tree
Showing 50 changed files with 1,384 additions and 722 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ryot"
version = "2.23.2"
version = "2.24.0"
edition = "2021"
repository = "https://github.com/IgnisDa/ryot"
license = "GPL-V3"
Expand Down
17 changes: 6 additions & 11 deletions apps/backend/src/entities/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::collection_to_entity::Entity")]
CollectionToEntity,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
Expand All @@ -32,22 +34,15 @@ pub enum Relation {
User,
}

impl Related<super::user::Entity> for Entity {
impl Related<super::collection_to_entity::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
Relation::CollectionToEntity.def()
}
}

impl Related<super::metadata::Entity> for Entity {
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
super::metadata_to_collection::Relation::Metadata.def()
}
fn via() -> Option<RelationDef> {
Some(
super::metadata_to_collection::Relation::Collection
.def()
.rev(),
)
Relation::User.def()
}
}

Expand Down
117 changes: 117 additions & 0 deletions apps/backend/src/entities/collection_to_entity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.3
use async_trait::async_trait;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::utils::associate_user_with_metadata;

use super::prelude::Collection;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "collection_to_entity")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub last_updated_on: DateTimeUtc,
pub collection_id: i32,
pub metadata_id: Option<i32>,
pub person_id: Option<i32>,
pub metadata_group_id: Option<i32>,
pub exercise_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::collection::Entity",
from = "Column::CollectionId",
to = "super::collection::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Collection,
#[sea_orm(
belongs_to = "super::metadata::Entity",
from = "Column::MetadataId",
to = "super::metadata::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Metadata,
#[sea_orm(
belongs_to = "super::metadata_group::Entity",
from = "Column::MetadataGroupId",
to = "super::metadata_group::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
MetadataGroup,
#[sea_orm(
belongs_to = "super::person::Entity",
from = "Column::PersonId",
to = "super::person::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Person,
#[sea_orm(
belongs_to = "super::exercise::Entity",
from = "Column::ExerciseId",
to = "super::exercise::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
Exercise,
}

impl Related<super::collection::Entity> for Entity {
fn to() -> RelationDef {
Relation::Collection.def()
}
}

impl Related<super::metadata::Entity> for Entity {
fn to() -> RelationDef {
Relation::Metadata.def()
}
}

impl Related<super::metadata_group::Entity> for Entity {
fn to() -> RelationDef {
Relation::MetadataGroup.def()
}
}

impl Related<super::person::Entity> for Entity {
fn to() -> RelationDef {
Relation::Person.def()
}
}

impl Related<super::exercise::Entity> for Entity {
fn to() -> RelationDef {
Relation::Exercise.def()
}
}

#[async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn after_save<C>(model: Model, db: &C, insert: bool) -> Result<Model, DbErr>
where
C: ConnectionTrait,
{
if let Some(metadata_id) = model.metadata_id {
if insert {
let collection = Collection::find_by_id(model.collection_id)
.one(db)
.await?
.unwrap();
associate_user_with_metadata(&collection.user_id, &metadata_id, db)
.await
.ok();
}
}
Ok(model)
}
}
8 changes: 8 additions & 0 deletions apps/backend/src/entities/exercise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,18 @@ impl ExerciseListItem {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::collection_to_entity::Entity")]
CollectionToEntity,
#[sea_orm(has_many = "super::user_to_exercise::Entity")]
UserToExercise,
}

impl Related<super::collection_to_entity::Entity> for Entity {
fn to() -> RelationDef {
Relation::CollectionToEntity.def()
}
}

impl Related<super::user_to_exercise::Entity> for Entity {
fn to() -> RelationDef {
Relation::UserToExercise.def()
Expand Down
21 changes: 4 additions & 17 deletions apps/backend/src/entities/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct Model {
pub enum Relation {
#[sea_orm(has_many = "super::calendar_event::Entity")]
CalendarEvent,
#[sea_orm(has_many = "super::metadata_to_collection::Entity")]
MetadataToCollection,
#[sea_orm(has_many = "super::collection_to_entity::Entity")]
CollectionToEntity,
#[sea_orm(has_many = "super::metadata_to_genre::Entity")]
MetadataToGenre,
#[sea_orm(has_many = "super::metadata_to_partial_metadata::Entity")]
Expand All @@ -64,9 +64,9 @@ impl Related<super::calendar_event::Entity> for Entity {
}
}

impl Related<super::metadata_to_collection::Entity> for Entity {
impl Related<super::collection_to_entity::Entity> for Entity {
fn to() -> RelationDef {
Relation::MetadataToCollection.def()
Relation::CollectionToEntity.def()
}
}

Expand Down Expand Up @@ -106,19 +106,6 @@ impl Related<super::user_to_metadata::Entity> for Entity {
}
}

impl Related<super::collection::Entity> for Entity {
fn to() -> RelationDef {
super::metadata_to_collection::Relation::Collection.def()
}
fn via() -> Option<RelationDef> {
Some(
super::metadata_to_collection::Relation::Metadata
.def()
.rev(),
)
}
}

impl Related<super::genre::Entity> for Entity {
fn to() -> RelationDef {
super::metadata_to_genre::Relation::Genre.def()
Expand Down
8 changes: 8 additions & 0 deletions apps/backend/src/entities/metadata_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::collection_to_entity::Entity")]
CollectionToEntity,
#[sea_orm(has_many = "super::partial_metadata_to_metadata_group::Entity")]
PartialMetadataToMetadataGroup,
}

impl Related<super::collection_to_entity::Entity> for Entity {
fn to() -> RelationDef {
Relation::CollectionToEntity.def()
}
}

impl Related<super::partial_metadata_to_metadata_group::Entity> for Entity {
fn to() -> RelationDef {
Relation::PartialMetadataToMetadataGroup.def()
Expand Down
69 changes: 0 additions & 69 deletions apps/backend/src/entities/metadata_to_collection.rs

This file was deleted.

2 changes: 1 addition & 1 deletion apps/backend/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pub mod prelude;

pub mod calendar_event;
pub mod collection;
pub mod collection_to_entity;
pub mod exercise;
pub mod genre;
pub mod import_report;
pub mod metadata;
pub mod metadata_group;
pub mod metadata_to_collection;
pub mod metadata_to_genre;
pub mod metadata_to_partial_metadata;
pub mod metadata_to_person;
Expand Down
8 changes: 8 additions & 0 deletions apps/backend/src/entities/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::collection_to_entity::Entity")]
CollectionToEntity,
#[sea_orm(has_many = "super::metadata_to_person::Entity")]
MetadataToPerson,
#[sea_orm(has_many = "super::person_to_partial_metadata::Entity")]
Expand All @@ -40,6 +42,12 @@ pub enum Relation {
Review,
}

impl Related<super::collection_to_entity::Entity> for Entity {
fn to() -> RelationDef {
Relation::CollectionToEntity.def()
}
}

impl Related<super::metadata_to_person::Entity> for Entity {
fn to() -> RelationDef {
Relation::MetadataToPerson.def()
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/entities/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
pub use super::calendar_event::Entity as CalendarEvent;
pub use super::collection::Entity as Collection;
pub use super::collection_to_entity::Entity as CollectionToEntity;
pub use super::exercise::Entity as Exercise;
pub use super::genre::Entity as Genre;
pub use super::import_report::Entity as ImportReport;
pub use super::metadata::Entity as Metadata;
pub use super::metadata_group::Entity as MetadataGroup;
pub use super::metadata_to_collection::Entity as MetadataToCollection;
pub use super::metadata_to_genre::Entity as MetadataToGenre;
pub use super::metadata_to_partial_metadata::Entity as MetadataToPartialMetadata;
pub use super::metadata_to_person::Entity as MetadataToPerson;
Expand Down
13 changes: 8 additions & 5 deletions apps/backend/src/fitness/logic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;

use anyhow::{anyhow, Result};
use anyhow::Result;
use chrono::Utc;
use rs_utils::LengthVec;
use rust_decimal::{prelude::FromPrimitive, Decimal};
Expand Down Expand Up @@ -91,10 +91,13 @@ impl UserWorkoutInput {
let mut exercises = vec![];
let mut workout_totals = vec![];
for (idx, ex) in self.exercises.into_iter().enumerate() {
let db_ex = Exercise::find_by_id(ex.exercise_id)
.one(db)
.await?
.ok_or_else(|| anyhow!("No exercise found!"))?;
let db_ex = match Exercise::find_by_id(ex.exercise_id).one(db).await? {
None => {
tracing::error!("Exercise with id = {} not found", ex.exercise_id);
continue;
}
Some(e) => e,
};
let mut sets = vec![];
let mut total = WorkoutTotalMeasurement::default();
let association = UserToExercise::find()
Expand Down
Loading

0 comments on commit 70c3807

Please sign in to comment.