Output formand and unwraps #761
-
I have the follow entities configured im my project And i'm using rocket to create a api to get the results from the database. From a specific scenario i want to get a URL and the related associated tags. I create the following code: #[get("/url/<id_url>")]
async fn get_url_by_id(id_url: i32) -> Json<(JsonValue, std::option::Option<JsonValue>)> {
let connection = get_connection().await;
Json(
Url::find_by_id(id_url)
.find_with_related(Tag)
.into_json()
.one(&connection)
.await
.unwrap()
.unwrap(),
)
} The code works, but i have some issues to address...
[
{
"id":364,
"url":"http://www.arewewebyet.org/"
},
{
"id":5,
"tag":"rust"
}
] How can i return something like... {
"id":364,
"url":"http://www.arewewebyet.org/",
"tags":{
"id":5,
"tag":"rust"
}
}
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
@fabioluciano I see Many-to-Many relation. Did you mean: {
"id":364,
"url":"http://www.arewewebyet.org/",
"tags": [
{
"id":5,
"tag":"rust"
},
{
"id": 5,
"tag": "rust"
}
]
} |
Beta Was this translation helpful? Give feedback.
-
I tried to do what you said, but i think that i need to study rust a little more hehehe... #[macro_use]
extern crate rocket;
use common::database::get_connection;
use rocket::serde::{json::Json, Deserialize};
use sea_orm::EntityTrait;
use sea_orm::JsonValue;
use entity::tag::Entity as Tag;
use entity::url::Entity as Url;
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
struct UrlTagResponse {
url: Url,
tags: Vec<Tag>,
}
#[get("/url")]
async fn get_all_urls() -> Json<Vec<JsonValue>> {
let connection = get_connection().await;
Json(Url::find().into_json().all(&connection).await.unwrap())
}
#[get("/url/<id_url>")]
async fn get_url_by_id(id_url: i32) -> Json<(JsonValue, std::option::Option<JsonValue>)> {
let connection = get_connection().await;
Json(
Url::find_by_id(id_url)
.find_with_related(Tag)
.into_json()
.one(&connection)
.await
.unwrap()
.unwrap(),
)
}
#[get("/url/<id_url>/tag")]
async fn get_url_tags_by_id(id_url: i32) {
let connection = get_connection().await;
let url = Url::find_by_id(id_url)
.find_with_related(Tag)
.into_json()
.one(&connection)
.await;
}
#[launch]
fn rocket() -> _ {
rocket::build().mount(
"/",
routes![get_all_urls, get_url_by_id, get_url_tags_by_id],
)
} My entities are setted like this use rocket::serde::{Deserialize, Serialize};
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
#[sea_orm(table_name = "urls")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub url: String,
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl Related<super::tag::Entity> for Entity {
fn to() -> RelationDef {
super::url_tag::Relation::Tag.def()
}
fn via() -> Option<RelationDef> {
Some(super::url_tag::Relation::Url.def().rev())
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {} I got some errors, and i read that i need to use the same serde from rocket, so i added use rocket::serde::{Deserialize, Serialize}; and #[serde(crate = "rocket::serde")] as mentioned here but i still getting some errors
|
Beta Was this translation helpful? Give feedback.
-
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 {
url: model.0,
tags: model.1,
})
} Will produce a output like this: {
"id":368,
"tags":[
{
"id":6,
"tag":"ebpf"
},
{
"id":7,
"tag":"observability"
},
{
"id":8,
"tag":"tracing"
}
],
"url":"https://ebpf.io/"
} |
Beta Was this translation helpful? Give feedback.
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
And a method like this