22
33namespace App \Http \Controllers \Api ;
44
5+ use App \Http \Controllers \Controller ;
56use App \Models \Todo ;
67use Illuminate \Http \Request ;
78use Illuminate \Support \Carbon ;
89use Illuminate \Support \Facades \Log ;
9- use App \Http \Controllers \Controller ;
1010
1111class TodoSyncController extends Controller
1212{
1313 public function push (Request $ request )
1414 {
1515 $ data = $ request ->validate ([
16- 'operations ' => ['required ' , 'array ' ],
17- 'operations.*.uuid ' => ['required ' , 'uuid ' ],
16+ 'operations ' => ['required ' , 'array ' ],
17+ 'operations.*.uuid ' => ['required ' , 'uuid ' ],
1818 'operations.*.operation ' => ['required ' , 'in:created,updated,deleted ' ],
19- 'operations.*.payload ' => ['nullable ' , 'array ' ],
19+ 'operations.*.payload ' => ['nullable ' , 'array ' ],
2020 ]);
2121
2222 $ user = $ request ->user ();
@@ -30,10 +30,10 @@ public function push(Request $request)
3030 $ incomingModifiedAt = $ this ->parseModifiedAt ($ payload );
3131
3232 Log::debug ('API SYNC PUSH: incoming operation ' , [
33- 'user_id ' => $ user ->id ,
34- 'uuid ' => $ uuid ,
35- 'operation ' => $ type ,
36- 'payload ' => $ payload ,
33+ 'user_id ' => $ user ->id ,
34+ 'uuid ' => $ uuid ,
35+ 'operation ' => $ type ,
36+ 'payload ' => $ payload ,
3737 'incoming_modified_at ' => $ incomingModifiedAt ->toIso8601String (),
3838 ]);
3939
@@ -44,32 +44,32 @@ public function push(Request $request)
4444
4545 if ($ type === 'deleted ' ) {
4646 if ($ todo ) {
47- if (!$ todo ->last_modified_at || $ incomingModifiedAt ->gte ($ todo ->last_modified_at )) {
47+ if (! $ todo ->last_modified_at || $ incomingModifiedAt ->gte ($ todo ->last_modified_at )) {
4848 $ todo ->last_modified_at = $ incomingModifiedAt ;
4949 $ todo ->save ();
5050 $ todo ->delete ();
5151 }
5252 }
5353
5454 $ results [] = [
55- 'uuid ' => $ uuid ,
56- 'status ' => 'ok ' ,
55+ 'uuid ' => $ uuid ,
56+ 'status ' => 'ok ' ,
5757 'deleted_at ' => $ incomingModifiedAt ->toIso8601String (),
5858 ];
5959
6060 continue ;
6161 }
6262
63- if (!$ todo ) {
63+ if (! $ todo ) {
6464 $ todo = Todo::create ([
65- 'uuid ' => $ uuid ,
66- 'user_id ' => $ user ->id ,
67- 'title ' => $ this ->payloadString ($ payload , 'title ' , '' ),
68- 'is_completed ' => $ this ->payloadBool ($ payload , 'is_completed ' , false ),
65+ 'uuid ' => $ uuid ,
66+ 'user_id ' => $ user ->id ,
67+ 'title ' => $ this ->payloadString ($ payload , 'title ' , '' ),
68+ 'is_completed ' => $ this ->payloadBool ($ payload , 'is_completed ' , false ),
6969 'last_modified_at ' => $ incomingModifiedAt ,
7070 ]);
7171 } else {
72- if (!$ todo ->last_modified_at || $ incomingModifiedAt ->gte ($ todo ->last_modified_at )) {
72+ if (! $ todo ->last_modified_at || $ incomingModifiedAt ->gte ($ todo ->last_modified_at )) {
7373 $ todo ->title = $ this ->payloadString ($ payload , 'title ' , $ todo ->title );
7474 $ todo ->is_completed = $ this ->payloadBool ($ payload , 'is_completed ' , (bool ) $ todo ->is_completed );
7575 $ todo ->last_modified_at = $ incomingModifiedAt ;
@@ -82,24 +82,24 @@ public function push(Request $request)
8282 }
8383
8484 Log::debug ('API SYNC PUSH: saved todo ' , [
85- 'uuid ' => $ todo ->uuid ,
86- 'title ' => $ todo ->title ,
87- 'is_completed ' => (bool )$ todo ->is_completed ,
85+ 'uuid ' => $ todo ->uuid ,
86+ 'title ' => $ todo ->title ,
87+ 'is_completed ' => (bool ) $ todo ->is_completed ,
8888 'last_modified_at ' => optional ($ todo ->last_modified_at )?->toIso8601String(),
8989 ]);
9090
9191 $ results [] = [
92- 'uuid ' => $ todo ->uuid ,
93- 'status ' => 'ok ' ,
94- 'title ' => $ todo ->title ,
95- 'is_completed ' => (bool )$ todo ->is_completed ,
92+ 'uuid ' => $ todo ->uuid ,
93+ 'status ' => 'ok ' ,
94+ 'title ' => $ todo ->title ,
95+ 'is_completed ' => (bool ) $ todo ->is_completed ,
9696 'last_modified_at ' => optional ($ todo ->last_modified_at )->toIso8601String (),
97- 'deleted_at ' => optional ($ todo ->deleted_at )->toIso8601String (),
97+ 'deleted_at ' => optional ($ todo ->deleted_at )->toIso8601String (),
9898 ];
9999 }
100100
101101 return response ()->json ([
102- 'results ' => $ results ,
102+ 'results ' => $ results ,
103103 'server_time ' => now ()->toIso8601String (),
104104 ]);
105105 }
@@ -127,19 +127,19 @@ public function pull(Request $request)
127127
128128 return response ()->json ([
129129 'todos ' => $ todos ->map (fn (Todo $ todo ) => [
130- 'uuid ' => $ todo ->uuid ,
131- 'title ' => $ todo ->title ,
132- 'is_completed ' => (bool )$ todo ->is_completed ,
130+ 'uuid ' => $ todo ->uuid ,
131+ 'title ' => $ todo ->title ,
132+ 'is_completed ' => (bool ) $ todo ->is_completed ,
133133 'last_modified_at ' => optional ($ todo ->last_modified_at )->toIso8601String (),
134- 'deleted_at ' => optional ($ todo ->deleted_at )->toIso8601String (),
134+ 'deleted_at ' => optional ($ todo ->deleted_at )->toIso8601String (),
135135 ])->values (),
136136 'server_time ' => now ()->toIso8601String (),
137137 ]);
138138 }
139139
140140 protected function parseModifiedAt (array $ payload ): Carbon
141141 {
142- if (array_key_exists ('last_modified_at ' , $ payload ) && !empty ($ payload ['last_modified_at ' ])) {
142+ if (array_key_exists ('last_modified_at ' , $ payload ) && ! empty ($ payload ['last_modified_at ' ])) {
143143 return Carbon::parse ($ payload ['last_modified_at ' ]);
144144 }
145145
0 commit comments