From a66242feff30bdc8d0885c706614e6befa875a19 Mon Sep 17 00:00:00 2001 From: "fidrafid@gmail.com" <fidrafid@gmail.com> Date: Mon, 9 Apr 2018 06:39:53 +0700 Subject: [PATCH] Upate Create, Update, Delete on Profile Page --- app/Education.php | 2 +- app/Employment.php | 2 +- app/Experience.php | 2 +- .../Profile/ProfileAPIController.php | 188 ++++++++++++++++-- app/Performance.php | 2 +- ...018_04_05_034810_create_profiles_table.php | 2 +- ...04_07_004142_create_performances_table.php | 2 +- ...18_04_07_014150_create_education_table.php | 4 +- 8 files changed, 177 insertions(+), 27 deletions(-) diff --git a/app/Education.php b/app/Education.php index 4e751a3..a4bbf9a 100644 --- a/app/Education.php +++ b/app/Education.php @@ -13,5 +13,5 @@ class Education extends Model 'major', 'start_year', 'end_year' - ] + ]; } diff --git a/app/Employment.php b/app/Employment.php index 2308e33..9d659f9 100644 --- a/app/Employment.php +++ b/app/Employment.php @@ -13,5 +13,5 @@ class Employment extends Model 'position', 'start_year', 'end_year' - ] + ]; } diff --git a/app/Experience.php b/app/Experience.php index 9a3ca5f..8ac170c 100644 --- a/app/Experience.php +++ b/app/Experience.php @@ -12,5 +12,5 @@ class Experience extends Model 'position', 'start_year', 'end_year' - ] + ]; } diff --git a/app/Http/Controllers/Profile/ProfileAPIController.php b/app/Http/Controllers/Profile/ProfileAPIController.php index ab60fd7..feaf4bf 100644 --- a/app/Http/Controllers/Profile/ProfileAPIController.php +++ b/app/Http/Controllers/Profile/ProfileAPIController.php @@ -11,7 +11,9 @@ use App\Employment; use App\Experience; use App\Performance; use App\Profile; +use App\User; use Validator; +use Illuminate\Support\Facades\Hash; class ProfileAPIController extends APIBaseController @@ -41,21 +43,39 @@ class ProfileAPIController extends APIBaseController $validator = Validator::make($input, [ 'name' => 'required', + 'email' => 'required', + 'password' => 'required', 'nip' => 'required', 'birth_place' => 'required', - 'birth_date' => 'required' + 'birth_date' => 'required', ]); - if($validator->fails()){ return $this->sendError('Validation Error.', $validator->errors()); } + $find = User::where('email', $input['email'])->count(); - $post = Profile::create($input); + if($find != 0){ + return $this->sendError('Email Already Exist'); + } + $postUser = User::create([ + 'name' => $input['name'], + 'email' => $input['email'], + 'password' => Hash::make($input['password']), + ]); - return $this->sendResponse($post->toArray(), 'Profile created successfully.'); + $postProfile = Profile::create([ + 'name' => $input['name'], + 'nip' => $input['nip'], + 'birth_place' => $input['birth_place'], + 'birth_date' => $input['birth_date'], + ]); + + $post = array_merge($postUser->toArray(), $postProfile->toArray()); + + return $this->sendResponse($post, 'User created successfully.'); } @@ -90,34 +110,129 @@ class ProfileAPIController extends APIBaseController { $input = $request->all(); - $validator = Validator::make($input, [ 'name' => 'required', + 'email' => 'required', + 'password' => 'required', 'nip' => 'required', - 'birth_place' => 'required', - 'birth_date' => 'required', ]); - if($validator->fails()){ return $this->sendError('Validation Error.', $validator->errors()); } + + $post = User::find($id); - - $post = Profile::find($id); if (is_null($post)) { return $this->sendError('Profile not found.'); } + $user = User::find($id); + $profile = Profile::find($id); + + if(!is_null($input['name'])){ + $user->name = $input['name']; + $profile->name = $input['name']; + } + + if(!is_null($input['email'])){ + $user->email = $input['email']; + } + + if(!is_null($input['password'])){ + $user->password = Hash::make($input['password']); + } + + if(!is_null($input['nip'])){ + $profile->nip = $input['nip']; + } + + if(!is_null($input['birth_place'])){ + $profile->birth_place = $input['birth_place']; + } + + if(!is_null($input['birth_date'])){ + $profile->birth_date = $input['birth_date']; + } + + $performance = Performance::where('user_id', $user->id); - $post->name = $input['name']; - $post->nip = $input['nip']; - $post->birth_place = $input['birth_place']; - $post->birth_date = $input['birth_date']; - $post->save(); + if($performance->count() > 0){ + $performance->delete(); + } + + for($i = 1; $i <= $input['performance_counter']; $i++){ + $postPerformance = Performance::create([ + 'user_id' => $user->id, + 'date' => $input['performance_date_' . $i], + 'purpose' => $input['performance_purpose_' . $i], + 'performance_report' => $input['performance_report_' . $i], + ]); + } + + $education = Education::where('user_id', $user->id); + + if($education->count() > 0){ + $education->delete(); + } + + for($i = 1; $i <= $input['education_counter']; $i++){ + $postEducation = Education::create([ + 'user_id' => $user->id, + 'degree' => $input['education_degree_' . $i], + 'institution' => $input['education_institution_' . $i], + 'major' => $input['education_major_' . $i], + 'start_year' => $input['education_start_year_' . $i], + 'end_year' => $input['education_end_year_' . $i], + ]); + } + $employment = Employment::where('user_id', $user->id); - return $this->sendResponse($post->toArray(), 'Profile updated successfully.'); + if($employment->count() > 0){ + $employment->delete(); + } + + for($i = 1; $i <= $input['employment_counter']; $i++){ + $postEmployment = Employment::create([ + 'user_id' => $user->id, + 'competency' => $input['employment_competency_' . $i], + 'unit' => $input['employment_unit_' . $i], + 'position' => $input['employment_position_' . $i], + 'start_year' => $input['employment_start_year_' . $i], + 'end_year' => $input['employment_end_year_' . $i], + ]); + } + + $experience = Experience::where('user_id', $user->id); + + if($experience->count() > 0){ + $experience->delete(); + } + + for($i = 1; $i <= $input['experience_counter']; $i++){ + $postExperience = Experience::create([ + 'user_id' => $user->id, + 'institution' => $input['experience_institution_' . $i], + 'position' => $input['experience_position_' . $i], + 'start_year' => $input['experience_start_year_' . $i], + 'end_year' => $input['experience_end_year_' . $i], + ]); + } + + $user->save(); + $profile->save(); + + $data = array_merge( + $user->toArray(), + $profile->toArray(), + $performance->get()->toArray(), + $education->get()->toArray(), + $employment->get()->toArray(), + $experience->get()->toArray() + ); + + return $this->sendResponse($data, 'Profile updated successfully.'); } @@ -129,15 +244,50 @@ class ProfileAPIController extends APIBaseController */ public function destroy($id) { - $post = Profile::find($id); + $user = User::find($id); + + if (is_null($user)) { + return $this->sendError('Profile not found.'); + } + $user->delete(); - if (is_null($post)) { + + $profile = Profile::find($id); + + if (is_null($profile)) { return $this->sendError('Profile not found.'); } + $profile->delete(); + + + $performance = Performance::where('user_id', $user->id); + + if($performance->count() > 0){ + $performance->delete(); + } + + + $education = Education::where('user_id', $user->id); + + if($education->count() > 0){ + $education->delete(); + } + + + $employment = Employment::where('user_id', $user->id); + + if($employment->count() > 0){ + $employment->delete(); + } + - $post->delete(); + $experience = Experience::where('user_id', $user->id); + + if($experience->count() > 0){ + $experience->delete(); + } return $this->sendResponse($id, 'Tag deleted successfully.'); diff --git a/app/Performance.php b/app/Performance.php index fa404b4..c2f9991 100644 --- a/app/Performance.php +++ b/app/Performance.php @@ -11,5 +11,5 @@ class Performance extends Model 'date', 'purpose', 'performance_report' - ] + ]; } diff --git a/database/migrations/2018_04_05_034810_create_profiles_table.php b/database/migrations/2018_04_05_034810_create_profiles_table.php index 0b66ed9..8b3a1c9 100644 --- a/database/migrations/2018_04_05_034810_create_profiles_table.php +++ b/database/migrations/2018_04_05_034810_create_profiles_table.php @@ -14,7 +14,7 @@ class CreateProfilesTable extends Migration public function up() { Schema::create('profiles', function (Blueprint $table) { - $table->increments('user_id'); + $table->increments('id'); $table->string('name', 200); $table->string('nip', 200); $table->string('birth_place', 200); diff --git a/database/migrations/2018_04_07_004142_create_performances_table.php b/database/migrations/2018_04_07_004142_create_performances_table.php index 82790c2..4188bf2 100644 --- a/database/migrations/2018_04_07_004142_create_performances_table.php +++ b/database/migrations/2018_04_07_004142_create_performances_table.php @@ -18,7 +18,7 @@ class CreatePerformancesTable extends Migration $table->integer('user_id'); $table->string('date', 200); $table->string('purpose', 250); - $table->string('performace_report', 300); + $table->string('performance_report', 300); $table->timestamps(); }); } diff --git a/database/migrations/2018_04_07_014150_create_education_table.php b/database/migrations/2018_04_07_014150_create_education_table.php index 9507dc7..5e8ef80 100644 --- a/database/migrations/2018_04_07_014150_create_education_table.php +++ b/database/migrations/2018_04_07_014150_create_education_table.php @@ -18,8 +18,8 @@ class CreateEducationTable extends Migration $table->string('degree', 200); $table->string('institution', 200); $table->string('major', 200); - $table->string('start_date', 200); - $table->string('end_date', 200); + $table->string('start_year', 200); + $table->string('end_year', 200); $table->timestamps(); }); } -- GitLab