forked from vidar-team/hctf_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c8d8bf
commit 2e64e93
Showing
5 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Bulletin extends Model | ||
{ | ||
protected $table = "bulletin"; | ||
protected $primaryKey = "bulletin_id"; | ||
public $timestamps = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use APIReturn; | ||
use App\Bulletin; | ||
use Illuminate\Http\Request; | ||
|
||
class BulletinController extends Controller | ||
{ | ||
/** | ||
* 获得全部公告 | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @author Eridanus Sora <[email protected]> | ||
*/ | ||
public function list(Request $request) | ||
{ | ||
try { | ||
return APIReturn::success(Bulletin::orderBy('bulletin_id', 'desc')->get()); | ||
} catch (\Exception $e) { | ||
return APIReturn::error("database_error", "数据库读写错误", 500); | ||
} | ||
} | ||
|
||
/** | ||
* 创建公告 | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @author Eridanus Sora <[email protected]> | ||
*/ | ||
public function create(Request $request) | ||
{ | ||
$validator = \Validator::make($request->only(['title', 'content']), [ | ||
'title' => 'required', | ||
'content' => 'required' | ||
], [ | ||
'title.required' => '缺少标题字段', | ||
'content.required' => '缺少内容字段' | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return APIReturn::error('invalid_parameters', $validator->errors()->all(), 400); | ||
} | ||
|
||
try { | ||
$bulletin = new Bulletin(); | ||
$bulletin->title = $request->input('title'); | ||
$bulletin->content = $request->input('content'); | ||
$bulletin->save(); | ||
return APIReturn::success($bulletin); | ||
} catch (\Exception $e) { | ||
dump($e); | ||
return APIReturn::error("database_error", "数据库读写错误", 500); | ||
} | ||
} | ||
|
||
/** | ||
* 删除公告 | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @author Eridanus Sora <[email protected]> | ||
*/ | ||
public function delete(Request $request) | ||
{ | ||
$validator = \Validator::make($request->only(['bulletinId']), [ | ||
'bulletinId' => 'required' | ||
], [ | ||
'bulletinId.required' => '缺少公告ID字段' | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return APIReturn::error('invalid_parameters', $validator->errors()->all(), 400); | ||
} | ||
|
||
try{ | ||
$bulletin = Bulletin::find($request->input('bulletinId')); | ||
if (!$bulletin){ | ||
return APIReturn::error("bulletin_not_found", "该公告不存在", 404); | ||
} | ||
$bulletin->delete(); | ||
return APIReturn::success(); | ||
} | ||
catch (\Exception $e){ | ||
return APIReturn::error("database_error", "数据库读写错误", 500); | ||
} | ||
} | ||
|
||
/** | ||
* 修改公告 | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
* @author Eridanus Sora <[email protected]> | ||
*/ | ||
public function edit(Request $request){ | ||
$validator = \Validator::make($request->only(['bulletinId', 'title', 'content']), [ | ||
'bulletinId' => 'required', | ||
'title' => 'required', | ||
'content' => 'required' | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return APIReturn::error('invalid_parameters', $validator->errors()->all(), 400); | ||
} | ||
|
||
try{ | ||
$bulletin = Bulletin::find($request->input('bulletinId')); | ||
if (!$bulletin){ | ||
return APIReturn::error("bulletin_not_found", "该公告不存在", 404); | ||
} | ||
$bulletin->title = $request->input('title'); | ||
$bulletin->content = $request->input('content'); | ||
$bulletin->save(); | ||
return APIReturn::success($bulletin); | ||
} | ||
catch (\Exception $e){ | ||
return APIReturn::error("database_error", "数据库读写错误", 500); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
database/migrations/2017_11_08_134052_add_bulletin_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddBulletinTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create("bulletin", function(Blueprint $table){ | ||
$table->increments("bulletin_id"); | ||
$table->string("title"); | ||
$table->text("content"); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters