shared/api/endpoints/
resource.rs

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use crate::{
    api::Method,
    domain::{
        resource::{
            ListLikedPath, ListLikedRequest, ListLikedResponse, ResourceAdminDataUpdatePath,
            ResourceBrowsePath, ResourceBrowseQuery, ResourceBrowseResponse, ResourceClonePath,
            ResourceCountPath, ResourceCountResponse, ResourceCoverPath, ResourceCreatePath,
            ResourceCreateRequest, ResourceDeleteAllPath, ResourceDeletePath, ResourceGetDraftPath,
            ResourceGetLivePath, ResourceId, ResourceLikePath, ResourceLikedPath,
            ResourceLikedResponse, ResourcePublishPath, ResourceResponse, ResourceSearchPath,
            ResourceSearchQuery, ResourceSearchResponse, ResourceUnlikePath,
            ResourceUpdateAdminDataRequest, ResourceUpdateDraftDataPath,
            ResourceUpdateDraftDataRequest, ResourceViewPath,
        },
        CreateResponse,
    },
    error::{EmptyError, MetadataNotFound},
};

use super::ApiEndpoint;

/// Endpoints for resource curation.
pub mod curation;

/// Endpoints for resource reports.
pub mod report;

/// Create a Resource and it's draft and live data copies.
///
/// * New resources are all set to `PrivacyLevel::Unlisted` by default
///
/// # Flow:
/// 1. Create a Resource and its two data copies with [`Create`]
/// 2. Optionally update Resource info such as privacy, author with [`Update`]
/// 3. Make updates to draft data:
///     a. Patch Resource data through [`UpdateDraftData`]
///     b. Modify modules, through [`module::Update`]
/// 4. Finalize draft changes by calling [`Publish`]
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource`
pub struct Create;
impl ApiEndpoint for Create {
    type Path = ResourceCreatePath;
    type Req = ResourceCreateRequest;
    type Res = CreateResponse<ResourceId>;
    type Err = MetadataNotFound;
    const METHOD: Method = Method::Post;
}

/// Get a Resource's live data by ID.
///
/// # Authorization
/// * None
///
/// # Errors
/// * [`NotFound`](http::StatusCode::NOT_FOUND) if the module does not exist, or the parent resource doesn't exist.
pub struct GetLive;
impl ApiEndpoint for GetLive {
    type Path = ResourceGetLivePath;
    type Req = ();
    type Res = ResourceResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// Get a Resource's draft data by ID.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`,, or `ManageSelfResource` for owned Resources
