Skip to content

Output formand and unwraps #761

Answered by fabioluciano
fabioluciano asked this question in Q&A
Discussion options

You must be logged in to vote

Finally i figured out a solution! I dont know if is the best one, but worked. Maybe in the future, i'll find a better one heheheh

Firts, define a struct like this

#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
struct UrlTagResponse {
    #[serde(flatten)]
    url: entity::url::Model,
    tags: Vec<entity::tag::Model>,
}

And a method like this

#[get("/url/<id_url>")]
async fn get_url_by_id(id_url: i32) -> Value {
    let connection = get_connection().await;
    let model = entity::url::Entity::find_by_id(id_url)
        .find_with_related(entity::tag::Entity)
        .all(&connection)
        .await
        .unwrap_or_default()[0]
        .to_owned();

    json!(UrlTagResponse 

Replies: 3 comments 8 replies

Comment options

You must be logged in to vote
2 replies
@fabioluciano
Comment options

@ikrivosheev
Comment options

Comment options

You must be logged in to vote
5 replies
@ikrivosheev
Comment options

@fabioluciano
Comment options

@ikrivosheev
Comment options

@billy1624
Comment options

@fabioluciano
Comment options

Comment options

You must be logged in to vote
1 reply
@billy1624
Comment options

Answer selected by ikrivosheev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants