1616use OCA \Tables \Middleware \Attribute \RequirePermission ;
1717use OCA \Tables \Model \RowDataInput ;
1818use OCA \Tables \ResponseDefinitions ;
19+ use OCA \Tables \Service \FederationService ;
1920use OCA \Tables \Service \RowService ;
21+ use OCA \Tables \Service \TableService ;
22+ use OCA \Tables \Service \ViewService ;
2023use OCP \AppFramework \Http ;
24+ use OCP \AppFramework \Http \Attribute \ApiRoute ;
2125use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
2226use OCP \AppFramework \Http \DataResponse ;
2327use OCP \IL10N ;
@@ -35,6 +39,9 @@ public function __construct(
3539 IL10N $ n ,
3640 string $ userId ,
3741 protected RowService $ rowService ,
42+ private TableService $ tableService ,
43+ private ViewService $ viewService ,
44+ private FederationService $ federationService ,
3845 ) {
3946 parent ::__construct ($ request , $ logger , $ n , $ userId );
4047 }
@@ -55,6 +62,7 @@ public function __construct(
5562 */
5663 #[NoAdminRequired]
5764 #[RequirePermission(permission: Application::PERMISSION_CREATE , typeParam: 'nodeCollection ' )]
65+ #[ApiRoute(verb: 'POST ' , url: '/api/2/{nodeCollection}/{nodeId}/rows ' , requirements: ['nodeCollection ' => '(tables|views) ' , 'nodeId ' => '(\d+) ' ])]
5866 public function createRow (string $ nodeCollection , int $ nodeId , mixed $ data ): DataResponse {
5967 if (is_string ($ data )) {
6068 $ data = json_decode ($ data , true );
@@ -67,8 +75,16 @@ public function createRow(string $nodeCollection, int $nodeId, mixed $data): Dat
6775 $ tableId = $ viewId = null ;
6876 if ($ iNodeType === Application::NODE_TYPE_TABLE ) {
6977 $ tableId = $ nodeId ;
78+ if ($ this ->federationService ->isNodeFederated ($ tableId , 'table ' )) {
79+ $ table = $ this ->tableService ->find ($ nodeId , true );
80+ return new DataResponse ($ this ->federationService ->createRow ($ table , $ data ));
81+ }
7082 } elseif ($ iNodeType === Application::NODE_TYPE_VIEW ) {
7183 $ viewId = $ nodeId ;
84+ if ($ this ->federationService ->isNodeFederated ($ viewId , 'view ' )) {
85+ $ view = $ this ->viewService ->find ($ nodeId , false , $ this ->userId );
86+ return new DataResponse ($ this ->federationService ->createRow ($ view , $ data ));
87+ }
7288 }
7389
7490 $ newRowData = new RowDataInput ();
@@ -88,4 +104,97 @@ public function createRow(string $nodeCollection, int $nodeId, mixed $data): Dat
88104 return $ this ->handleError ($ e );
89105 }
90106 }
107+
108+ /**
109+ * [api v2] Update a row in a table or a view
110+ *
111+ * @param 'tables'|'views' $nodeCollection Indicates whether to update a row on a table or view
112+ * @param int $nodeId The identifier of the targeted table or view
113+ * @param int $rowId The identifier of the row to update
114+ * @param string|array<string, mixed> $data An array containing the column identifiers and their values
115+ * @return DataResponse<Http::STATUS_OK, TablesRow, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
116+ *
117+ * 200: Row updated
118+ * 403: No permissions
119+ * 404: Not found
120+ * 500: Internal error
121+ */
122+ #[NoAdminRequired]
123+ #[RequirePermission(permission: Application::PERMISSION_UPDATE , typeParam: 'nodeCollection ' )]
124+ #[ApiRoute(verb: 'PUT ' , url: '/api/2/{nodeCollection}/{nodeId}/rows/{rowId} ' , requirements: ['nodeCollection ' => '(tables|views) ' , 'nodeId ' => '(\d+) ' ])]
125+ public function updateRow (string $ nodeCollection , int $ nodeId , int $ rowId , mixed $ data ): DataResponse {
126+ if (is_string ($ data )) {
127+ $ data = json_decode ($ data , true );
128+ }
129+ if (!is_array ($ data )) {
130+ return $ this ->handleBadRequestError (new BadRequestError ('Cannot update row: data input is invalid. ' ));
131+ }
132+ $ iNodeType = ConversionHelper::stringNodeType2Const ($ nodeCollection );
133+ $ tableId = $ viewId = null ;
134+ if ($ iNodeType === Application::NODE_TYPE_TABLE ) {
135+ $ tableId = $ nodeId ;
136+ if ($ this ->federationService ->isNodeFederated ($ tableId , 'table ' )) {
137+ $ table = $ this ->tableService ->find ($ nodeId , true );
138+ return new DataResponse ($ this ->federationService ->updateRow ($ table , $ rowId , $ data ));
139+ }
140+ } elseif ($ iNodeType === Application::NODE_TYPE_VIEW ) {
141+ $ viewId = $ nodeId ;
142+ if ($ this ->federationService ->isNodeFederated ($ viewId , 'view ' )) {
143+ $ view = $ this ->viewService ->find ($ nodeId , false , $ this ->userId );
144+ return new DataResponse ($ this ->federationService ->updateRow ($ view , $ rowId , $ data ));
145+ }
146+ }
147+ try {
148+ return new DataResponse ($ this ->rowService ->updateSet ($ rowId , $ viewId , $ data , $ this ->userId , $ tableId )->jsonSerialize ());
149+ } catch (NotFoundError $ e ) {
150+ return $ this ->handleNotFoundError ($ e );
151+ } catch (PermissionError $ e ) {
152+ return $ this ->handlePermissionError ($ e );
153+ } catch (InternalError |\Exception $ e ) {
154+ return $ this ->handleError ($ e );
155+ }
156+ }
157+
158+ /**
159+ * [api v2] Delete a row in a table or a view
160+ *
161+ * @param 'tables'|'views' $nodeCollection Indicates whether to delete a row on a table or view
162+ * @param int $nodeId The identifier of the targeted table or view
163+ * @param int $rowId The identifier of the row to delete
164+ * @return DataResponse<Http::STATUS_OK, TablesRow, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
165+ *
166+ * 200: Row deleted
167+ * 403: No permissions
168+ * 404: Not found
169+ * 500: Internal error
170+ */
171+ #[NoAdminRequired]
172+ #[RequirePermission(permission: Application::PERMISSION_DELETE , typeParam: 'nodeCollection ' )]
173+ #[ApiRoute(verb: 'DELETE ' , url: '/api/2/{nodeCollection}/{nodeId}/rows/{rowId} ' , requirements: ['nodeCollection ' => '(tables|views) ' , 'nodeId ' => '(\d+) ' ])]
174+ public function deleteRow (string $ nodeCollection , int $ nodeId , int $ rowId ): DataResponse {
175+ $ iNodeType = ConversionHelper::stringNodeType2Const ($ nodeCollection );
176+ $ tableId = $ viewId = null ;
177+ if ($ iNodeType === Application::NODE_TYPE_TABLE ) {
178+ $ tableId = $ nodeId ;
179+ if ($ this ->federationService ->isNodeFederated ($ tableId , 'table ' )) {
180+ $ table = $ this ->tableService ->find ($ nodeId , true );
181+ return new DataResponse ($ this ->federationService ->deleteRow ($ table , $ rowId ));
182+ }
183+ } elseif ($ iNodeType === Application::NODE_TYPE_VIEW ) {
184+ $ viewId = $ nodeId ;
185+ if ($ this ->federationService ->isNodeFederated ($ viewId , 'view ' )) {
186+ $ view = $ this ->viewService ->find ($ nodeId , false , $ this ->userId );
187+ return new DataResponse ($ this ->federationService ->deleteRow ($ view , $ rowId ));
188+ }
189+ }
190+ try {
191+ return new DataResponse ($ this ->rowService ->delete ($ rowId , $ viewId , $ this ->userId , $ tableId )->jsonSerialize ());
192+ } catch (NotFoundError $ e ) {
193+ return $ this ->handleNotFoundError ($ e );
194+ } catch (PermissionError $ e ) {
195+ return $ this ->handlePermissionError ($ e );
196+ } catch (InternalError |\Exception $ e ) {
197+ return $ this ->handleError ($ e );
198+ }
199+ }
91200}
0 commit comments