1010
1111use Closure ;
1212use OCA \Polls \Exceptions \Exception ;
13- use OCA \Polls \Exceptions \NoUpdatesException ;
1413use OCP \AppFramework \Controller ;
15- use OCP \AppFramework \Db \DoesNotExistException ;
1614use OCP \AppFramework \Http ;
1715use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
1816use OCP \AppFramework \Http \JSONResponse ;
@@ -32,54 +30,23 @@ public function __construct(
3230 /**
3331 * response
3432 * @param Closure $callback Callback function
33+ * @param int $successStatus HTTP status code for success
3534 */
3635 #[NoAdminRequired]
37- protected function response (Closure $ callback ): JSONResponse {
36+ protected function response (
37+ Closure $ callback ,
38+ int $ successStatus = Http::STATUS_OK ,
39+ ): JSONResponse {
3840 try {
39- return new JSONResponse ($ callback ());
41+ return new JSONResponse ($ callback (), $ successStatus );
4042 } catch (Exception $ e ) {
41- return new JSONResponse (['message ' => $ e ->getMessage ()], $ e ->getStatus ());
42- }
43- }
4443
45- /**
46- * response
47- * @param Closure $callback Callback function
48- */
49- #[NoAdminRequired]
50- protected function responseLong (Closure $ callback ): JSONResponse {
51- try {
52- return new JSONResponse ($ callback ());
53- } catch (NoUpdatesException $ e ) {
54- return new JSONResponse ([], Http::STATUS_NOT_MODIFIED );
55- }
56- }
44+ if ($ e ->getStatus () === Http::STATUS_NOT_MODIFIED ) {
45+ return new JSONResponse (statusCode: $ e ->getStatus ());
46+ }
5747
58- /**
59- * responseCreate
60- * @param Closure $callback Callback function
61- */
62- #[NoAdminRequired]
63- protected function responseCreate (Closure $ callback ): JSONResponse {
64- try {
65- return new JSONResponse ($ callback (), Http::STATUS_CREATED );
66- } catch (Exception $ e ) {
6748 return new JSONResponse (['message ' => $ e ->getMessage ()], $ e ->getStatus ());
6849 }
6950 }
7051
71- /**
72- * responseDeleteTolerant
73- * @param Closure $callback Callback function
74- */
75- #[NoAdminRequired]
76- protected function responseDeleteTolerant (Closure $ callback ): JSONResponse {
77- try {
78- return new JSONResponse ($ callback ());
79- } catch (DoesNotExistException $ e ) {
80- return new JSONResponse (['message ' => 'Not found, assume already deleted ' ]);
81- } catch (Exception $ e ) {
82- return new JSONResponse (['message ' => $ e ->getMessage ()], $ e ->getStatus ());
83- }
84- }
8552}
0 commit comments