diff --git a/plugins/ahmadfatoni/apigenerator/controllers/api/contactController.php b/plugins/ahmadfatoni/apigenerator/controllers/api/contactController.php index a9f3ef2..0ac87f3 100644 --- a/plugins/ahmadfatoni/apigenerator/controllers/api/contactController.php +++ b/plugins/ahmadfatoni/apigenerator/controllers/api/contactController.php @@ -39,25 +39,63 @@ class contactController extends Controller } - public function store(Request $request){ + // public function store(Request $request){ - $arr = $request->all(); + // $arr = $request->all(); - while ( $data = current($arr)) { - $this->Contacts->{key($arr)} = $data; - next($arr); - } + // while ( $data = current($arr)) { + // $this->Contacts->{key($arr)} = $data; + // next($arr); + // } - $validation = Validator::make($request->all(), $this->Contacts->rules); + // $validation = Validator::make($request->all(), $this->Contacts->rules); - if( $validation->passes() ){ - $this->Contacts->save(); - return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Contacts->id]); - }else{ - return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() ); - } + // if( $validation->passes() ){ + // $this->Contacts->save(); + // return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Contacts->id]); + // }else{ + // return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors() ); + // } + // } + + public function store(Request $request) + { + $arr = $request->all(); + // dd($arr); + + // Assign request data to the Contacts model + foreach ($arr as $key => $value) { + if ($key != 'file') { + $this->Contacts->{$key} = $value; + } + } + + // Validate the request data against the model's rules + $validation = Validator::make($request->all(), $this->Contacts->rules); + + if ($validation->fails()) { + return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validation->errors()); + } + + // Save the Contacts model + $this->Contacts->save(); + + // Handle file upload and attachment + if ($request->hasFile('file')) { + $file = $request->file('file'); + $uploadedFile = new \System\Models\File; + $uploadedFile->data = $file; + $uploadedFile->save(); + + // Attach the file to the Contacts model using attachOne + $this->Contacts->file()->add($uploadedFile); + } + + return $this->helpers->apiArrayResponseBuilder(201, 'created', ['id' => $this->Contacts->id]); } + + public function update($id, Request $request){