///
/// # Errors
/// * [`Unauthorized`](http::StatusCode::UNAUTHORIZED) if authorization is not valid.
/// * [`NotFound`](http::StatusCode::NOT_FOUND) if the module does not exist, or the parent resource doesn't exist.
pub struct GetDraft;
impl ApiEndpoint for GetDraft {
    type Path = ResourceGetDraftPath;
    type Req = ();
    type Res = ResourceResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// Update the draft data of a Resource.
///
/// Note that a copy of the Resource's draft or live data can not be fetched directly, but only as a part
/// of one of the following routes:
/// * [`GetLive`] fetches live copies
/// * [`Search`]
///
/// See [`ResourceData`](crate::domain::resource::ResourceData) for the over-the-wire representation.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`,, or `ManageSelfResource` for owned Resources
pub struct UpdateDraftData;
impl ApiEndpoint for UpdateDraftData {
    type Path = ResourceUpdateDraftDataPath;
    type Req = ResourceUpdateDraftDataRequest;
    type Res = ();
    type Err = MetadataNotFound;
    const METHOD: Method = Method::Patch;
}

/// Publish a Resource draft to live by copying over the Resource and module data.
///
/// # Authorization
/// * None
pub struct Publish;
impl ApiEndpoint for Publish {
    type Path = ResourcePublishPath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Put;
}

/// Browse Resources. Returns the draft data copies in the response.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource`
pub struct Browse;
impl ApiEndpoint for Browse {
    type Path = ResourceBrowsePath;
    type Req = ResourceBrowseQuery;
    type Res = ResourceBrowseResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// Search for Resources.
///
/// # Authorization
/// * None
pub struct Search;
impl ApiEndpoint for Search {
    type Path = ResourceSearchPath;
    type Req = ResourceSearchQuery;
    type Res = ResourceSearchResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// Clone a Resource. This clones both the draft and live.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource`
///
/// # Errors
/// * [`Unauthorized`](http::StatusCode::UNAUTHORIZED) if authorization is not valid.
/// * [`Forbidden`](http::StatusCode::FORBIDDEN) if the user does not have sufficient permission to perform the action.
/// * ['NotFound'](http::StatusCode::NOT_FOUND) if the resource does not exist.
/// * ['BadRequest'](http::StatusCode::BAD_REQUEST) if the request is malformed or the Resource is a draft.
pub struct Clone;
impl ApiEndpoint for Clone {
    type Path = ResourceClonePath;
    type Req = ();
    type Res = CreateResponse<ResourceId>;
    type Err = EmptyError;
    const METHOD: Method = Method::Post;
}

/// Delete a Resource.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource` for owned Resources
pub struct Delete;
impl ApiEndpoint for Delete {
    type Path = ResourceDeletePath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Delete;
}

/// Delete all resources associated with current user.
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource` for owned Resources
pub struct DeleteAll;
impl ApiEndpoint for DeleteAll {
    type Path = ResourceDeleteAllPath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Delete;
}

/// Indicates that a resource has a cover
///
/// # Authorization
/// * One of `Admin`, `AdminResource`, or `ManageSelfResource` for owned Resources
pub struct Cover;
impl ApiEndpoint for Cover {
    type Path = ResourceCoverPath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Patch;
}

/// Count of all public Resources. See [`PrivacyLevel`](crate::domain::resource::PrivacyLevel).
///
/// # Authorization
/// * None
pub struct Count;
impl ApiEndpoint for Count {
    type Path = ResourceCountPath;
    type Req = ();
    type Res = ResourceCountResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// Like a Resource
///
/// # Authorization
/// * Admin, BasicAuth
pub struct Like;
impl ApiEndpoint for Like {
    type Path = ResourceLikePath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Put;
}

/// Unlike a Resource
///
/// # Authorization
/// * Admin, BasicAuth
pub struct Unlike;
impl ApiEndpoint for Unlike {
    type Path = ResourceUnlikePath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Delete;
}

/// Is a Resource liked by a user
///
/// # Authorization
/// * Admin, BasicAuth
pub struct Liked;
impl ApiEndpoint for Liked {
    type Path = ResourceLikedPath;
    type Req = ();
    type Res = ResourceLikedResponse;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// List user's liked resources.
pub struct ListLiked;
impl ApiEndpoint for ListLiked {
    type Req = ListLikedRequest;
    type Res = ListLikedResponse;
    type Path = ListLikedPath;
    type Err = EmptyError;
    const METHOD: Method = Method::Get;
}

/// View a Resource
///
/// # Authorization
/// * None
pub struct View;
impl ApiEndpoint for View {
    type Path = ResourceViewPath;
    type Req = ();
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Put;
}

/// Update an admin data for a Resource.
///
/// # Authorization
///
/// * Standard + [`UserScope::ManageResource`](crate::domain::user::UserScope)
///
/// # Errors
///
/// * [`Unauthorized`](http::StatusCode::UNAUTHORIZED) if authorization is not valid.
/// * [`Forbidden`](http::StatusCode::FORBIDDEN) if the user does not have sufficient permission to perform the action.
/// * [`BadRequest`](http::StatusCode::BAD_REQUEST) if the request is missing/invalid.
pub struct ResourceAdminDataUpdate;
impl ApiEndpoint for ResourceAdminDataUpdate {
    type Path = ResourceAdminDataUpdatePath;
    type Req = ResourceUpdateAdminDataRequest;
    type Res = ();
    type Err = EmptyError;
    const METHOD: Method = Method::Patch;
}