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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
|
//! API endpoint handlers
use ConnectionPool;
use api_helpers::*;
use chrono;
use database_models::*;
use database_schema::{changelog, container_ident, container_rev, creator_ident, creator_rev,
editgroup, editor, file_ident, file_rev, release_ident, release_rev,
work_ident, work_rev};
use diesel::prelude::*;
use diesel::{self, insert_into};
use errors::*;
use fatcat_api::models;
use fatcat_api::models::*;
use fatcat_api::{Api, ApiError, ContainerIdGetResponse, ContainerLookupGetResponse,
ContainerPostResponse, Context, CreatorIdGetResponse, CreatorLookupGetResponse,
CreatorPostResponse, EditgroupIdAcceptPostResponse, EditgroupIdGetResponse,
EditgroupPostResponse, EditorUsernameChangelogGetResponse,
EditorUsernameGetResponse, FileIdGetResponse, FileLookupGetResponse,
FilePostResponse, ReleaseIdGetResponse, ReleaseLookupGetResponse,
ReleasePostResponse, WorkIdGetResponse, WorkPostResponse};
use futures::{self, Future};
use uuid;
// Helper for calling through to handlers
macro_rules! wrap_get_id_handler {
($get_fn:ident, $handler:ident, $resp:ident, $idtype:ident) => {
fn $get_fn(
&self,
id: $idtype,
_context: &Context,
) -> Box<Future<Item = $resp, Error = ApiError> + Send> {
match self.$handler(id) {
Ok(Some(entity)) =>
Box::new(futures::done(Ok($resp::FoundEntity(entity)))),
Ok(None) =>
Box::new(futures::done(Ok($resp::NotFound(
ErrorResponse { message: "No such entity".to_string() }),
))),
Err(e) =>
// TODO: dig in to error type here
Box::new(futures::done(Ok($resp::BadRequest(
ErrorResponse { message: e.to_string() },
)))),
}
}
}
}
macro_rules! wrap_lookup_handler {
($get_fn:ident, $handler:ident, $resp:ident, $idname:ident, $idtype:ident) => {
fn $get_fn(
&self,
$idname: $idtype,
_context: &Context,
) -> Box<Future<Item = $resp, Error = ApiError> + Send> {
match self.$handler($idname) {
Ok(Some(entity)) =>
Box::new(futures::done(Ok($resp::FoundEntity(entity)))),
Ok(None) =>
Box::new(futures::done(Ok($resp::NotFound(
ErrorResponse { message: "No such entity".to_string() }),
))),
Err(e) =>
// TODO: dig in to error type here
Box::new(futures::done(Ok($resp::BadRequest(
ErrorResponse { message: e.to_string() },
)))),
}
}
}
}
#[derive(Clone)]
pub struct Server {
pub db_pool: ConnectionPool,
}
impl Server {
fn container_id_get_handler(&self, id: String) -> Result<Option<ContainerEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let id = uuid::Uuid::parse_str(&id)?;
let res: ::std::result::Result<(ContainerIdentRow, ContainerRevRow), _> =
container_ident::table
.find(id)
.inner_join(container_rev::table)
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = ContainerEntity {
issn: rev.issn,
publisher: rev.publisher,
name: rev.name,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn container_lookup_get_handler(&self, issn: String) -> Result<Option<ContainerEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let res: ::std::result::Result<(ContainerIdentRow, ContainerRevRow), _> =
container_ident::table
.inner_join(container_rev::table)
.filter(container_rev::issn.eq(&issn))
.filter(container_ident::is_live.eq(true))
.filter(container_ident::redirect_id.is_null())
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = ContainerEntity {
issn: rev.issn,
publisher: rev.publisher,
name: rev.name,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn creator_id_get_handler(&self, id: String) -> Result<Option<CreatorEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let id = uuid::Uuid::parse_str(&id)?;
let res: ::std::result::Result<(CreatorIdentRow, CreatorRevRow), _> = creator_ident::table
.find(id)
.inner_join(creator_rev::table)
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = CreatorEntity {
name: rev.name,
orcid: rev.orcid,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn creator_lookup_get_handler(&self, orcid: String) -> Result<Option<CreatorEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let res: ::std::result::Result<(CreatorIdentRow, CreatorRevRow), _> = creator_ident::table
.inner_join(creator_rev::table)
.filter(creator_rev::orcid.eq(&orcid))
.filter(creator_ident::is_live.eq(true))
.filter(creator_ident::redirect_id.is_null())
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = CreatorEntity {
name: rev.name,
orcid: rev.orcid,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn file_id_get_handler(&self, id: String) -> Result<Option<FileEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let id = uuid::Uuid::parse_str(&id)?;
let res: ::std::result::Result<(FileIdentRow, FileRevRow), _> = file_ident::table
.find(id)
.inner_join(file_rev::table)
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = FileEntity {
sha1: rev.sha1,
size: rev.size.map(|v| v as i64),
url: rev.url,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id.map(|v| v),
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn file_lookup_get_handler(&self, sha1: String) -> Result<Option<FileEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let res: ::std::result::Result<(FileIdentRow, FileRevRow), _> = file_ident::table
.inner_join(file_rev::table)
.filter(file_rev::sha1.eq(&sha1))
.filter(file_ident::is_live.eq(true))
.filter(file_ident::redirect_id.is_null())
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = FileEntity {
sha1: rev.sha1,
size: rev.size.map(|v| v as i64),
url: rev.url,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id.map(|v| v),
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn work_id_get_handler(&self, id: String) -> Result<Option<WorkEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let id = uuid::Uuid::parse_str(&id)?;
let res: ::std::result::Result<(WorkIdentRow, WorkRevRow), _> = work_ident::table
.find(id)
.inner_join(work_rev::table)
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = WorkEntity {
work_type: rev.work_type,
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn release_id_get_handler(&self, id: String) -> Result<Option<ReleaseEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let id = uuid::Uuid::parse_str(&id)?;
let res: ::std::result::Result<(ReleaseIdentRow, ReleaseRevRow), _> = release_ident::table
.find(id)
.inner_join(release_rev::table)
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = ReleaseEntity {
title: rev.title,
release_type: rev.release_type,
//date: rev.date,
doi: rev.doi,
volume: rev.volume,
pages: rev.pages,
issue: rev.issue,
container_id: rev.container_ident_id.map(|u| u.to_string()),
work_id: rev.work_ident_id.to_string(),
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn release_lookup_get_handler(&self, doi: String) -> Result<Option<ReleaseEntity>> {
let conn = self.db_pool.get().expect("db_pool error");
let res: ::std::result::Result<(ReleaseIdentRow, ReleaseRevRow), _> = release_ident::table
.inner_join(release_rev::table)
.filter(release_rev::doi.eq(&doi))
.filter(release_ident::is_live.eq(true))
.filter(release_ident::redirect_id.is_null())
.first(&conn);
let (ident, rev) = match res {
Ok(r) => r,
Err(diesel::result::Error::NotFound) => return Ok(None),
Err(e) => return Err(e.into()),
};
let entity = ReleaseEntity {
title: rev.title,
release_type: rev.release_type,
//date: rev.date,
doi: rev.doi,
volume: rev.volume,
pages: rev.pages,
issue: rev.issue,
container_id: rev.container_ident_id.map(|u| u.to_string()),
work_id: rev.work_ident_id.to_string(),
state: Some(ident.state().unwrap().shortname()),
ident: Some(ident.id.to_string()),
revision: ident.rev_id,
redirect: ident.redirect_id.map(|u| u.to_string()),
editgroup_id: None,
extra: rev.extra_json,
};
Ok(Some(entity))
}
fn editgroup_id_get_handler(&self, id: i64) -> Result<Option<Editgroup>> {
let conn = self.db_pool.get().expect("db_pool error");
let row: EditgroupRow = editgroup::table.find(id as i64).first(&conn)?;
let eg = Editgroup {
id: Some(row.id),
editor_id: row.editor_id,
description: row.description,
extra: row.extra_json,
};
Ok(Some(eg))
}
fn editor_get_handler(&self, username: String) -> Result<Option<Editor>> {
let conn = self.db_pool.get().expect("db_pool error");
let row: EditorRow = editor::table
.filter(editor::username.eq(&username))
.first(&conn)?;
let ed = Editor {
username: row.username,
};
Ok(Some(ed))
}
fn editor_changelog_get_handler(&self, username: String) -> Result<Option<Changelogentries>> {
let conn = self.db_pool.get().expect("db_pool error");
// TODO: single query
let editor: EditorRow = editor::table
.filter(editor::username.eq(username))
.first(&conn)?;
let changes: Vec<(ChangelogRow, EditgroupRow)> = changelog::table
.inner_join(editgroup::table)
.filter(editgroup::editor_id.eq(editor.id))
.load(&conn)?;
let entries = changes
.iter()
.map(|(row, _)| ChangelogentriesInner {
index: row.id,
editgroup_id: row.editgroup_id,
timestamp: chrono::DateTime::from_utc(row.timestamp, chrono::Utc),
})
.collect();
Ok(Some(entries))
}
}
impl Api for Server {
wrap_get_id_handler!(
container_id_get,
container_id_get_handler,
ContainerIdGetResponse,
String
);
wrap_get_id_handler!(
creator_id_get,
creator_id_get_handler,
CreatorIdGetResponse,
String
);
wrap_get_id_handler!(file_id_get, file_id_get_handler, FileIdGetResponse, String);
wrap_get_id_handler!(work_id_get, work_id_get_handler, WorkIdGetResponse, String);
wrap_get_id_handler!(
release_id_get,
release_id_get_handler,
ReleaseIdGetResponse,
String
);
wrap_get_id_handler!(
editgroup_id_get,
editgroup_id_get_handler,
EditgroupIdGetResponse,
i64
);
wrap_lookup_handler!(
container_lookup_get,
container_lookup_get_handler,
ContainerLookupGetResponse,
issn,
String
);
wrap_lookup_handler!(
creator_lookup_get,
creator_lookup_get_handler,
CreatorLookupGetResponse,
orcid,
String
);
wrap_lookup_handler!(
file_lookup_get,
file_lookup_get_handler,
FileLookupGetResponse,
sha1,
String
);
wrap_lookup_handler!(
release_lookup_get,
release_lookup_get_handler,
ReleaseLookupGetResponse,
doi,
String
);
fn container_post(
&self,
body: models::ContainerEntity,
_context: &Context,
) -> Box<Future<Item = ContainerPostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let editor_id = 1; // TODO: auth
let editgroup_id = match body.editgroup_id {
None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
Some(param) => param as i64,
};
let edit: ContainerEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO container_rev (name, publisher, issn)
VALUES ($1, $2, $3)
RETURNING id ),
ident AS ( INSERT INTO container_ident (rev_id)
VALUES ((SELECT rev.id FROM rev))
RETURNING id )
INSERT INTO container_edit (editgroup_id, ident_id, rev_id) VALUES
($4, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
RETURNING *",
).bind::<diesel::sql_types::Text, _>(body.name)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.publisher)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.issn)
.bind::<diesel::sql_types::BigInt, _>(editgroup_id)
.get_result(&conn)
.unwrap();
let edit = &edit;
let entity_edit = EntityEdit {
editgroup_id: Some(edit.editgroup_id),
revision: Some(edit.rev_id.unwrap()),
ident: Some(edit.ident_id.to_string()),
edit_id: Some(edit.id),
extra: edit.extra_json.clone(),
};
Box::new(futures::done(Ok(ContainerPostResponse::CreatedEntity(
entity_edit,
))))
}
fn creator_post(
&self,
body: models::CreatorEntity,
_context: &Context,
) -> Box<Future<Item = CreatorPostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let editor_id = 1; // TODO: auth
let editgroup_id = match body.editgroup_id {
None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
Some(param) => param as i64,
};
let edit: CreatorEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO creator_rev (name, orcid)
VALUES ($1, $2)
RETURNING id ),
ident AS ( INSERT INTO creator_ident (rev_id)
VALUES ((SELECT rev.id FROM rev))
RETURNING id )
INSERT INTO creator_edit (editgroup_id, ident_id, rev_id) VALUES
($3, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
RETURNING *",
).bind::<diesel::sql_types::Text, _>(body.name)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.orcid)
.bind::<diesel::sql_types::BigInt, _>(editgroup_id)
.get_result(&conn)
.unwrap();
let edit = &edit;
let entity_edit = EntityEdit {
editgroup_id: Some(edit.editgroup_id),
revision: Some(edit.rev_id.unwrap()),
ident: Some(edit.ident_id.to_string()),
edit_id: Some(edit.id),
extra: edit.extra_json.clone(),
};
Box::new(futures::done(Ok(CreatorPostResponse::CreatedEntity(
entity_edit,
))))
}
fn file_post(
&self,
body: models::FileEntity,
_context: &Context,
) -> Box<Future<Item = FilePostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let editor_id = 1; // TODO: auth
let editgroup_id = match body.editgroup_id {
None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
Some(param) => param as i64,
};
let edit: FileEditRow =
diesel::sql_query(
"WITH rev AS ( INSERT INTO file_rev (size, sha1, url)
VALUES ($1, $2, $3)
RETURNING id ),
ident AS ( INSERT INTO file_ident (rev_id)
VALUES ((SELECT rev.id FROM rev))
RETURNING id )
INSERT INTO file_edit (editgroup_id, ident_id, rev_id) VALUES
($4, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
RETURNING *",
).bind::<diesel::sql_types::Nullable<diesel::sql_types::Int8>, _>(body.size)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.sha1)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.url)
.bind::<diesel::sql_types::BigInt, _>(editgroup_id)
.get_result(&conn)
.unwrap();
let edit = &edit;
let entity_edit = EntityEdit {
editgroup_id: Some(edit.editgroup_id),
revision: Some(edit.rev_id.unwrap()),
ident: Some(edit.ident_id.to_string()),
edit_id: Some(edit.id),
extra: edit.extra_json.clone(),
};
Box::new(futures::done(Ok(FilePostResponse::CreatedEntity(
entity_edit,
))))
}
fn work_post(
&self,
body: models::WorkEntity,
_context: &Context,
) -> Box<Future<Item = WorkPostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let editor_id = 1; // TODO: auth
let editgroup_id = match body.editgroup_id {
None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
Some(param) => param as i64,
};
let edit: WorkEditRow =
diesel::sql_query(
"WITH rev AS ( INSERT INTO work_rev (work_type)
VALUES ($1)
RETURNING id ),
ident AS ( INSERT INTO work_ident (rev_id)
VALUES ((SELECT rev.id FROM rev))
RETURNING id )
INSERT INTO work_edit (editgroup_id, ident_id, rev_id) VALUES
($2, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
RETURNING *",
).bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.work_type)
.bind::<diesel::sql_types::BigInt, _>(editgroup_id)
.get_result(&conn)
.unwrap();
let edit = &edit;
let entity_edit = EntityEdit {
editgroup_id: Some(edit.editgroup_id),
revision: Some(edit.rev_id.unwrap()),
ident: Some(edit.ident_id.to_string()),
edit_id: Some(edit.id),
extra: edit.extra_json.clone(),
};
Box::new(futures::done(Ok(WorkPostResponse::CreatedEntity(
entity_edit,
))))
}
fn release_post(
&self,
body: models::ReleaseEntity,
_context: &Context,
) -> Box<Future<Item = ReleasePostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let editor_id = 1; // TODO: auth
let editgroup_id = match body.editgroup_id {
None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
Some(param) => param as i64,
};
let work_id = uuid::Uuid::parse_str(&body.work_id).expect("invalid UUID");
let container_id: Option<uuid::Uuid> = match body.container_id {
Some(id) => Some(uuid::Uuid::parse_str(&id).expect("invalid UUID")),
None => None,
};
let edit: ReleaseEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO release_rev (title, release_type, doi, volume, pages, issue, work_ident_id, container_ident_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id ),
ident AS ( INSERT INTO release_ident (rev_id)
VALUES ((SELECT rev.id FROM rev))
RETURNING id )
INSERT INTO release_edit (editgroup_id, ident_id, rev_id) VALUES
($9, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev))
RETURNING *",
).bind::<diesel::sql_types::Text, _>(body.title)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.release_type)
//XXX .bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.date)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.doi)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.volume)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.pages)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.issue)
.bind::<diesel::sql_types::Uuid, _>(work_id)
.bind::<diesel::sql_types::Nullable<diesel::sql_types::Uuid>, _>(container_id)
.bind::<diesel::sql_types::BigInt, _>(editgroup_id)
.get_result(&conn)
.unwrap();
let edit = &edit;
let entity_edit = EntityEdit {
editgroup_id: Some(edit.editgroup_id),
revision: Some(edit.rev_id.unwrap()),
ident: Some(edit.ident_id.to_string()),
edit_id: Some(edit.id),
extra: edit.extra_json.clone(),
};
Box::new(futures::done(Ok(ReleasePostResponse::CreatedEntity(
entity_edit,
))))
}
fn editgroup_id_accept_post(
&self,
id: i64,
_context: &Context,
) -> Box<Future<Item = EditgroupIdAcceptPostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
accept_editgroup(id as i64, &conn).expect("failed to accept editgroup");
Box::new(futures::done(Ok(
EditgroupIdAcceptPostResponse::MergedSuccessfully(Success {
message: "horray!".to_string(),
}),
)))
}
fn editgroup_post(
&self,
body: models::Editgroup,
_context: &Context,
) -> Box<Future<Item = EditgroupPostResponse, Error = ApiError> + Send> {
let conn = self.db_pool.get().expect("db_pool error");
let row: EditgroupRow = insert_into(editgroup::table)
.values((
editgroup::editor_id.eq(body.editor_id as i64),
editgroup::description.eq(body.description),
editgroup::extra_json.eq(body.extra),
))
.get_result(&conn)
.expect("error creating edit group");
let new_eg = Editgroup {
id: Some(row.id),
editor_id: row.editor_id,
description: row.description,
extra: row.extra_json,
};
Box::new(futures::done(Ok(
EditgroupPostResponse::SuccessfullyCreated(new_eg),
)))
}
fn editor_username_changelog_get(
&self,
username: String,
_context: &Context,
) -> Box<Future<Item = EditorUsernameChangelogGetResponse, Error = ApiError> + Send> {
match self.editor_changelog_get_handler(username) {
Ok(Some(entries)) =>
Box::new(futures::done(Ok(EditorUsernameChangelogGetResponse::FoundMergedChanges(entries)))),
Ok(None) =>
Box::new(futures::done(Ok(EditorUsernameChangelogGetResponse::NotFound(
ErrorResponse { message: "No such entity".to_string() }),
))),
Err(e) =>
// TODO: dig in to error type here
Box::new(futures::done(Ok(EditorUsernameChangelogGetResponse::GenericError(
ErrorResponse { message: e.to_string() },
)))),
}
}
fn editor_username_get(
&self,
username: String,
_context: &Context,
) -> Box<Future<Item = EditorUsernameGetResponse, Error = ApiError> + Send> {
match self.editor_get_handler(username) {
Ok(Some(entity)) =>
Box::new(futures::done(Ok(EditorUsernameGetResponse::FoundEditor(entity)))),
Ok(None) =>
Box::new(futures::done(Ok(EditorUsernameGetResponse::NotFound(
ErrorResponse { message: "No such entity".to_string() }),
))),
Err(e) =>
// TODO: dig in to error type here
Box::new(futures::done(Ok(EditorUsernameGetResponse::GenericError(
ErrorResponse { message: e.to_string() },
)))),
}
}
}
|