Skip to content

Commit

Permalink
Merge pull request #30 from Newman101/BugFixCategories
Browse files Browse the repository at this point in the history
Bug Fixes
  • Loading branch information
jremes-foss committed Feb 22, 2020
2 parents be4197f + fe243d8 commit e1507e3
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 39 deletions.
11 changes: 9 additions & 2 deletions app/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
class Category extends Model
{
protected $table = 'categories';
protected $foreignKey = 'category_id';

protected $fillable = [
'category',
'description'
];

public function challenges() {
return $this->hasMany('App\Challenge');
public function challenge_categories()
{
return $this->hasMany('App\ChallengeCategory', 'category_id', 'id');
}

public function challenges()
{
return $this->hasMany('App\Challenge', 'challenge_id', 'id');
}
}
12 changes: 7 additions & 5 deletions app/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
class Challenge extends Model
{
protected $table = 'challenges';

protected $foreignKey = 'challenge_id';

protected $fillable = [
'category',
'score',
'title',
'flag',
'content'
];

public function attachments() {
public function attachments()
{
return $this->hasOne('App\Attachment');
}

public function categories() {
return $this->hasOne('App\Category');
public function challenge_categories()
{
return $this->hasOne('App\ChallengeCategory', 'challenge_id', 'id');
}
}
25 changes: 25 additions & 0 deletions app/ChallengeCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ChallengeCategory extends Model
{
protected $table = "challenge_category";

protected $fillable = [
'category_id',
'challenge_id'
];

public function challenges()
{
return $this->belongsTo('App\Challenge', 'challenge_id', 'id');
}

public function categories()
{
return $this->belongsTo('App\Category', 'category_id', 'id');
}
}
58 changes: 44 additions & 14 deletions app/Http/Controllers/ChallengesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Auth;
use App\Challenge;
use App\Category;
use App\ChallengeCategory;
use App\Solved;
use App\Score;
use App\Attachment;
Expand All @@ -17,7 +18,6 @@ class ChallengesController extends Controller
public function store(Request $request)
{
$challenge = array(
'category' => $request->get('inputCategory'),
'title' => $request->get('inputTitle'),
'score' => $request->get('inputScore'),
'flag' => $request->get('inputFlag'),
Expand All @@ -26,6 +26,24 @@ public function store(Request $request)

Challenge::create($challenge);

$category = array();
$category['category'] = $request->get('inputCategory');

if($request->has('inputCategory')) {
$get_challenge = Challenge::orderBy('updated_at', 'DESC')->first();
$get_category = Category::where('category', $category['category'])->first();
$challenge_id = $get_challenge->id;
$category_id = $get_category->id;
$category['challenge_id'] = $challenge_id;
$category['category_id'] = $category_id;
}

if(!empty($category)) {
// This needs to be unset so the DB can be inserted
unset($category['category']);
ChallengeCategory::create($category);
}

$attachment = array();

if($request->has('inputURL') || $request->has('inputFile')) {
Expand Down Expand Up @@ -67,9 +85,8 @@ public function create()

public function indexAdmin()
{
$challenges = Challenge::all();
return view('admin.challenges')
->with('challenges', $challenges);
$challenges = Challenge::with('challenge_categories.categories')->get();
return view('admin.challenges')->with('challenges', $challenges);
}

public function indexUser()
Expand All @@ -86,23 +103,29 @@ public function indexUser()
public function edit($id)
{
$attachments = Attachment::all();
$challenges = Challenge::find($id);
$categories = Category::all();
$challenges = Challenge::with('challenge_categories')->find($id);
$categories = Category::with('challenge_categories')->get();
$challenge_category = ChallengeCategory::where('challenge_id', $id)->first();
return view('admin.challenges.edit')
->with('attachments', $attachments)
->with('categories', $categories)
->with('challenge', $challenges);
->with('challenge', $challenges)
->with('challenge_category', $challenge_category);
}

public function update(Request $request, $id)
{
$challenge = Challenge::find($id);
$attachment = Attachment::find($id);
$challenge->category = $request->get('inputCategory');
$attachment = Attachment::where('challenge_id', $id)->first();

/** Updates the entry in challenge_category table */
$challenge_category = ChallengeCategory::where('challenge_id', $id)->first();
$challenge_category->category_id = $request->get('inputCategory');
$challenge->title = $request->get('inputTitle');
$challenge->score = $request->get('inputScore');
$challenge->flag = $request->get('inputFlag');
$challenge->content = $request->get('inputContent');

// Update the attachment file
if($request->hasFile('inputFile')) {
$attachment_file = $request->file('inputFile');
Expand All @@ -111,11 +134,17 @@ public function update(Request $request, $id)
$ext = $attachment_file->getClientOriginalExtension();
$attachment_file->storeAs($directory, $file);
}
// Update the attachment database entries
$attachment->url = $request->get('inputURL');

// Update the attachment and challenge database entries
if($request->has('inputURL')) {
$attachment->url = $request->get('inputURL');
}

$attachment->update();
$challenge->update();
return redirect()->route('user.challenges')->with('success', 'Challenge updated!');
$challenge_category->update();

return redirect()->route('admin.challenges')->with('message', 'Challenge updated!');
}

public function destroy($id)
Expand All @@ -140,9 +169,10 @@ public function submitFlag(Request $request)
$solved->user_id = $request->user()->id;
$solved->save();
$this->addScore($request);
return redirect('/challenges')->with('message', 'Correct Flag, Congratulations!');
return redirect('user.challenges')
->with('message', 'Correct Flag, Congratulations!');
} else {
return redirect('/challenges')->with('message', 'Try Again!');
return redirect('user.challenges')->with('message', 'Try Again!');
}
}

Expand Down
12 changes: 12 additions & 0 deletions database/factories/ChallengeCategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use Faker\Generator as Faker;

$factory->define(App\ChallengeCategory::class, function (Faker $faker) {
return [
'category_id' => 1,
'challenge_id' => 1
];
});
1 change: 0 additions & 1 deletion database/factories/ChallengeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

$factory->define(App\Challenge::class, function (Faker $faker) {
return [
'category' => 'Crypto',
'score' => '350',
'title' => $faker->sentence,
'flag' => 'FLAG{' . $faker->word . '}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public function up()
{
Schema::create('challenges', function (Blueprint $table) {
$table->increments('id');
$table->string('category');
$table->integer('score');
$table->string('title');
$table->string('flag');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChallengeCategory extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('challenge_category', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('challenge_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->foreign('challenge_id')->references('id')->on('challenges');
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('challenge_category');
}
}
11 changes: 10 additions & 1 deletion resources/views/admin/challenges.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
</div>
<div class="container">
<div class="row">
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
<a class="btn btn-primary" href="{{ route('admin.challenges.create') }}" role="button" style="float: right;">New Challenge</a>
<table class="table table-hover">
<thead>
Expand All @@ -23,7 +28,11 @@
@foreach($challenges as $challenge)
<tr>
<th scope="row">{{ $challenge->id }}</th>
<td>{{ $challenge->category }}</td>
<td><!-- Ugly way, refactor!! -->
{{
$challenge->join('challenge_category', 'challenge_category.challenge_id', '=', 'challenges.id')->join('categories', 'challenge_category.category_id', '=', 'categories.id')->select('categories.category')->where('challenge_category.challenge_id', '=', $challenge->id)->value('category')
}}
</td>
<td>{{ $challenge->score }}</td>
<td>{{ $challenge->title }}</td>
<td>{{ $challenge->flag }}</td>
Expand Down
4 changes: 3 additions & 1 deletion resources/views/admin/challenges/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<span class="input-group-addon"><i class="fa fa-address-book"></i></span>
<select class="form-control" id="inputCategory" name="inputCategory">
@foreach($categories as $category)
<option value="{{ $category->category }}">{{ $category->category }}</option>
<option value="{{ $challenge_category->category_id }}">
{{ $category->category }}
</option>
@endforeach
</select>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/challenges.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
{{ csrf_field() }}
<div class="from-inline">
<select name="category" class="form-control form-control-lg" id="choose_category">
<option>Select a category...</option>
@foreach($categories as $category)
<option>Select a category...</option>
<option value="{{ $category->category }}">{{ $category->category }}</option>
@endforeach
</select>
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/AttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ class AttachmentTest extends TestCase

public function testAttachmentRelationship()
{
$attachment = factory(Attachment::class)->make([
$attachment = factory(Attachment::class)->create([
'challenge_id' => 1,
'filename' => 'test.zip',
'url' => 'http://127.0.0.1'
]);

factory(\App\Challenge::class)->create([
'category' => 'Crypto',
'score' => '250',
'title' => 'TEST',
'flag' => 'FLAG{th1s_1s_4_t3stSt}',
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/CategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function testChallengesRelationship()
public function testHasMany()
{
factory(\App\Challenge::class)->create([
'category' => 'Crypto',
'score' => '250',
'title' => 'TEST',
'flag' => 'FLAG{th1s_1s_4_t3stSt}',
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/ChallengeCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Category;
use App\Challenge;
use App\ChallengeCategory;

class ChallengeCategoryTest extends TestCase
{
use RefreshDatabase;

public function testChallengeCategoryRelationship()
{
$challenge = factory(Challenge::class)->create([
'score' => '250',
'title' => 'TEST',
'flag' => 'FLAG{th1s_1s_4_t3st}',
'content' => 'This is a test.'
]);

$category = factory(Category::class)->create([
'category' => 'Crypto'
]);

$challenge_category = factory(ChallengeCategory::class)->create([
'category_id' => 1,
'challenge_id' => 1
]);

$challenge = Challenge::find(1);
$challenge_category = ChallengeCategory::find(1);

$this->assertEquals(1, $challenge->challenge_categories->challenge_id);
$this->assertEquals(1, $challenge->challenge_categories->category_id);

// Test categories relationship
$this->assertEquals('Crypto', $challenge->challenge_categories->categories->category);

// Test challenges relationship
$this->assertEquals(250, $challenge_category->challenges->score);
$this->assertEquals('TEST', $challenge_category->challenges->title);
$this->assertEquals('FLAG{th1s_1s_4_t3st}', $challenge_category->challenges->flag);
$this->assertEquals('This is a test.', $challenge_category->challenges->content);

}
}
Loading

0 comments on commit e1507e3

Please sign in to comment.