Added new endpoint upload speaker photo

PUT /api/v1/speakers/{speaker_id}/photo

Change-Id: I02bd88c12194ae00f540adfa025ad8a87a9f7924
This commit is contained in:
Sebastian Marcet
2017-12-12 08:52:31 -03:00
parent 5dd7f6e4fc
commit 3d586f2924
7 changed files with 111 additions and 9 deletions

View File

@@ -11,7 +11,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Exception;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
@@ -32,7 +31,7 @@ use utils\FilterParser;
use utils\FilterParserException;
use utils\OrderParser;
use utils\PagingInfo;
use Illuminate\Http\Request as LaravelRequest;
/**
* Class OAuth2SummitSpeakersApiController
* @package App\Http\Controllers
@@ -378,4 +377,37 @@ final class OAuth2SummitSpeakersApiController extends OAuth2ProtectedController
return $this->error500($ex);
}
}
public function addSpeakerPhoto(LaravelRequest $request, $speaker_id){
try {
$file = $request->file('file');
if (is_null($file)) {
return $this->error412(array('file param not set!'));
}
$res = $this->service->addSpeakerAttachment($speaker_id, $file);
return !is_null($res) ? $this->created($res->getId()) : $this->error400();
}
catch (EntityNotFoundException $ex1) {
Log::warning($ex1);
return $this->error404();
}
catch(ValidationException $ex2)
{
Log::warning($ex2);
return $this->error412(array($ex2->getMessage()));
}
catch(\HTTP401UnauthorizedException $ex3)
{
Log::warning($ex3);
return $this->error401();
}
catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@@ -1,5 +1,4 @@
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
@@ -10,7 +9,6 @@
| and give it the controller to call when that URI is requested.
|
*/
use Illuminate\Support\Facades\Config;
// public api ( without AUTHZ [OAUTH2.0])
@@ -215,7 +213,7 @@ Route::group([
Route::put('/publish', [ 'middleware' => 'auth.user:administrators', 'uses' => 'OAuth2SummitEventsApiController@publishEvent']);
Route::delete('/publish', [ 'middleware' => 'auth.user:administrators', 'uses' => 'OAuth2SummitEventsApiController@unPublishEvent']);
Route::post('/feedback', 'OAuth2SummitEventsApiController@addEventFeedback');
Route::post('/attachment', 'OAuth2SummitEventsApiController@addEventAttachment');
Route::post('/attachment', [ 'middleware' => 'auth.user:administrators', 'uses' => 'OAuth2SummitEventsApiController@addEventAttachment']);
Route::get('/feedback/{attendee_id?}', ['middleware' => 'cache:'.Config::get('cache_api_response.get_event_feedback_response_lifetime', 300), 'uses' => 'OAuth2SummitEventsApiController@getEventFeedback'] )->where('attendee_id', 'me|[0-9]+');
});
});
@@ -309,6 +307,12 @@ Route::group([
// speakers
Route::group(array('prefix' => 'speakers'), function () {
Route::get('', 'OAuth2SummitSpeakersApiController@getAll');
Route::group(['prefix' => '{speaker_id}'], function () {
Route::post('/photo', [ 'middleware' => 'auth.user:administrators', 'uses' => 'OAuth2SummitSpeakersApiController@addSpeakerPhoto']);
});
});
});

View File

@@ -109,7 +109,7 @@ class PresentationSpeaker extends SilverstripeBaseModel
private $moderated_presentations;
/**
* @ORM\ManyToOne(targetEntity="models\main\File")
* @ORM\ManyToOne(targetEntity="models\main\File", cascade={"persist"})
* @ORM\JoinColumn(name="PhotoID", referencedColumnName="ID")
* @var File
*/
@@ -343,6 +343,14 @@ class PresentationSpeaker extends SilverstripeBaseModel
return $this->photo;
}
/**
* @param File $photo
*/
public function setPhoto(File $photo)
{
$this->photo = $photo;
}
/**
* @return Member
*/

View File

@@ -11,10 +11,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\File;
use models\summit\PresentationSpeaker;
use models\summit\SpeakerSummitRegistrationPromoCode;
use models\summit\Summit;
use Illuminate\Http\UploadedFile;
/**
* Interface ISpeakerService
* @package services\model
@@ -46,4 +49,14 @@ interface ISpeakerService
* @throws ValidationException
*/
public function registerSummitPromoCodeByValue(PresentationSpeaker $speaker, Summit $summit, $reg_code);
/**
* @param int $speaker_id
* @param UploadedFile $file
* @param int $max_file_size
* @throws ValidationException
* @throws EntityNotFoundException
* @return File
*/
public function addSpeakerAttachment($speaker_id, UploadedFile $file, $max_file_size = 10485760);
}

View File

@@ -22,9 +22,7 @@ use models\summit\SummitAttendee;
use models\summit\SummitEvent;
use models\summit\SummitEventFeedback;
use models\summit\SummitScheduleEmptySpot;
use DateTime;
use utils\Filter;
/**
* Interface ISummitService
* @package services\model

View File

@@ -11,9 +11,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Illuminate\Http\UploadedFile;
use libs\utils\ITransactionService;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\File;
use models\main\IEmailCreationRequestRepository;
use models\main\IFolderRepository;
use models\main\IMemberRepository;
@@ -26,7 +28,7 @@ use models\summit\PresentationSpeakerSummitAssistanceConfirmationRequest;
use models\summit\SpeakerRegistrationRequest;
use models\summit\SpeakerSummitRegistrationPromoCode;
use models\summit\Summit;
use App\Http\Utils\FileUploader;
/**
* Class SpeakerService
* @package services\model
@@ -335,4 +337,41 @@ final class SpeakerService implements ISpeakerService
return $speaker;
});
}
/**
* @param int $speaker_id
* @param UploadedFile $file
* @param int $max_file_size
* @throws ValidationException
* @throws EntityNotFoundException
* @return File
*/
public function addSpeakerAttachment($speaker_id, UploadedFile $file, $max_file_size = 10485760)
{
return $this->tx_service->transaction(function () use ($speaker_id, $file, $max_file_size) {
$allowed_extensions = ['png','jpg','jpeg','gif','pdf'];
$speaker = $this->speaker_repository->getById($speaker_id);
if (is_null($speaker)) {
throw new EntityNotFoundException('speaker not found!');
}
if(!in_array($file->extension(), $allowed_extensions)){
throw new ValidationException("file does not has a valid extension ('png','jpg','jpeg','gif','pdf').");
}
if($file->getSize() > $max_file_size)
{
throw new ValidationException(sprintf( "file exceeds max_file_size (%s MB).", ($max_file_size/1024)/1024));
}
$uploader = new FileUploader($this->folder_repository);
$photo = $uploader->build($file, 'profile-images');
$speaker->setPhoto($photo);
return $photo;
});
}
}

View File

@@ -173,6 +173,14 @@ class ApiEndpointsSeeder extends Seeder
sprintf(SummitScopes::WriteSpeakersData, $current_realm),
],
),
array(
'name' => 'add-speaker-photo',
'route' => '/api/v1/speakers/{speaker_id}/photo',
'http_method' => 'POST',
'scopes' => [
sprintf(SummitScopes::WriteSpeakersData, $current_realm),
],
),
array(
'name' => 'get-all-speakers',
'route' => '/api/v1/speakers',