前幾篇部落格實作了使用者管理,從本篇部落格起,開始寫比賽報名系統
比賽報名系統
系統設計
功能:
1、管理者可以随便釋出比賽(删除比賽),使用者可以點選報名,管理者還可以設定給每個人單獨發比賽賬号
2、遊客不能進行報名,隻有注冊了才能報名,避免惡意報名
管理者登入退出
1、注冊路由
//管理者登入
$api->post('adminlogin','AdminsController@login')
->name('api.adminlogin.login');
//管理者退出
$api->post('adminlogout','AdminsController@logout')
->name('api.adminlogout.logout');
路由注冊完了之後,需要寫遷移檔案,用于存儲管理者賬号
php artisan make:migration create_admins_table –create=admins
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
執行指令,進行遷移
php artisan migrate
然後再寫一個模型檔案
php artisan make:model App\Models\Admin
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class Admin extends Model
{
//
use Notifiable;
protected $table = "admins";
protected $fillable = [
'username','password'
];
protected $hidden = [
'password',
];
}
最後再重新生成一個控制器
php artisan make:Controller Api\AdminsController
2、寫控制邏輯
public function login(Request $request){
$this->validate($request,[
'username' => 'required',
'password' => 'required',
]);
$password = $request->password;
$username = $request->username;
if($request->session()->has($username)){
return $this->response->array([
'info' => '您已經登入',
'status_code'=>400,
])->setStatusCode(200);
}else{
$admin = Admin::where('username',$username)->first();
if(md5($password)==$admin->password){
$adminkey = $username;
$key = 'adminlogin'.str_random(15);
$request->session()->put($adminkey,$key);
$request->session()->put($key,$adminkey);
return $this->response->array([
'key' => $key,
'info' => '登入成功',
'status_code' => 200,
])->setStatusCode(200);
}
}
}
public function logout(Request $request){
$this->validate($request,[
'verification_key' => 'required',
]);
$key = $request->verification_key;
$admin = $request->session()->get($key);
$request->session()->forget($key);
$request->session()->forget($admin);
return $this->response->array([
'info' => '登出成功',
'status_code' => 200,
])->setStatusCode(200);
}
3、測試
登陸成功
{
"key": "adminloginyF5TuBjYpG1vyAl",
"info": "登入成功",
"status_code": 200
}
登陸失敗
{
"info": "您已經登入",
"status_code": 400
}
///////
{
"info": "密碼錯誤",
"status_code": 200
}
登出成功
{
"info": "登出成功",
"status_code": 200
}
管理者管理比賽
管理比賽包括新增、删除、修改和查詢比賽資訊
比賽資訊:比賽全稱、負責人名字、負責人電話号碼、比賽時間、比賽地點、組織機關、比賽規則詳情、報名人數、最新公告、比賽結果表id、報名表id、比賽狀态(是否還能報名)
路由
//管理者管理比賽
$api->post('creategame','GamesController@create')
->name('api.creategame.create');
$api->post('deletegame','GamesController@delete')
->name('api.deletegame.delete');
$api->get('showgames','GamesController@show')
->name('api.showgames,show');
$api->post('udpategame','GamesController@update')
->name('api.updategame.update');
建立資料庫遷移和持久化模型
php artisan make:migration create_games_table –create=games
public function up()
{
Schema::create('games', function (Blueprint $table) {
$table->increments('id');
$table->string('gamename');
$table->string('fuzeren');
$table->string('fuzerenphone');
$table->dateTime('gametime');
$table->text('address');
$table->string('origanizetion');
$table->text('guize');
$table->integer('number');
$table->text('news');
$table->integer('resultid');
$table->integer('baomingid');
$table->integer('gamestatus');
$table->timestamps();
});
}
php artisan migrate
php artisan make:model App\Models\Game
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
class Game extends Model
{
use Notifiable;
protected $table = "games";
protected $fillable = [
'gamename','fuzeren','fuzerenphone','gametime','address','origanizetion','guize','number','news','resultid','baomingid','gamestatus', ];
protected $hidden = [
'fuzeren','fuzerenphone',
];
}
建立競賽管理控制器
php artisan make:Controller Api\GamesController
在建立新的遊戲的時候需要關聯一個表,報名表,是以需要用一個資料庫來記錄報名表最新是多少。
public function create(Request $request){
$this->validate($request,[
'key' => 'required',
]);
$key = $request->key;
if($request->session()->has($key)){
$game = Game::create([
'gamename' => $request->gamename,
'fuzeren' => $request->fuzeren,
'fuzerenphone' => $request->fuzerenphone,
'gametime' => $request->gametime,
'address' => $request->address,
'origanizetion' => $request->origanizetion,
'guize' => $request->guize,
'number' => 0,
'news' => $request->news,
'gamestatus' => 0,
]);
$baoming = Baoming::where('id',1)->first();
$game->baomingid = $baoming->number;
$game->save();
$baoming->number = $baoming->number+1;
$baoming->save();
return $this->response->array([
'info' => '釋出成功',
'status_code' => 200,
])->setStatusCode(201);
}else{
return $this->response->array([
'info' => '請登入後重試',
'status_code' => 400,
])->setStatusCode(200);
}
}
目前先實作建立比賽的功能,後續的管理功能再加
{
"info": "釋出成功",
"status_code": 200
}