16
16
17
17
use Cpsit \EventSubmission \Domain \Model \ApiResponseInterface ;
18
18
use Cpsit \EventSubmission \Domain \Model \Job ;
19
+ use Cpsit \EventSubmission \Domain \Repository \JobRepository ;
19
20
use Cpsit \EventSubmission \Event \SubmissionWithdrawnEvent ;
21
+ use Cpsit \EventSubmission \Exceptions \InvalidArgumentException ;
20
22
use Cpsit \EventSubmission \Factory \ApiResponse \ApiResponseFactoryFactory ;
21
23
use Cpsit \EventSubmission \Factory \ApiResponse \ApiResponseFactoryInterface ;
22
- use Cpsit \EventSubmission \Factory \Job \JobFactory ;
23
24
use Cpsit \EventSubmission \Type \SubmissionStatus ;
24
- use Nng \Nnhelpers \Utilities \Db ;
25
25
use Nng \Nnrestapi \Annotations as Api ;
26
26
use Nng \Nnrestapi \Api \AbstractApi ;
27
27
use Psr \EventDispatcher \EventDispatcherInterface ;
28
+ use TYPO3 \CMS \Extbase \Persistence \Generic \PersistenceManager ;
28
29
29
30
/**
30
31
* Event API end point for PUT method
@@ -35,10 +36,12 @@ final class Withdraw extends AbstractApi implements EventApiInterface
35
36
{
36
37
public const RESPONSE_NAME = 'EventWithdrawApiResponse ' ;
37
38
protected ApiResponseFactoryInterface $ responseFactory ;
39
+ public const MESSAGE_INVALID_ARGUMENT = 'Invalid or missing argument %s. ' ;
38
40
39
41
public function __construct (
40
- ApiResponseFactoryFactory $ apiResponseFactoryFactory ,
41
- private readonly Db $ db ,
42
+ ApiResponseFactoryFactory $ apiResponseFactoryFactory ,
43
+ private readonly JobRepository $ jobRepository ,
44
+ private readonly PersistenceManager $ persistenceManager ,
42
45
private readonly EventDispatcherInterface $ eventDispatcher ,
43
46
)
44
47
{
@@ -83,35 +86,27 @@ public function __construct(
83
86
public function withdraw (): array
84
87
{
85
88
$ arguments = $ this ->request ->getArguments ();
86
- $ responseCode = ApiResponseInterface::EVENT_WITHDRAW_ERROR ;
87
- $ id = $ arguments [self ::PARAMETER_ID ];
88
- $ responseData = [
89
- 'id ' => $ id ,
90
- ];
89
+ $ responseCode = ApiResponseInterface::EVENT_WITHDRAW_ERROR ;
91
90
92
- // find job by identifier
93
- // Note: job could be approved and imported already
94
- $ job = $ this ->db ->findOneByValues (
95
- Job::TABLE_NAME ,
96
- [
97
- Job::FIELD_UUID => $ id ,
98
- ]
99
- );
100
-
101
- // update job status
102
- if (!empty ($ job )) {
103
- $ data = [
104
- Job::FIELD_APPROVED => 0 ,
105
- Job::FIELD_STATUS => SubmissionStatus::WITHDRAWN ,
106
- ];
107
-
108
- $ where = [
109
- Job::FIELD_UID => $ job [Job::FIELD_UID ],
91
+ try {
92
+ if (!is_array ($ arguments ) || empty ($ arguments [self ::PARAMETER_ID ])) {
93
+ $ message = sprintf (self ::MESSAGE_INVALID_ARGUMENT , self ::PARAMETER_ID );
94
+ throw new InvalidArgumentException ($ message , $ responseCode );
95
+ }
96
+ $ id = $ arguments [self ::PARAMETER_ID ];
97
+ $ responseData = [
98
+ 'id ' => $ id ,
110
99
];
111
- $ result = $ this ->db ->update (Job::TABLE_NAME , $ data , $ where );
112
100
101
+ $ job = $ this ->jobRepository ->findOneByUuid ($ id );
113
102
114
- if ($ result === 1 ) {
103
+ // update job status
104
+ // Note: job could be approved and imported already
105
+ if ($ job instanceof Job) {
106
+ $ job ->setStatus (SubmissionStatus::WITHDRAWN )
107
+ ->setApproved (false );
108
+ $ this ->jobRepository ->update ($ job );
109
+ $ this ->persistenceManager ->persistAll ();
115
110
$ responseCode = ApiResponseInterface::EVENT_WITHDRAW_SUCCESS ;
116
111
$ this ->eventDispatcher ->dispatch (
117
112
new SubmissionWithdrawnEvent (
@@ -120,6 +115,13 @@ public function withdraw(): array
120
115
)
121
116
);
122
117
}
118
+
119
+ } catch (\Exception $ exception ) {
120
+ return $ this ->responseFactory
121
+ ->errorResponse ()
122
+ ->setMessage ($ exception ->getMessage ())
123
+ ->setCode ($ exception ->getCode ())
124
+ ->toArray ();
123
125
}
124
126
125
127
return $ this ->responseFactory
0 commit comments