contactModel = new ContactModel(); $this->contactRule = [ 'NameFirst' => 'required' ]; } public function index() { $model = new ContactModel(); $rows = $model->getContactsWithDetail(); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "no Data.", 'data' => $rows, ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "fetch success", 'data' => $rows, ], 200); } public function show($ContactID = null) { $model = new ContactModel(); $rows = $model->getContactWithDetail($ContactID); if (empty($rows)) { return $this->respond([ 'status' => 'success', 'message' => "Data not found.", 'data' => [], ], 200); } return $this->respond([ 'status' => 'success', 'message'=> "Data fetched successfully", 'data' => $rows, ], 200); } public function create() { $input = $this->request->getJSON(true); $dataContact = $this->prepareContactData($input); $dataContactDetail = $this->prepareContactDetailData($input); if (!$this->validateData($dataContact, $this->contactRule)) { return $this->failValidationErrors($this->validator->getErrors()); } try{ $result = $this->contactModel->create($dataContact, $dataContactDetail); if ($result) { return $this->respondCreated([ 'status' => 'success', 'message' => 'Contact created successfully', 'data' => $dataContact, ]); } else { return $this->failServerError('Failed to create contact'); } } catch (\Throwable $e) { return $this->failServerError('Error: ' . $e->getMessage()); } } public function update() { $input = $this->request->getJSON(true); $ContactID = $input['ContactID']; $dataContact = $this->prepareContactData($input); $dataContactDetail = $this->prepareContactDetailData($input); if (!$this->validateData($dataContact, $this->contactRule)) { return $this->failValidationErrors( $this->validator->getErrors()); } try{ $result = $this->contactModel->update($ContactID, $dataContact, $dataContactDetail); if ($result) { return $this->respondCreated([ 'status' => 'success', 'message' => 'Contact updated successfully', 'data' => $dataContact, ]); } else { return $this->failServerError('Failed to create contact'); } } catch (\Throwable $e) { return $this->failServerError('Error: ' . $e->getMessage()); } } public function delete() { try { $input = $this->request->getJSON(true); $ContactID = $input["ContactID"]; if (!$ContactID) { return $this->failValidationError('ContactID is required.'); } $contact = $this->db->table('contact')->where('ContactID', $ContactID)->get()->getRow(); if (!$contact) { return $this->failNotFound("data with {$ContactID} not found."); } $this->db->table('contact')->where('ContactID', $ContactID)->update(['EndDate' => NOW()]); return $this->respondDeleted([ 'status' => 'success', 'message' => "Contact with {$ContactID} deleted successfully." ]); } catch (\Throwable $e) { // Ensure rollback if something goes wrong if ($this->db->transStatus() !== false) { $this->db->transRollback(); } return $this->failServerError('Something went wrong: ' . $e->getMessage()); } } private function prepareContactData(array $input): array { $data = [ "NameFirst" => $input['NameFirst'] ?? null, "NameLast" => $input['NameLast'] ?? null, "Title" => $input['Title'] ?? null, "Initial" => $input['Initial'] ?? null, "Birthdate" => $input['Birthdate'] ?? null, "EmailAddress1" => $input['EmailAddress1'] ?? null, "EmailAddress2" => $input['EmailAddress2'] ?? null, "Phone" => $input["Phone"] ?? null, "MobilePhone1" => $input["MobilePhone1"] ?? null, "MobilePhone2" => $input["MobilePhone2"] ?? null, "Specialty" => $input["Specialty"] ?? null, "SubSpecialty" => $input["SubSpecialty"] ?? null, ]; if(!empty($input["ContactID"])) { $data["ContactID"] = $input["ContactID"]; } return $data; } private function prepareContactDetailData(array $input): array { foreach($input['ContactDetail'] as $detail) { $data[] = [ "SiteID" => $detail['SiteID'] ?? null, "ContactCode" => $detail['ContactCode'] ?? null, "ContactEmail" => $detail['ContactEmail'] ?? null, "OccupationID" => $detail['OccupationID'] ?? null, "JobTitle" => $detail['JobTitle'] ?? null, "Department" => $detail['Department'] ?? null, "ContactStartDate" => $detail['ContactStartDate'] ?? null, "ContactEndDate" => $detail['ContactEndDate'] ?? null, ]; } return $data; } }