Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
Start rework project structure.
Browse files Browse the repository at this point in the history
Laravel is MVC Framework written in OOP style.
We have Eloquent ORM -> 1 table - 1 model, already written helpers to work with responses, requests, post/get parameters, good routing system with many sweet things, learn and use it.
 Make peoples happy :P

 Create default API structure (url, version, logical group, methods), let's talk about it! :)
 Move working with DB to ORM
 Move logic to controller

 achievement - now support getting by id
 achievement categories - now support id like URI part

 Add IDE helper package
  • Loading branch information
avengerweb committed Mar 25, 2016
1 parent 1ad12f9 commit 9056188
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 39 deletions.
4 changes: 4 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ if you don't have [git](http://git-scm.com/) installed, you can [download it dir

- Copy the file **.env.example** to **.env**

- Automatic encryption key generation:

`php artisan key:generate`

## 2) Configure the API

- Open **.env** with a text editor and set properly DB_* parameters, example:
Expand Down
77 changes: 77 additions & 0 deletions app/Http/Controllers/Achievements/AchievementsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: AvengerWeb
* Date: 25.03.16
* Time: 22:37
*/
namespace App\Http\Controllers\Achievements;

use App\Models\Achievements\Achievement;
use App\Models\Achievements\AchievementCategory;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

/**
* Class AchievementsController
* @package App\Http\Controllers\Achievements
*/
class AchievementsController extends Controller
{
/**
* Get achievements list or achievement by ID
*
* @param Request $request
* @param int $id
* @return Achievement|array|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|static[]
*/
public function getAchievements(Request $request, $id = 0) {

$result = Achievement::withExtra($request->has('no_extra_fields'));

if ($id)
$result = $result->findOrFail($id);
else {
if ($category = $request->get("category"))
$result->where('category', '=', $category);

if ($faction = $request->get('faction'))
switch ($faction) {
case "horde":
$result->where('Faction', '!=', '1');
break;
case "alliance":
$result->where('Faction', '!=', '0');
break;
}

$result = $result->get();
}

return $result;
}

/**
* Get achievements categories list or category by ID
*
* @param Request $request
* @param int $id
* @return AchievementCategory|array
*/
public function getAchievementCategories(Request $request, $id = 0) {

$result = AchievementCategory::withExtra($request->has('no_extra_fields'));

//backward compatibility
$id = $id ?: $request->get("id");

if ($id)
$result = $result->findOrFail($id);
else {
//TODO::Search by other fields
$result = $result->get();
}

return $result;
}
}
52 changes: 14 additions & 38 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
<?php

/* DBC */

