@@ -36,7 +36,7 @@ public function register(SiteRequest $request): JsonResponse
36
36
37
37
$ connectionService = App::makeWith (
38
38
Connection::class,
39
- ["baseUrl " => $ url , "key " => $ key ]
39
+ ["baseUrl " => rtrim ( $ url, " / " ) , "key " => $ key ]
40
40
);
41
41
42
42
// Do a health check
@@ -48,11 +48,11 @@ public function register(SiteRequest $request): JsonResponse
48
48
return $ this ->error ($ e ->getMessage (), 500 );
49
49
}
50
50
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 ();
53
53
54
54
$ site ->key = $ key ;
55
- $ site ->url = rtrim ( $ url, " / " ) ;
55
+ $ site ->url = $ url ;
56
56
$ site ->last_seen = Carbon::now ();
57
57
58
58
// Fill with site info
@@ -75,15 +75,20 @@ public function check(SiteRequest $request): JsonResponse
75
75
$ url = $ request ->string ('url ' );
76
76
$ key = $ request ->string ('key ' );
77
77
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
+ }
79
84
80
85
// Do a health check
81
86
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 ) {
84
91
return $ this ->error ($ e ->getMessage (), 500 );
85
- } catch (ClientException |\Exception $ e ) {
86
- return $ this ->error ($ e ->getMessage ());
87
92
}
88
93
89
94
return $ this ->ok ();
@@ -100,11 +105,13 @@ public function delete(SiteRequest $request): JsonResponse
100
105
$ key = $ request ->string ('key ' );
101
106
102
107
try {
103
- Site::where ('url ' , $ url )->where ('key ' , $ key )->delete ();
108
+ $ site = Site::where ('url ' , $ url )->where ('key ' , $ key )->firstOrFail ();
104
109
} catch (\Exception $ e ) {
105
- return $ this ->error ($ e -> getMessage () );
110
+ return $ this ->error (" Not found " , 404 );
106
111
}
107
112
113
+ $ site ->delete ();
114
+
108
115
return $ this ->ok ();
109
116
}
110
117
}
0 commit comments