Skip to content

Commit 778a28f

Browse files
committed
Add delete education endpoint
1 parent d5fd56d commit 778a28f

File tree

8 files changed

+68
-14
lines changed

8 files changed

+68
-14
lines changed

app/Http/Controllers/Api/V1_0/VolunteerProfileController.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ public function updateEducationMe(UpdateEducationRequest $request)
157157
{
158158
$this->getVolunteerIdentifier();
159159

160+
/*
161+
* TODO: Need to check the id
162+
*/
160163
$id = StringUtil::getLastId($request->input('education_id'));
161164
$education = Education::findOrFail($id);
162-
//$educationVolunteer = $education->volunteer()->first();
163165

164166
// Check permission
165167
if (Gate::denies('update', $education)) {
@@ -172,6 +174,29 @@ public function updateEducationMe(UpdateEducationRequest $request)
172174
return response()->json(null, 204);
173175
}
174176

177+
/**
178+
* Delete volunteer's own education
179+
*
180+
* @param Integer $educationId
181+
*/
182+
public function deleteEducationMe($educationId)
183+
{
184+
$this->getVolunteerIdentifier();
185+
186+
$id = StringUtil::getLastId($educationId);
187+
$education = Education::findOrFail($id);
188+
189+
// Check permission
190+
if (Gate::denies('delete', $education)) {
191+
// Forbidden to delete the education record
192+
throw new AccessDeniedException();
193+
}
194+
195+
$education->delete();
196+
197+
return response()->json(null, 204);
198+
}
199+
175200
protected function getVolunteerIdentifier()
176201
{
177202
try {

app/Http/routes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
$api->get('/users/me', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@showMe');
4242
$api->post('/users/me/skills', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@updateSkillsMe');
4343
$api->post('/users/me/equipment', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@updateEquipmentMe');
44-
$api->post('/users/me/education', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@storeEducationMe');
45-
$api->put('/users/me/education', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@updateEducationMe');
44+
$api->post('/users/me/educations', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@storeEducationMe');
45+
$api->put('/users/me/educations', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@updateEducationMe');
46+
$api->delete('/users/me/educations/{education_id}', 'App\Http\Controllers\Api\V1_0\VolunteerProfileController@deleteEducationMe');
4647
$api->get('email_verification/{email_address}/{verification_code}',
4748
'App\Http\Controllers\Api\V1_0\VolunteerAuthController@emailVerification');
4849
});

app/Policies/VolunteerEducationPolicy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function update(Volunteer $volunteer, Education $education)
2121
{
2222
return $volunteer->id === $education->volunteer_id;
2323
}
24+
25+
public function delete(Volunteer $volunteer, Education $education)
26+
{
27+
return $volunteer->id === $education->volunteer_id;
28+
}
2429
}

config/mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
|
5555
*/
5656

57-
'from' => ['address' => env('MAIL_FROM', null), 'name' => env('MAIL_NAME', null)],
57+
'from' => ['address' => env('MAIL_FROM', 'vms@vms.app'), 'name' => env('MAIL_NAME', 'VMS')],
5858

5959
/*
6060
|--------------------------------------------------------------------------

tests/MiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ protected function factoryModel()
9797
$city->country()->associate($country);
9898
$city->save();
9999
}
100-
}
100+
}
101101
}
102102
}

tests/ValidatorUtilTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ public function testFormatterWithDifferentMessage()
5252

5353
$this->assertEquals('not_enough_password_strength', $actualNEPSFieldValidatorError->getCode());
5454
$this->assertContains('password', $actualNEPSFieldValidatorError->getFields());
55-
}
55+
}
5656
}

tests/VolunteerAuthControllerTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function setUp()
4040

4141
public function testJsonRequestValidation()
4242
{
43-
$validationErrorPostData =
43+
$validationErrorPostData =
4444
json_decode(file_get_contents(__DIR__ . '/examples/register_post_validation_error.json'), true);
4545
$expectedJsonResponseBody = [
4646
'message' => 'Validation failed',
@@ -82,7 +82,7 @@ public function testSuccessfulEmailVerification()
8282

8383
$token = JWTAuth::fromUser($volunteer);
8484

85-
$this->json('get',
85+
$this->json('get',
8686
'/api/email_verification/' . $volunteer->email . '/' . $code . '?token=' . $token,
8787
[],
8888
[
@@ -135,7 +135,6 @@ public function testSuccessfullyLogout()
135135
'X-VMS-API-Key' => $this->apiKey
136136
])
137137
->assertResponseStatus(204);
138-
139138
}
140139

141140
protected function factoryModel()
@@ -180,6 +179,6 @@ protected function factoryModel()
180179
$city->country()->associate($country);
181180
$city->save();
182181
}
183-
}
182+
}
184183
}
185184
}

tests/VolunteerProfileControllerTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function testSuccessfullyStoreEductionMe()
223223
'end_year' => 2014
224224
];
225225

226-
$this->json('post', '/api/users/me/education', $postData,
226+
$this->json('post', '/api/users/me/educations', $postData,
227227
[
228228
'Authorization' => 'Bearer ' . $token,
229229
'X-VMS-API-Key' => $this->apiKey
@@ -253,12 +253,14 @@ public function testSuccessfullyUpdateEducationMe()
253253
'end_year' => 2014
254254
];
255255

256-
$this->json('put', '/api/users/me/education', $putData,
256+
$this->json('put', '/api/users/me/educations', $putData,
257257
[
258258
'Authorization' => 'Bearer ' . $token,
259259
'X-VMS-API-Key' => $this->apiKey
260260
])
261261
->assertResponseStatus(204);
262+
263+
$this->seeInDatabase('educations', ['id' => $education->id, 'degree' => 4]);
262264
}
263265

264266
public function testUpdateEducationMeAccessDeniedException()
@@ -297,7 +299,7 @@ public function testUpdateEducationMeAccessDeniedException()
297299
'end_year' => '2013'
298300
];
299301

300-
$this->json('put', '/api/users/me/education', $putData,
302+
$this->json('put', '/api/users/me/educations', $putData,
301303
[
302304
'Authorization' => 'Bearer ' . $token,
303305
'X-VMS-API-Key' => $this->apiKey
@@ -309,7 +311,29 @@ public function testUpdateEducationMeAccessDeniedException()
309311
]]
310312
])
311313
->assertResponseStatus(403);
314+
}
315+
316+
public function testSuccessfullyDeleteEducationMe()
317+
{
318+
$this->factoryModel();
319+
320+
$volunteer = factory(App\Volunteer::class)->create();
321+
$volunteer->is_actived = true;
322+
323+
$education = factory(App\Education::class)->make();
324+
$volunteer->educations()->save($education);
312325

326+
$token = JWTAuth::fromUser($volunteer);
327+
328+
$this->json('delete',
329+
'/api/users/me/educations/' . $education->id
330+
,[]
331+
,[
332+
'Authorization' => 'Bearer ' . $token,
333+
'X-VMS-API-Key' => $this->apiKey
334+
])
335+
->assertResponseStatus(204);
336+
$this->notSeeInDatabase('educations', ['id' => $education->id]);
313337
}
314338

315339
protected function factoryModel()
@@ -354,6 +378,6 @@ protected function factoryModel()
354378
$city->country()->associate($country);
355379
$city->save();
356380
}
357-
}
381+
}
358382
}
359383
}

0 commit comments

Comments
 (0)