Route::get('/achievement', function() {

$query = DB::connection('achievement')->table('achievement');

if (!isset($_GET['no_extra_fields']))
$query->select('ID', 'Faction', 'Map', 'Name', 'Description', 'Category', 'Points', 'Flags', 'SpellIcon', 'icon');

if (isset($_GET['category']) && $_GET['category'] != "")
$query->where('category', '=', $_GET['category']);

if (isset($_GET['faction']) && $_GET['faction'] != "") {
if ($_GET['faction'] == "horde") {
$query->where('Faction', '!=', '1');
}

if ($_GET['faction'] == "alliance")
$query->where('Faction', '!=', '0');
}

$result = $query->get();

return Response::json($result);
Route::group(["prefix" => "api/v1"], function() {
Route::group(["prefix" => "achievements"], function() {
Route::get("{id?}", "Achievements\AchievementsController@getAchievements")->where('id', '[0-9]+');
Route::get("categories/{id?}", "Achievements\AchievementsController@getAchievementCategories")
->where('id', '[0-9]+');
});
});

Route::get('/achievement_category', function() {

$query = DB::connection('achievement')->table('achievementcategory');

if (isset($_GET['no_extra_fields']) && $_GET['no_extra_fields'] != "" && $_GET['no_extra_fields'] == 0)
$query->select('*');
else
$query->select('ID', 'ParentID', 'Name');

if (isset($_GET['id']) && $_GET['id'] != "")
$query->where('ID', '=', $_GET['id']);
/*
* backward compatibility
*/
Route::get('achievement/{id?}', "Achievements\AchievementsController@getAchievements")->where('id', '[0-9]+');
Route::get('achievement_category/{id?}', "Achievements\AchievementsController@getAchievementCategories")
->where('id', '[0-9]+');

$result = $query->get();

return Response::json($result);
});
/* DBC */

Route::get('/dbc/achievements/{id}', function($id) {

Expand Down Expand Up @@ -298,7 +274,7 @@
if (isset($_GET['version']) && $_GET['version'] == 6)
{
// TODO
return;
return [];
}
else
{
Expand Down
155 changes: 155 additions & 0 deletions app/Models/Achievements/Achievement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace App\Models\Achievements;

use Illuminate\Database\Eloquent\Model;

/**
* App\Models\Achievements\Achievement
*
* @property integer $ID
* @property integer $Faction
* @property integer $Map
* @property integer $Previous
* @property string $Name
* @property string $field6
* @property string $field7
* @property string $field8
* @property string $field9
* @property string $field10
* @property string $field11
* @property string $field12
* @property string $field13
* @property string $field14
* @property string $field15
* @property string $field16
* @property string $field17
* @property string $field18
* @property string $field19
* @property string $field20
* @property string $field21
* @property string $Description
* @property string $field23
* @property string $field24
* @property string $field25
* @property mixed $field26
* @property string $field27
* @property string $field28
* @property string $field29
* @property string $field30
* @property string $field31
* @property string $field32
* @property string $field33
* @property string $field34
* @property string $field35
* @property string $field36
* @property string $field37
* @property string $field38
* @property integer $Category
* @property integer $Points
* @property integer $OrderInGroup
* @property integer $Flags
* @property integer $SpellIcon
* @property string $Reward
* @property string $field45
* @property string $field46
* @property string $Bonus
* @property string $field48
* @property string $field49
* @property string $field50
* @property string $field51
* @property string $field52
* @property string $field53
* @property string $field54
* @property string $field55
* @property string $field56
* @property string $field57
* @property string $field58
* @property string $field59
* @property string $field60
* @property integer $Demands
* @property integer $ReferencedAchievement
* @property string $icon
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereID($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereFaction($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereMap($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement wherePrevious($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereDescription($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereCategory($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement wherePoints($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereOrderInGroup($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereFlags($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereSpellIcon($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereReward($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereBonus($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereDemands($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereReferencedAchievement($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereIcon($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField6($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField7($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField8($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField9($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField10($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField11($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField12($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField13($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField14($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField15($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField16($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField17($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField18($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField19($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField20($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField21($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField23($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField24($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField25($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField26($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField27($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField28($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField29($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField30($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField31($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField32($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField33($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField34($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField35($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField36($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField37($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField38($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField45($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField46($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField48($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField49($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField50($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField51($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField52($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField53($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField54($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField55($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField56($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField57($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField58($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField59($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement whereField60($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\Achievement withExtra($extra = false)
* @mixin \Eloquent
*/
class Achievement extends Model
{
protected $table = "achievement";
protected $connection = "achievement";

/**
* Returning extra fields
*
* @param $query
* @param bool $extra
* @return Achievement
*/
public function scopeWithExtra($query, $extra = false) {
return $query->select($extra ? ['*'] :
['ID', 'Faction', 'Map', 'Name', 'Description', 'Category', 'Points', 'Flags', 'SpellIcon', 'icon']);
}
}
68 changes: 68 additions & 0 deletions app/Models/Achievements/AchievementCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Models\Achievements;

use Illuminate\Database\Eloquent\Model;

/**
* App\Models\Achievements\AchievementCategory
*
* @property integer $ID
* @property integer $ParentID
* @property string $Name
* @property string $field4
* @property string $field5
* @property string $field6
* @property string $field7
* @property string $field8
* @property string $field9
* @property string $field10
* @property string $field11
* @property string $field12
* @property string $field13
* @property string $field14
* @property string $field15
* @property string $field16
* @property string $field17
* @property string $field18
* @property string $field19
* @property integer $ui_order
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereID($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereParentID($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereName($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereUiOrder($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField4($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField5($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField6($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField7($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField8($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField9($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField10($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField11($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField12($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField13($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField14($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField15($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField16($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField17($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField18($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory whereField19($value)
* @method static \Illuminate\Database\Query\Builder|\App\Models\Achievements\AchievementCategory withExtra($extra = false)
* @mixin \Eloquent
*/
class AchievementCategory extends Model
{
protected $table = "achievementcategory";
protected $connection = "achievement";

/**
* Returning extra fields
*
* @param $query
* @param bool $extra
* @return AchievementCategory
*/
public function scopeWithExtra($query, $extra = false) {
return $query->select($extra ? [] : ['ID', 'ParentID', 'Name']);
}
}
5 changes: 5 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

use Illuminate\Foundation\Auth\User as Authenticatable;

/**
* App\User
*
* @mixin \Eloquent
*/
class User extends Authenticatable
{
/**
Expand Down
Loading

0 comments on commit 9056188

Please sign in to comment.