1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Game; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
class CDCController extends Controller |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Display a listing of the resource. |
13
|
|
|
* |
14
|
|
|
* @return \Illuminate\Http\Response |
15
|
|
|
*/ |
16
|
|
View Code Duplication |
public function index() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$cdc = \DB::table('games_coupdecoeur') |
19
|
|
|
->leftJoin('games', 'games.id', '=', 'games_coupdecoeur.game_id') |
20
|
|
|
->leftJoin('games_developer', 'games.id', '=', 'games_developer.game_id') |
21
|
|
|
->leftJoin('developer', 'games_developer.developer_id', '=', 'developer.id') |
22
|
|
|
->leftJoin('makers', 'makers.id', '=', 'games.maker_id') |
23
|
|
|
->leftJoin('comments', function($join) { |
24
|
|
|
$join->on('comments.content_id', '=', 'games.id'); |
25
|
|
|
$join->on('comments.content_type', '=', \DB::raw("'game'")); |
26
|
|
|
}) |
27
|
|
|
->leftJoin('games_files', 'games_files.game_id', '=', 'games.id') |
28
|
|
|
->leftJoin('users', 'games_developer.user_id', '=', 'users.id') |
29
|
|
|
->select([ |
30
|
|
|
'games.id as gameid', |
31
|
|
|
'games.title as gametitle', |
32
|
|
|
'games.subtitle as gamesubtitle', |
33
|
|
|
'developer.name as developername', |
34
|
|
|
'developer.id as developerid', |
35
|
|
|
'developer.created_at as developerdate', |
36
|
|
|
'developer.user_id as developeruserid', |
37
|
|
|
'users.name as developerusername', |
38
|
|
|
'games.created_at as gamecreated_at', |
39
|
|
|
'makers.short as makershort', |
40
|
|
|
'makers.title as makertitle', |
41
|
|
|
'makers.id as makerid', |
42
|
|
|
'games_coupdecoeur.created_at as cdcdate', |
43
|
|
|
]) |
44
|
|
|
->selectRaw('(SELECT COUNT(id) FROM comments WHERE content_id = games.id AND content_type = "game") as commentcount') |
45
|
|
|
->selectRaw('(SELECT SUM(vote_up) FROM comments WHERE content_id = games.id AND content_type = "game") as voteup') |
46
|
|
|
->selectRaw('(SELECT SUM(vote_down) FROM comments WHERE content_id = games.id AND content_type = "game") as votedown') |
47
|
|
|
->selectRaw('MAX(games_files.release_type) as gametype') |
48
|
|
|
->selectRaw("(SELECT STR_TO_DATE(CONCAT(release_year,'-',release_month,'-',release_day ), '%Y-%m-%d') FROM games_files WHERE game_id = games.id ORDER BY release_year DESC, release_month DESC, release_day DESC LIMIT 1) as releasedate") |
49
|
|
|
->selectRaw('(SELECT COUNT(id) FROM games_coupdecoeur WHERE game_id = games.id) as cdccount') |
50
|
|
|
->groupBy('games.id') |
51
|
|
|
->orderBy('games_coupdecoeur.created_at', 'desc') |
52
|
|
|
->get(); |
53
|
|
|
|
54
|
|
|
$gametypes = \DB::table('games_files_types') |
55
|
|
|
->select('id', 'title', 'short') |
56
|
|
|
->get(); |
57
|
|
|
$gtypes = array(); |
|
|
|
|
58
|
|
|
foreach ($gametypes as $gt) { |
59
|
|
|
$t['title'] = $gt->title; |
|
|
|
|
60
|
|
|
$t['short'] = $gt->short; |
|
|
|
|
61
|
|
|
$gtypes[$gt->id] = $t; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return view('cdc.index', [ |
65
|
|
|
'cdcs' => $cdc, |
66
|
|
|
'gametypes' => $gtypes, |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Show the form for creating a new resource. |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Http\Response |
74
|
|
|
*/ |
75
|
|
|
public function create() |
76
|
|
|
{ |
77
|
|
|
return view('cdc.create'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Store a newly created resource in storage. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Http\Request $request |
84
|
|
|
* @return \Illuminate\Http\RedirectResponse |
85
|
|
|
*/ |
86
|
|
|
public function store(Request $request) |
87
|
|
|
{ |
88
|
|
|
$this->validate($request, [ |
89
|
|
|
'gamename' => 'required', |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
// Prüfen ob das Spiel auch wirklich existiert |
93
|
|
|
$title = explode(" -=- ", $request->get('gamename')); |
|
|
|
|
94
|
|
|
|
95
|
|
|
if (count($title) == 1) { |
96
|
|
|
$game = Game::whereTitle($title[0]) |
97
|
|
|
->first(); |
98
|
|
|
} else { |
99
|
|
|
$game = Game::whereTitle($title[0]) |
|
|
|
|
100
|
|
|
->orWhere('subtitle', '=', $title[1]) |
101
|
|
|
->first(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
\DB::table('games_coupdecoeur')->insert([ |
105
|
|
|
'game_id' => $game->id, |
106
|
|
|
'user_id' => \Auth::id(), |
107
|
|
|
'created_at' => Carbon::now(), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
return redirect()->action('MsgBoxController@cdc_add', [$game->id]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.