Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create speaker controller #76

Merged
merged 4 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions app/Http/Controllers/SpeakerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace App\Http\Controllers;

use App\Speaker;
use App\Http\Requests\SpeakerRequest;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;

class SpeakerController extends Controller
{
use ApiTrait;
use CheckPermissionTrait;

/**
* @param $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$search = $request->input('search', '');
$order_field = $request->input('orderby_field', 'id');
$order_method = $request->input('orderby_method', 'desc');
if ($request->input('all', false)) {
$speaker = Speaker::orderBy($order_field, $order_method)->get();
} else {
$limit = $request->input('limit', 15);
$speaker = Speaker::Where(function ($query) use ($search) {
$query->orWhere('name', 'LIKE', '%' . $search . '%')
->orWhere('company', 'LIKE', '%' . $search . '%')
->orWhere('topic', 'LIKE', '%' . $search . '%');
})
->orderBy($order_field, $order_method)
->paginate($limit);
}

return $this->returnSuccess('Success.', $speaker);
}

/**
* Store a newly created resource in storage.
*
* @param SpeakerRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(SpeakerRequest $request)
{
$data = $request->only(['name', 'speaker_type']);
$data['last_edited_by'] = auth()->user()->name;
hashman marked this conversation as resolved.
Show resolved Hide resolved
$speaker = Speaker::create($data);

return $this->returnSuccess('Store success.', $speaker);
}

/**
* @param Speaker $speaker
* @return \Illuminate\Http\JsonResponse
*/
public function show(Speaker $speaker)
{
return $this->returnSuccess('Show success.', $speaker);
}

/**
* Update the specified resource in storage.
*
* @param SpeakerRequest $request
* @param Speaker $speaker
* @return \Illuminate\Http\JsonResponse
*/
public function update(SpeakerRequest $request, Speaker $speaker)
{
$data = $request->except('file', 'last_edited_by');
$data['last_edited_by'] = auth()->user()->name;
$speaker->update($data);

if ($request->hasFile('file')) {
$speaker = $this->savePhoto($request->file('file'), $speaker);
}

return $this->returnSuccess('Update success.', $speaker);
}

/**
* @param Speaker $speaker
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Speaker $speaker)
{
$speaker->delete();

return $this->returnSuccess('destroy success.');
}

/**
* @return \Illuminate\Http\JsonResponse
*/
public function getOptions()
{
$options = [
'tagItem' => Speaker::$tagItem,
'levelItem' => Speaker::$levelItem,
'licenseItem' => Speaker::$licenseItem,
'tshirtSizeItem' => Speaker::$tshirtSizeItem,
'mealPreferenceItem' => Speaker::$mealPreferenceItem,
'speakerStatusItem' => Speaker::$speakerStatusItem,
'speakerTypeItem' => Speaker::$speakerTypeItem,
];

return $this->returnSuccess('Success.', $options);
}

/**
* @param $accessKey
* @return \Illuminate\Http\JsonResponse
*/
public function externalShow($accessKey)
{
$speaker = Speaker::where('access_key', '=', $accessKey)->first();
return $this->returnSuccess('Success.', $speaker);
}

/**
* @param SpeakerRequest $request
* @param $accessKey
* @return \Illuminate\Http\JsonResponse
*/
public function externalUpdate(SpeakerRequest $request, $accessKey)
{
$speaker = Speaker::where('access_key', '=', $accessKey)->first();
if ($speaker) {
$data = $request->except('file', 'speaker_status', 'speaker_type', 'last_edited_by');
$data['last_edited_by'] = $speaker->name;
$speaker->update($data);

if ($request->hasFile('file')) {
$speaker = $this->savePhoto($request->file('file'), $speaker);
}
}

return $this->returnSuccess('Update success.', $speaker);
}


/**
* Update the specified resource in storage.
*
* @param UploadedFile $image
* @param Speaker $speaker
* @return Speaker
*/
public function savePhoto(UploadedFile $image, Speaker $speaker)
{
$newFileName = $speaker->name . '-' . Str::random(8) . '.' . $image->getClientOriginalExtension();
$image->move(public_path(Speaker::$photoPath), $newFileName);

$speaker->photo = Speaker::$photoPath . '/' . $newFileName;
$speaker->save();

return $speaker;
}
}
1 change: 1 addition & 0 deletions app/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ class VerifyCsrfToken extends Middleware
*/
protected $except = [
'/telegram/web/hook/*',
'/speaker/*',
];
}
97 changes: 97 additions & 0 deletions app/Http/Requests/SpeakerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Http\Requests;

use App\Speaker;
use Illuminate\Contracts\Validation\Validator;

