@@ -36,7 +36,7 @@ public function register(SiteRequest $request): JsonResponse
3636
3737 $ connectionService = App::makeWith (
3838 Connection::class,
39- ["baseUrl " => $ url , "key " => $ key ]
39+ ["baseUrl " => rtrim ( $ url, " / " ) , "key " => $ key ]
4040 );
4141
4242 // Do a health check
@@ -48,11 +48,11 @@ public function register(SiteRequest $request): JsonResponse
4848 return $ this ->error ($ e ->getMessage (), 500 );
4949 }
5050
51- // If successful save site
52- $ site = new Site ();
51+ // If successful create or update site
52+ $ site = Site:: where ( ' url ' , $ url )-> where ( ' key ' , $ key )-> first () ?? new Site ();
5353
5454 $ site ->key = $ key ;
55- $ site ->url = rtrim ( $ url, " / " ) ;
55+ $ site ->url = $ url ;
5656 $ site ->last_seen = Carbon::now ();
5757
5858 // Fill with site info
@@ -75,15 +75,20 @@ public function check(SiteRequest $request): JsonResponse
7575 $ url = $ request ->string ('url ' );
7676 $ key = $ request ->string ('key ' );
7777
78- $ connectionService = new Connection ($ url , $ key );
78+ try {
79+ /** @var Site $site */
80+ $ site = Site::where ('url ' , $ url )->where ('key ' , $ key )->firstOrFail ();
81+ } catch (\Exception $ e ) {
82+ return $ this ->error ("Not found " , 404 );
83+ }
7984
8085 // Do a health check
8186 try {
82- $ connectionService ->checkHealth ();
83- } catch (ServerException $ e ) {
87+ $ site ->connection ->checkHealth ();
88+ } catch (ServerException |ClientException $ e ) {
89+ return $ this ->error ($ e ->getMessage (), 400 );
90+ } catch (\Exception $ e ) {
8491 return $ this ->error ($ e ->getMessage (), 500 );
85- } catch (ClientException |\Exception $ e ) {
86- return $ this ->error ($ e ->getMessage ());
8792 }
8893
8994 return $ this ->ok ();
@@ -100,11 +105,13 @@ public function delete(SiteRequest $request): JsonResponse
100105 $ key = $ request ->string ('key ' );
101106
102107 try {
103- Site::where ('url ' , $ url )->where ('key ' , $ key )->delete ();
108+ $ site = Site::where ('url ' , $ url )->where ('key ' , $ key )->firstOrFail ();
104109 } catch (\Exception $ e ) {
105- return $ this ->error ($ e -> getMessage () );
110+ return $ this ->error (" Not found " , 404 );
106111 }
107112
113+ $ site ->delete ();
114+
108115 return $ this ->ok ();
109116 }
110117}
0 commit comments