-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmutation.rs
More file actions
1526 lines (1301 loc) · 61.3 KB
/
Copy pathmutation.rs
File metadata and controls
1526 lines (1301 loc) · 61.3 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
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use juniper::FieldResult;
use uuid::Uuid;
use crate::graphql::Context;
use crate::markup::{convert_to_jats, ConversionLimit, MarkupFormat};
use crate::model::{
additional_resource::{
AdditionalResource, AdditionalResourcePolicy, NewAdditionalResource,
PatchAdditionalResource, ResourceType,
},
affiliation::{Affiliation, AffiliationPolicy, NewAffiliation, PatchAffiliation},
award::{Award, AwardPolicy, NewAward, PatchAward},
biography::{Biography, BiographyPolicy, NewBiography, PatchBiography},
book_review::{BookReview, BookReviewPolicy, NewBookReview, PatchBookReview},
contact::{Contact, ContactPolicy, NewContact, PatchContact},
contribution::{Contribution, ContributionPolicy, NewContribution, PatchContribution},
contributor::{Contributor, ContributorPolicy, NewContributor, PatchContributor},
endorsement::{Endorsement, EndorsementPolicy, NewEndorsement, PatchEndorsement},
file::{
CompleteFileUpload, File, FilePolicy, FileUpload, FileUploadResponse,
NewAdditionalResourceFileUpload, NewFileUpload, NewFrontcoverFileUpload,
NewPublicationFileUpload, NewWorkFeaturedVideoFileUpload,
},
funding::{Funding, FundingPolicy, NewFunding, PatchFunding},
imprint::{Imprint, ImprintPolicy, NewImprint, PatchImprint},
institution::{Institution, InstitutionPolicy, NewInstitution, PatchInstitution},
issue::{Issue, IssuePolicy, NewIssue, PatchIssue},
language::{Language, LanguagePolicy, NewLanguage, PatchLanguage},
location::{Location, LocationPolicy, NewLocation, PatchLocation},
price::{NewPrice, PatchPrice, Price, PricePolicy},
publication::{
NewPublication, PatchPublication, Publication, PublicationPolicy, PublicationProperties,
},
publisher::{NewPublisher, PatchPublisher, Publisher, PublisherPolicy},
r#abstract::{Abstract, AbstractPolicy, NewAbstract, PatchAbstract},
reference::{NewReference, PatchReference, Reference, ReferencePolicy},
series::{NewSeries, PatchSeries, Series, SeriesPolicy},
subject::{NewSubject, PatchSubject, Subject, SubjectPolicy},
title::{convert_title_to_jats, NewTitle, PatchTitle, Title, TitlePolicy},
work::{NewWork, PatchWork, Work, WorkPolicy},
work_featured_video::{
NewWorkFeaturedVideo, PatchWorkFeaturedVideo, WorkFeaturedVideo, WorkFeaturedVideoPolicy,
},
work_relation::{NewWorkRelation, PatchWorkRelation, WorkRelation, WorkRelationPolicy},
Crud, Reorder,
};
use crate::policy::{
CreatePolicy, DeletePolicy, MovePolicy, PolicyContext, UpdatePolicy, UserAccess,
};
use crate::storage::{
additional_resource_cleanup_plan, build_cdn_url, copy_temp_object_to_final, delete_object,
head_object, probe_video_dimensions, publication_cleanup_plan, reconcile_replaced_object,
run_cleanup_plan_sync, temp_key, work_cleanup_plan, work_featured_video_cleanup_plan,
StorageConfig,
};
use thoth_errors::ThothError;
pub struct MutationRoot;
#[juniper::graphql_object(Context = Context)]
impl MutationRoot {
#[graphql(description = "Create a new work with the specified values")]
fn create_work(
context: &Context,
#[graphql(description = "Values for work to be created")] data: NewWork,
) -> FieldResult<Work> {
WorkPolicy::can_create(context, &data, ())?;
Work::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new publisher with the specified values")]
fn create_publisher(
context: &Context,
#[graphql(description = "Values for publisher to be created")] data: NewPublisher,
) -> FieldResult<Publisher> {
PublisherPolicy::can_create(context, &data, ())?;
Publisher::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new imprint with the specified values")]
fn create_imprint(
context: &Context,
#[graphql(description = "Values for imprint to be created")] data: NewImprint,
) -> FieldResult<Imprint> {
ImprintPolicy::can_create(context, &data, ())?;
Imprint::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new contributor with the specified values")]
fn create_contributor(
context: &Context,
#[graphql(description = "Values for contributor to be created")] data: NewContributor,
) -> FieldResult<Contributor> {
ContributorPolicy::can_create(context, &data, ())?;
Contributor::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new contribution with the specified values")]
fn create_contribution(
context: &Context,
#[graphql(description = "Values for contribution to be created")] data: NewContribution,
) -> FieldResult<Contribution> {
ContributionPolicy::can_create(context, &data, ())?;
Contribution::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new publication with the specified values")]
fn create_publication(
context: &Context,
#[graphql(description = "Values for publication to be created")] data: NewPublication,
) -> FieldResult<Publication> {
let data = data.into_normalised()?;
PublicationPolicy::can_create(context, &data, ())?;
Publication::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new series with the specified values")]
fn create_series(
context: &Context,
#[graphql(description = "The markup format of the series description")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values for series to be created")] mut data: NewSeries,
) -> FieldResult<Series> {
SeriesPolicy::can_create(context, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.series_description = data
.series_description
.map(|series_description| {
convert_to_jats(series_description, markup, ConversionLimit::Abstract)
})
.transpose()?;
Series::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new issue with the specified values")]
fn create_issue(
context: &Context,
#[graphql(description = "Values for issue to be created")] data: NewIssue,
) -> FieldResult<Issue> {
IssuePolicy::can_create(context, &data, ())?;
Issue::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new language with the specified values")]
fn create_language(
context: &Context,
#[graphql(description = "Values for language to be created")] data: NewLanguage,
) -> FieldResult<Language> {
LanguagePolicy::can_create(context, &data, ())?;
Language::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new title with the specified values")]
fn create_title(
context: &Context,
#[graphql(description = "The markup format of the title")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values for title to be created")] mut data: NewTitle,
) -> FieldResult<Title> {
TitlePolicy::can_create(context, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
convert_title_to_jats(&mut data, markup)?;
Title::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new abstract with the specified values")]
fn create_abstract(
context: &Context,
#[graphql(description = "The markup format of the abstract")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values for abstract to be created")] mut data: NewAbstract,
) -> FieldResult<Abstract> {
AbstractPolicy::can_create(context, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
data.content = convert_to_jats(data.content, markup, ConversionLimit::Abstract)?;
Abstract::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new biography with the specified values")]
fn create_biography(
context: &Context,
#[graphql(description = "The markup format of the biography")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values for biography to be created")] mut data: NewBiography,
) -> FieldResult<Biography> {
BiographyPolicy::can_create(context, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
data.content = convert_to_jats(data.content, markup, ConversionLimit::Biography)?;
Biography::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new institution with the specified values")]
fn create_institution(
context: &Context,
#[graphql(description = "Values for institution to be created")] data: NewInstitution,
) -> FieldResult<Institution> {
InstitutionPolicy::can_create(context, &data, ())?;
Institution::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new funding with the specified values")]
fn create_funding(
context: &Context,
#[graphql(description = "Values for funding to be created")] data: NewFunding,
) -> FieldResult<Funding> {
FundingPolicy::can_create(context, &data, ())?;
Funding::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new location with the specified values")]
fn create_location(
context: &Context,
#[graphql(description = "Values for location to be created")] data: NewLocation,
) -> FieldResult<Location> {
LocationPolicy::can_create(context, &data, ())?;
Location::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new price with the specified values")]
fn create_price(
context: &Context,
#[graphql(description = "Values for price to be created")] data: NewPrice,
) -> FieldResult<Price> {
PricePolicy::can_create(context, &data, ())?;
Price::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new subject with the specified values")]
fn create_subject(
context: &Context,
#[graphql(description = "Values for subject to be created")] data: NewSubject,
) -> FieldResult<Subject> {
SubjectPolicy::can_create(context, &data, ())?;
Subject::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new affiliation with the specified values")]
fn create_affiliation(
context: &Context,
#[graphql(description = "Values for affiliation to be created")] data: NewAffiliation,
) -> FieldResult<Affiliation> {
AffiliationPolicy::can_create(context, &data, ())?;
Affiliation::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new work relation with the specified values")]
fn create_work_relation(
context: &Context,
#[graphql(description = "Values for work relation to be created")] data: NewWorkRelation,
) -> FieldResult<WorkRelation> {
WorkRelationPolicy::can_create(context, &data, ())?;
WorkRelation::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new reference with the specified values")]
fn create_reference(
context: &Context,
#[graphql(description = "Values for reference to be created")] data: NewReference,
) -> FieldResult<Reference> {
ReferencePolicy::can_create(context, &data, ())?;
Reference::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new additional resource with the specified values")]
fn create_additional_resource(
context: &Context,
#[graphql(description = "The markup format of the additional resource text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values for additional resource to be created")]
mut data: NewAdditionalResource,
) -> FieldResult<AdditionalResource> {
AdditionalResourcePolicy::can_create(context, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = convert_to_jats(data.title, markup, ConversionLimit::Title)?;
data.description = data
.description
.map(|description| convert_to_jats(description, markup, ConversionLimit::Abstract))
.transpose()?;
AdditionalResource::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new award with the specified values")]
fn create_award(
context: &Context,
#[graphql(description = "The markup format of the award text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values for award to be created")] mut data: NewAward,
) -> FieldResult<Award> {
AwardPolicy::can_create(context, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = convert_to_jats(data.title, markup, ConversionLimit::Title)?;
data.prize_statement = data
.prize_statement
.map(|prize_statement| {
convert_to_jats(prize_statement, markup, ConversionLimit::Abstract)
})
.transpose()?;
Award::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new endorsement with the specified values")]
fn create_endorsement(
context: &Context,
#[graphql(description = "The markup format of the endorsement rich-text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values for endorsement to be created")] mut data: NewEndorsement,
) -> FieldResult<Endorsement> {
EndorsementPolicy::can_create(context, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.author_role = data
.author_role
.map(|author_role| convert_to_jats(author_role, markup, ConversionLimit::Abstract))
.transpose()?;
data.text = data
.text
.map(|text| convert_to_jats(text, markup, ConversionLimit::Abstract))
.transpose()?;
Endorsement::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new book review with the specified values")]
fn create_book_review(
context: &Context,
#[graphql(description = "The markup format of the book review text field")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values for book review to be created")] mut data: NewBookReview,
) -> FieldResult<BookReview> {
BookReviewPolicy::can_create(context, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = data
.title
.map(|title| convert_to_jats(title, markup, ConversionLimit::Title))
.transpose()?;
data.text = data
.text
.map(|text| convert_to_jats(text, markup, ConversionLimit::Abstract))
.transpose()?;
BookReview::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new featured video with the specified values")]
fn create_work_featured_video(
context: &Context,
#[graphql(description = "Values for featured video to be created")]
data: NewWorkFeaturedVideo,
) -> FieldResult<WorkFeaturedVideo> {
WorkFeaturedVideoPolicy::can_create(context, &data, ())?;
WorkFeaturedVideo::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Create a new contact with the specified values")]
fn create_contact(
context: &Context,
#[graphql(description = "Values for contact to be created")] data: NewContact,
) -> FieldResult<Contact> {
ContactPolicy::can_create(context, &data, ())?;
Contact::create(&context.db, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing work with the specified values")]
fn update_work(
context: &Context,
#[graphql(description = "Values to apply to existing work")] data: PatchWork,
) -> FieldResult<Work> {
let work = context.load_current(&data.work_id)?;
WorkPolicy::can_update(context, &work, &data, ())?;
// update the work and, if it succeeds, synchronise its children statuses and pub. date
let w = work.update(context, &data)?;
for child in work.children(&context.db)? {
if child.publication_date != w.publication_date
|| child.work_status != w.work_status
|| child.withdrawn_date != w.withdrawn_date
{
let mut data: PatchWork = child.clone().into();
data.publication_date = w.publication_date;
data.withdrawn_date = w.withdrawn_date;
data.work_status = w.work_status;
child.update(context, &data)?;
}
}
Ok(w)
}
#[graphql(description = "Update an existing publisher with the specified values")]
fn update_publisher(
context: &Context,
#[graphql(description = "Values to apply to existing publisher")] data: PatchPublisher,
) -> FieldResult<Publisher> {
let publisher = context.load_current(&data.publisher_id)?;
PublisherPolicy::can_update(context, &publisher, &data, ())?;
publisher.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing imprint with the specified values")]
fn update_imprint(
context: &Context,
#[graphql(description = "Values to apply to existing imprint")] data: PatchImprint,
) -> FieldResult<Imprint> {
let imprint: Imprint = context.load_current(&data.imprint_id)?;
let mut data = data;
if !context.user().is_some_and(|user| user.is_superuser()) {
// Non-superusers cannot modify storage fields. If these fields are omitted in the
// GraphQL payload, Juniper deserializes them as `None`, so preserve existing values.
if data.s3_bucket.is_none() {
data.s3_bucket = imprint.s3_bucket.clone();
}
if data.cdn_domain.is_none() {
data.cdn_domain = imprint.cdn_domain.clone();
}
if data.cloudfront_dist_id.is_none() {
data.cloudfront_dist_id = imprint.cloudfront_dist_id.clone();
}
}
ImprintPolicy::can_update(context, &imprint, &data, ())?;
imprint.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing contributor with the specified values")]
fn update_contributor(
context: &Context,
#[graphql(description = "Values to apply to existing contributor")] data: PatchContributor,
) -> FieldResult<Contributor> {
let contributor = context.load_current(&data.contributor_id)?;
ContributorPolicy::can_update(context, &contributor, &data, ())?;
contributor.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing contribution with the specified values")]
fn update_contribution(
context: &Context,
#[graphql(description = "Values to apply to existing contribution")]
data: PatchContribution,
) -> FieldResult<Contribution> {
let contribution = context.load_current(&data.contribution_id)?;
ContributionPolicy::can_update(context, &contribution, &data, ())?;
contribution.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing publication with the specified values")]
fn update_publication(
context: &Context,
#[graphql(description = "Values to apply to existing publication")] data: PatchPublication,
) -> FieldResult<Publication> {
let data = data.into_normalised()?;
let publication = context.load_current(&data.publication_id)?;
PublicationPolicy::can_update(context, &publication, &data, ())?;
publication.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing series with the specified values")]
fn update_series(
context: &Context,
#[graphql(description = "The markup format of the series description")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values to apply to existing series")] mut data: PatchSeries,
) -> FieldResult<Series> {
let series = context.load_current(&data.series_id)?;
SeriesPolicy::can_update(context, &series, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.series_description = data
.series_description
.map(|series_description| {
convert_to_jats(series_description, markup, ConversionLimit::Abstract)
})
.transpose()?;
series.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing issue with the specified values")]
fn update_issue(
context: &Context,
#[graphql(description = "Values to apply to existing issue")] data: PatchIssue,
) -> FieldResult<Issue> {
let issue = context.load_current(&data.issue_id)?;
IssuePolicy::can_update(context, &issue, &data, ())?;
issue.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing language with the specified values")]
fn update_language(
context: &Context,
#[graphql(description = "Values to apply to existing language")] data: PatchLanguage,
) -> FieldResult<Language> {
let language = context.load_current(&data.language_id)?;
LanguagePolicy::can_update(context, &language, &data, ())?;
language.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing institution with the specified values")]
fn update_institution(
context: &Context,
#[graphql(description = "Values to apply to existing institution")] data: PatchInstitution,
) -> FieldResult<Institution> {
let institution = context.load_current(&data.institution_id)?;
InstitutionPolicy::can_update(context, &institution, &data, ())?;
institution.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing funding with the specified values")]
fn update_funding(
context: &Context,
#[graphql(description = "Values to apply to existing funding")] data: PatchFunding,
) -> FieldResult<Funding> {
let funding = context.load_current(&data.funding_id)?;
FundingPolicy::can_update(context, &funding, &data, ())?;
funding.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing location with the specified values")]
fn update_location(
context: &Context,
#[graphql(description = "Values to apply to existing location")] data: PatchLocation,
) -> FieldResult<Location> {
let current_location = context.load_current(&data.location_id)?;
LocationPolicy::can_update(context, ¤t_location, &data, ())?;
current_location.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing price with the specified values")]
fn update_price(
context: &Context,
#[graphql(description = "Values to apply to existing price")] data: PatchPrice,
) -> FieldResult<Price> {
let price = context.load_current(&data.price_id)?;
PricePolicy::can_update(context, &price, &data, ())?;
price.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing subject with the specified values")]
fn update_subject(
context: &Context,
#[graphql(description = "Values to apply to existing subject")] data: PatchSubject,
) -> FieldResult<Subject> {
let subject = context.load_current(&data.subject_id)?;
SubjectPolicy::can_update(context, &subject, &data, ())?;
subject.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing affiliation with the specified values")]
fn update_affiliation(
context: &Context,
#[graphql(description = "Values to apply to existing affiliation")] data: PatchAffiliation,
) -> FieldResult<Affiliation> {
let affiliation = context.load_current(&data.affiliation_id)?;
AffiliationPolicy::can_update(context, &affiliation, &data, ())?;
affiliation.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing work relation with the specified values")]
fn update_work_relation(
context: &Context,
#[graphql(description = "Values to apply to existing work relation")]
data: PatchWorkRelation,
) -> FieldResult<WorkRelation> {
let work_relation = context.load_current(&data.work_relation_id)?;
WorkRelationPolicy::can_update(context, &work_relation, &data, ())?;
work_relation.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing reference with the specified values")]
fn update_reference(
context: &Context,
#[graphql(description = "Values to apply to existing reference")] data: PatchReference,
) -> FieldResult<Reference> {
let reference = context.load_current(&data.reference_id)?;
ReferencePolicy::can_update(context, &reference, &data, ())?;
reference.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing additional resource with the specified values")]
fn update_additional_resource(
context: &Context,
#[graphql(description = "The markup format of the additional resource text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values to apply to existing additional resource")]
mut data: PatchAdditionalResource,
) -> FieldResult<AdditionalResource> {
let additional_resource = context.load_current(&data.additional_resource_id)?;
AdditionalResourcePolicy::can_update(context, &additional_resource, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = convert_to_jats(data.title, markup, ConversionLimit::Title)?;
data.description = data
.description
.map(|description| convert_to_jats(description, markup, ConversionLimit::Abstract))
.transpose()?;
additional_resource
.update(context, &data)
.map_err(Into::into)
}
#[graphql(description = "Update an existing award with the specified values")]
fn update_award(
context: &Context,
#[graphql(description = "The markup format of the award text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values to apply to existing award")] mut data: PatchAward,
) -> FieldResult<Award> {
let award = context.load_current(&data.award_id)?;
AwardPolicy::can_update(context, &award, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = convert_to_jats(data.title, markup, ConversionLimit::Title)?;
data.prize_statement = data
.prize_statement
.map(|prize_statement| {
convert_to_jats(prize_statement, markup, ConversionLimit::Abstract)
})
.transpose()?;
award.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing endorsement with the specified values")]
fn update_endorsement(
context: &Context,
#[graphql(description = "The markup format of the endorsement rich-text fields")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values to apply to existing endorsement")]
mut data: PatchEndorsement,
) -> FieldResult<Endorsement> {
let endorsement = context.load_current(&data.endorsement_id)?;
EndorsementPolicy::can_update(context, &endorsement, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.author_role = data
.author_role
.map(|author_role| convert_to_jats(author_role, markup, ConversionLimit::Abstract))
.transpose()?;
data.text = data
.text
.map(|text| convert_to_jats(text, markup, ConversionLimit::Abstract))
.transpose()?;
endorsement.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing book review with the specified values")]
fn update_book_review(
context: &Context,
#[graphql(description = "The markup format of the book review text field")]
markup_format: Option<MarkupFormat>,
#[graphql(description = "Values to apply to existing book review")]
mut data: PatchBookReview,
) -> FieldResult<BookReview> {
let book_review = context.load_current(&data.book_review_id)?;
BookReviewPolicy::can_update(context, &book_review, &data, ())?;
let markup = markup_format.unwrap_or(MarkupFormat::JatsXml);
data.title = data
.title
.map(|title| convert_to_jats(title, markup, ConversionLimit::Title))
.transpose()?;
data.text = data
.text
.map(|text| convert_to_jats(text, markup, ConversionLimit::Abstract))
.transpose()?;
book_review.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing featured video with the specified values")]
fn update_work_featured_video(
context: &Context,
#[graphql(description = "Values to apply to existing featured video")]
data: PatchWorkFeaturedVideo,
) -> FieldResult<WorkFeaturedVideo> {
let work_featured_video = context.load_current(&data.work_featured_video_id)?;
WorkFeaturedVideoPolicy::can_update(context, &work_featured_video, &data, ())?;
work_featured_video
.update(context, &data)
.map_err(Into::into)
}
#[graphql(description = "Update an existing contact with the specified values")]
fn update_contact(
context: &Context,
#[graphql(description = "Values to apply to existing contact")] data: PatchContact,
) -> FieldResult<Contact> {
let contact = context.load_current(&data.contact_id)?;
ContactPolicy::can_update(context, &contact, &data, ())?;
contact.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing title with the specified values")]
fn update_title(
context: &Context,
#[graphql(description = "The markup format of the title")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values to apply to existing title")] mut data: PatchTitle,
) -> FieldResult<Title> {
let title = context.load_current(&data.title_id)?;
TitlePolicy::can_update(context, &title, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
convert_title_to_jats(&mut data, markup)?;
title.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing abstract with the specified values")]
fn update_abstract(
context: &Context,
#[graphql(description = "The markup format of the abstract")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values to apply to existing abstract")] mut data: PatchAbstract,
) -> FieldResult<Abstract> {
let r#abstract = context.load_current(&data.abstract_id)?;
AbstractPolicy::can_update(context, &r#abstract, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
data.content = convert_to_jats(data.content, markup, ConversionLimit::Abstract)?;
r#abstract.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Update an existing biography with the specified values")]
fn update_biography(
context: &Context,
#[graphql(description = "The markup format of the biography")] markup_format: Option<
MarkupFormat,
>,
#[graphql(description = "Values to apply to existing biography")] mut data: PatchBiography,
) -> FieldResult<Biography> {
let biography = context.load_current(&data.biography_id)?;
BiographyPolicy::can_update(context, &biography, &data, markup_format)?;
let markup = markup_format.expect("Validated by policy");
data.content = convert_to_jats(data.content, markup, ConversionLimit::Biography)?;
biography.update(context, &data).map_err(Into::into)
}
#[graphql(description = "Delete a single work using its ID")]
fn delete_work(
context: &Context,
#[graphql(description = "Thoth ID of work to be deleted")] work_id: Uuid,
) -> FieldResult<Work> {
let work = context.load_current(&work_id)?;
WorkPolicy::can_delete(context, &work)?;
let cleanup_plan = work_cleanup_plan(context.db(), &work)?;
let deleted_work = work.delete(&context.db)?;
if let Some(plan) = cleanup_plan {
run_cleanup_plan_sync(context.s3_client(), context.cloudfront_client(), plan);
}
Ok(deleted_work)
}
#[graphql(description = "Delete a single publisher using its ID")]
fn delete_publisher(
context: &Context,
#[graphql(description = "Thoth ID of publisher to be deleted")] publisher_id: Uuid,
) -> FieldResult<Publisher> {
let publisher = context.load_current(&publisher_id)?;
PublisherPolicy::can_delete(context, &publisher)?;
publisher.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single imprint using its ID")]
fn delete_imprint(
context: &Context,
#[graphql(description = "Thoth ID of imprint to be deleted")] imprint_id: Uuid,
) -> FieldResult<Imprint> {
let imprint = context.load_current(&imprint_id)?;
ImprintPolicy::can_delete(context, &imprint)?;
imprint.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single contributor using its ID")]
fn delete_contributor(
context: &Context,
#[graphql(description = "Thoth ID of contributor to be deleted")] contributor_id: Uuid,
) -> FieldResult<Contributor> {
let contributor = context.load_current(&contributor_id)?;
ContributorPolicy::can_delete(context, &contributor)?;
contributor.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single contribution using its ID")]
fn delete_contribution(
context: &Context,
#[graphql(description = "Thoth ID of contribution to be deleted")] contribution_id: Uuid,
) -> FieldResult<Contribution> {
let contribution = context.load_current(&contribution_id)?;
ContributionPolicy::can_delete(context, &contribution)?;
contribution.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single publication using its ID")]
fn delete_publication(
context: &Context,
#[graphql(description = "Thoth ID of publication to be deleted")] publication_id: Uuid,
) -> FieldResult<Publication> {
let publication = context.load_current(&publication_id)?;
PublicationPolicy::can_delete(context, &publication)?;
let cleanup_plan = publication_cleanup_plan(context.db(), &publication)?;
let deleted_publication = publication.delete(&context.db)?;
if let Some(plan) = cleanup_plan {
run_cleanup_plan_sync(context.s3_client(), context.cloudfront_client(), plan);
}
Ok(deleted_publication)
}
#[graphql(description = "Delete a single series using its ID")]
fn delete_series(
context: &Context,
#[graphql(description = "Thoth ID of series to be deleted")] series_id: Uuid,
) -> FieldResult<Series> {
let series = context.load_current(&series_id)?;
SeriesPolicy::can_delete(context, &series)?;
series.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single issue using its ID")]
fn delete_issue(
context: &Context,
#[graphql(description = "Thoth ID of issue to be deleted")] issue_id: Uuid,
) -> FieldResult<Issue> {
let issue = context.load_current(&issue_id)?;
IssuePolicy::can_delete(context, &issue)?;
issue.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single language using its ID")]
fn delete_language(
context: &Context,
#[graphql(description = "Thoth ID of language to be deleted")] language_id: Uuid,
) -> FieldResult<Language> {
let language = context.load_current(&language_id)?;
LanguagePolicy::can_delete(context, &language)?;
language.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single title using its ID")]
fn delete_title(
context: &Context,
#[graphql(description = "Thoth ID of title to be deleted")] title_id: Uuid,
) -> FieldResult<Title> {
let title = context.load_current(&title_id)?;
TitlePolicy::can_delete(context, &title)?;
title.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single institution using its ID")]
fn delete_institution(
context: &Context,
#[graphql(description = "Thoth ID of institution to be deleted")] institution_id: Uuid,
) -> FieldResult<Institution> {
let institution = context.load_current(&institution_id)?;
InstitutionPolicy::can_delete(context, &institution)?;
institution.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single funding using its ID")]
fn delete_funding(
context: &Context,
#[graphql(description = "Thoth ID of funding to be deleted")] funding_id: Uuid,
) -> FieldResult<Funding> {
let funding = context.load_current(&funding_id)?;
FundingPolicy::can_delete(context, &funding)?;
funding.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single location using its ID")]
fn delete_location(
context: &Context,
#[graphql(description = "Thoth ID of location to be deleted")] location_id: Uuid,
) -> FieldResult<Location> {
let location = context.load_current(&location_id)?;
LocationPolicy::can_delete(context, &location)?;
location.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single price using its ID")]
fn delete_price(
context: &Context,
#[graphql(description = "Thoth ID of price to be deleted")] price_id: Uuid,
) -> FieldResult<Price> {
let price = context.load_current(&price_id)?;
PricePolicy::can_delete(context, &price)?;
price.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single subject using its ID")]
fn delete_subject(
context: &Context,
#[graphql(description = "Thoth ID of subject to be deleted")] subject_id: Uuid,
) -> FieldResult<Subject> {
let subject = context.load_current(&subject_id)?;
SubjectPolicy::can_delete(context, &subject)?;
subject.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single affiliation using its ID")]
fn delete_affiliation(
context: &Context,
#[graphql(description = "Thoth ID of affiliation to be deleted")] affiliation_id: Uuid,
) -> FieldResult<Affiliation> {
let affiliation = context.load_current(&affiliation_id)?;
AffiliationPolicy::can_delete(context, &affiliation)?;
affiliation.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single work relation using its ID")]
fn delete_work_relation(
context: &Context,
#[graphql(description = "Thoth ID of work relation to be deleted")] work_relation_id: Uuid,
) -> FieldResult<WorkRelation> {
let work_relation = context.load_current(&work_relation_id)?;
WorkRelationPolicy::can_delete(context, &work_relation)?;
work_relation.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single reference using its ID")]
fn delete_reference(
context: &Context,
#[graphql(description = "Thoth ID of reference to be deleted")] reference_id: Uuid,
) -> FieldResult<Reference> {
let reference = context.load_current(&reference_id)?;
ReferencePolicy::can_delete(context, &reference)?;
reference.delete(&context.db).map_err(Into::into)
}
#[graphql(description = "Delete a single additional resource using its ID")]
fn delete_additional_resource(
context: &Context,
#[graphql(description = "Thoth ID of additional resource to be deleted")]
additional_resource_id: Uuid,
) -> FieldResult<AdditionalResource> {
let additional_resource = context.load_current(&additional_resource_id)?;
AdditionalResourcePolicy::can_delete(context, &additional_resource)?;
let cleanup_plan = additional_resource_cleanup_plan(context.db(), &additional_resource)?;
let deleted_additional_resource = additional_resource.delete(&context.db)?;
if let Some(plan) = cleanup_plan {
run_cleanup_plan_sync(context.s3_client(), context.cloudfront_client(), plan);