-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTUFF
More file actions
105 lines (93 loc) · 2.85 KB
/
Copy pathSTUFF
File metadata and controls
105 lines (93 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
WOOT:
- How to handle this case (different relationship resource type/name (author != people)):
POST /articles/1/links/author
{
"people": "12"
}
=> Resource type and name are two different things !
- How to handle HABTM vs regular to_many ?
WARNING: Ruby code for illustration purposes, NOT CORRECTNESS
CREATE RESOURCE:
POST /albums
{
albums: {
title: "Animals"
}
}
Album.new(title: 'Animals').save!
INSERT INTO `albums` (`title`) VALUES ('Animals');
DELETE RESOURCE:
DELETE /albums/1
album = Album.find(1)
album.destroy
DELETE FROM `album` WHERE `album`.`id` = 1
GET TO_ONE RELATIONSHIP:
GET /albums/1/links/band
album = Album.find(1)
album.band
SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 1 LIMIT 1;
SELECT `bands`.* FROM `bands` WHERE `bands`.`id` = #{album.band_id} LIMIT 1;
CREATE/REPLACE TO_ONE RELATIONSHIP:
POST /albums/1/links/band
{
bands: "2"
}
album = Album.find(1)
album.band = Band.find(2)
UPDATE `albums` SET `band_id` = 2 WHERE `albums`.`id` = 1
REPLACE TO_ONE RELATIONSHIP (equivalent to ADD/REPLACE above):
PUT /albums/1
{
"articles": {
"can_update_other_properties_too": true,
"links": {
"band": "42"
}
}
}
album = Album.find(1)
album.update_attributes(attr)
album.band = Band.find(42)
UPDATE `albums` SET `attr` = attr WHERE `albums`.`id` = 1
UPDATE `albums` SET `band_id` = 42 WHERE `albums`.`id` = 1
DELETE TO_ONE RELATIONSHIP:
DELETE /albums/1/links/band
album = Album.find(1)
album.band = nil (.clear instead ?)
UPDATE `albums` SET `band_id` = NULL WHERE `albums`.`id` = 1
GET TO_MANY RELATIONSHIP:
GET /albums/1/links/tracks
album = Album.find(1)
album.tracks
SELECT `tracks`.* FROM `tracks` WHERE `tracks`.`album_id` = 1;
CREATE/ADD TO_MANY RELATIONSHIPS:
POST /album/1/links/tracks
{
tracks: ['1', '2']
}
album = Album.find(1)
album.tracks << tracks
UPDATE `tracks` SET `album_id` = 1 WHERE `tracks`.`id` = 1
UPDATE `tracks` SET `album_id` = 1 WHERE `tracks`.`id` = 2
REPLACE TO_MANY RELATIONSHIP (equivalent to ADD/REPLACE above):
PUT /albums/1
{
"articles": {
"can_update_other_properties_too": true,
"links": {
// This can be an empty array (remove all)
"tracks": ["3", "5"]
}
}
}
album = Album.find(1)
album.update_attributes(attr)
album.tracks = tracks
UPDATE `tracks` SET `tracks`.`album_id` = NULL WHERE `tracks`.`album_id` = 1 AND `tracks`.`id` IN (#{previous_track_ids})
UPDATE `tracks` SET `album_id` = 1 WHERE `tracks`.`id` = 3
UPDATE `tracks` SET `album_id` = 1 WHERE `tracks`.`id` = 5
DELETE TO_MANY RELATIONSHIPS:
DELETE /albums/1/links/tracks/3,4
album = Album.find(1)
album.tracks.where(id: [3, 4]).destroy_all
UPDATE `blog_post_pictures` SET `blog_post_pictures`.`post_id` = NULL WHERE `blog_post_pictures`.`id` IN (3, 4)