Apologies, if this question is obvious, but I can't seem to find sufficient documentation. I might be lacking knowledge with restful methodologies. How do I store a record with a relationship?
I have a place. I want to store posts of users made to this place. So a place can have many posts. A post belongs to one place.
I'm using Aqueduct 3.0 Pre-Release.
I have following models:
place.dart
class Place extends ManagedObject<_Place> implements _Place {}
class _Place {
@primaryKey
int id;
@Column(unique: true)
String name;
ManagedSet<Post> posts;
}
post.dart
import 'package:places_api/model/place.dart';
import 'package:places_api/places_api.dart';
class Post extends ManagedObject<_Post> implements _Post {}
class _Post {
@primaryKey
int id;
@Column()
String text;
@Relate(#posts)
Place place;
}
I try to save the post, but there is only the possibility to store a place object, and not a place_id. Obviously below post query does not work, as there is only a values.place object, and not a values.place_id property. Is it intended to load the place, and then store all the posts to it?
Also without relationship, I can't store the place_id, as it seems that Aqueduct treats the _ as something special. Can't I use database properties, that have an underscore?
Is there any example that explains this?
@Operation.post()
Future<Response> createPost() async {
final body = request.body.asMap();
final query = new Query<Post>(context)
..values.place_id = body['place_id']
..values.text = body['text'];
final insertedPost = await query.insert();
return new Response.ok(insertedPost);
}
Currently I'm sending following body as POST:
{
"place_id": 1,
"text": "My post here"
}
To following URL: http://localhost:8888/posts
Would it be better to send something like this?
{
"text": "My post here"
}
To URL: http://localhost:8888/place/1/posts
Then fetch the place first, and store the post to it?