@@ -47,7 +47,7 @@ public function create(array $data)
4747 $ validator = Validator::make ($ data , [
4848 'name ' => 'required|string|min:1|max:255 ' ,
4949 'description ' => 'required|string ' ,
50- 'file ' => 'required|regex:/^[\w.-]{1,50}$/ ' ,
50+ 'file ' => 'required|unique:services,file| regex:/^[\w.-]{1,50}$/ ' ,
5151 'executable ' => 'max:255|regex:/^(.*)$/ ' ,
5252 'startup ' => 'string '
5353 ]);
@@ -56,15 +56,23 @@ public function create(array $data)
5656 throw new DisplayValidationException ($ validator ->errors ());
5757 }
5858
59- if (Models \Service::where ('file ' , $ data ['file ' ])->first ()) {
60- throw new DisplayException ('A service using that configuration file already exists on the system. ' );
61- }
62-
6359 $ data ['author ' ] = env ('SERVICE_AUTHOR ' , (string ) Uuid::generate (4 ));
6460
6561 $ service = new Models \Service ;
66- $ service ->fill ($ data );
67- $ service ->save ();
62+ DB ::beginTransaction ();
63+
64+ try {
65+ $ service ->fill ($ data );
66+ $ service ->save ();
67+
68+ Storage::put ('services/ ' . $ data ['file ' ] . '/main.json ' , '{} ' );
69+ Storage::copy ('services/.templates/index.js ' , 'services/ ' . $ data ['file ' ] . '/index.js ' );
70+
71+ DB ::commit ();
72+ } catch (\Exception $ ex ) {
73+ DB ::rollBack ();
74+ throw $ ex ;
75+ }
6876
6977 return $ service ->id ;
7078 }
@@ -101,11 +109,11 @@ public function delete($id)
101109
102110 DB ::beginTransaction ();
103111 try {
104- Storage::deleteDirectory ('services/ ' . $ service ->file );
105-
106112 Models \ServiceVariables::whereIn ('option_id ' , $ options ->get ()->toArray ())->delete ();
107113 $ options ->delete ();
108114 $ service ->delete ();
115+
116+ Storage::deleteDirectory ('services/ ' . $ service ->file );
109117 DB ::commit ();
110118 } catch (\Exception $ ex ) {
111119 DB ::rollBack ();
0 commit comments