class SpeakerRequest extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string',
'name_e' => 'nullable|string|max:100',
'company' => 'nullable|string|max:100',
'job_title' => 'nullable|string|max:100',
'bio' => 'nullable|string|max:120',
'bio_e' => 'nullable|string|max:240',
'file' => 'nullable|image',
'link_fb' => 'nullable|url',
'link_github' => 'nullable|url',
'link_twitter' => 'nullable|url',
'link_other' => 'nullable|url',
'topic' => 'nullable|string|max:32',
'topic_e' => 'nullable|string|max:64',
'summary' => 'nullable|string|max:240',
'summary_e' => 'nullable|string|max:480',
'tag' => 'nullable|array',
'level' => 'nullable|integer|min:0|max:' . count(Speaker::$levelItem),
'license' => 'nullable|integer|min:0|max:' . count(Speaker::$licenseItem),
'promotion' => 'nullable|boolean',
'tshirt_size' => 'nullable|integer|min:0|max:' . count(Speaker::$tshirtSizeItem),
'need_parking_space' => 'nullable|boolean',
'has_dinner' => 'nullable|boolean',
'meal_preference' => 'nullable|integer|min:0|max:' . count(Speaker::$mealPreferenceItem),
'has_companion' => 'nullable|integer|min:0|max:10',
'speaker_status' => 'nullable|integer|min:0|max:' . count(Speaker::$speakerStatusItem),
'speaker_type' => 'nullable|integer|min:0|max:' . count(Speaker::$speakerTypeItem),
'note' => 'nullable|string',
'last_edited_by' => 'nullable|string',
];
}

/**
* Get custom attributes for validator errors.
*
* @return array
*/
// public function attributes()
// {
// return [
// 'name' => '姓名',
// 'name_e' => '英文名稱',
// 'company' => '公司/組織',
// 'job_title' => '職稱',
// 'bio' => '個人介紹',
// 'bio_e' => '個人介紹(英文)',
// 'photo' => '照片',
// 'link_fb' => 'Facebook',
// 'link_github' => 'Github',
// 'link_twitter' => 'Twitter',
// 'link_other' => '其他(如Website/Blog)',
// 'topic' => '演講主題',
// 'topic_e' => '演講主題(英文)',
// 'summary' => '演講摘要',
// 'summary_e' => '演講摘要(英文)',
// 'tag' => '標籤',
// 'level' => '難易度',
// 'license' => '授權方式',
// 'promotion' => '是否同意公開宣傳',
// 'tshirt_size' => 'T-shirt 尺寸',
// 'need_parking_space' => '您是否需有停車需求',
// 'has_dinner' => '敬邀參加講者晚宴',
// 'meal_preference' => '葷素食偏好',
// 'has_companion' => '晚宴攜伴人數',
// 'speaker_status' => '狀態',
// 'speaker_type' => '類型',
// 'note' => '備註',
// 'last_edited_by' => '最後更新者',
// ];
// }
}
70 changes: 38 additions & 32 deletions app/Speaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class Speaker extends Model
{
public static $photoPath = '/images/speaker';
public static $tagItem = [
'AI',
'AR/VR',
Expand Down Expand Up @@ -48,40 +49,35 @@ class Speaker extends Model
'全素',
'奶蛋素',
];
public static $speakerStatusItem = [
'待確認',
'確認中',
'已確認',
'下架',
];
public static $speakerTypeItem = [
'贊助商',
'CFP',
'CFR',
'內推',
'其他',
];

protected $table = 'speakers';
protected $fillable = [
'name',
'name_e',
'company',
'job_title',
'bio',
'bio_e',
protected $guarded = [
'id',
'photo',
'link_fb',
'link_github',
'link_twitter',
'link_other',
'topic',
'topic_e',
'summary',
'summary_e',
'tag',
'level',
'license',
'promotion',
'tshirt_size',
'need_parking_space',
'has_dinner',
'meal_preference',
'has_companion',
'access_key',
'access_secret',
];
protected $appends = [
'tag_text',
'level_text',
'license_text',
'tshirt_size_text',
'meal_preference_text'
'meal_preference_text',
'speaker_status_text',
'speaker_type_text',
];
protected $casts = ['tag' => 'array'];

Expand All @@ -94,11 +90,11 @@ public static function boot()
});
}

public function getTaxTextAttribute()
public function getTagTextAttribute()
{
$tmp_collection = collect($this->tag);
$new_collection = $tmp_collection->map(function ($item) {
return $this->tagItem[$item] ?? '';
return self::$tagItem[$item] ?? '';
})->reject(function ($item) {
return empty($item);
});
Expand All @@ -108,21 +104,31 @@ public function getTaxTextAttribute()

public function getLevelTextAttribute()
{
return $this->levelItem[$this->level] ?? '';
return self::$levelItem[$this->level] ?? '';
}

public function getLicenseTextAttribute()
{
return $this->licenseItem[$this->license] ?? '';
return self::$licenseItem[$this->license] ?? '';
}

public function getTshirtTextAttribute()
public function getTshirtSizeTextAttribute()
{
return $this->tshirtSizeItem[$this->tshirt_size] ?? '';
return self::$tshirtSizeItem[$this->tshirt_size] ?? '';
}

public function getMealPreferenceTextAttribute()
{
return $this->mealPreferenceItem[$this->meal_preference] ?? '';
return self::$mealPreferenceItem[$this->meal_preference] ?? '';
}

public function getSpeakerStatusTextAttribute()
{
return self::$speakerStatusItem[$this->speaker_status] ?? '';
}

public function getSpeakerTypeTextAttribute()
{
return self::$speakerTypeItem[$this->speaker_type] ?? '';
}
}
Loading