blessing-skin-server/app/Http/Controllers/ClosetController.php

127 lines
3.4 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
2016-08-28 10:05:21 +08:00
namespace App\Http\Controllers;
2016-07-21 22:01:57 +08:00
use View;
use Option;
2016-07-21 22:01:57 +08:00
use App\Models\User;
use App\Models\Closet;
use App\Models\Texture;
2016-07-21 22:01:57 +08:00
use App\Models\ClosetModel;
use Illuminate\Http\Request;
use App\Exceptions\PrettyPageException;
2016-07-21 22:01:57 +08:00
class ClosetController extends Controller
2016-07-21 22:01:57 +08:00
{
/**
* Instance of Closet.
*
* @var \App\Models\Closet
*/
private $closet;
2016-07-21 22:01:57 +08:00
public function __construct()
2016-07-21 22:01:57 +08:00
{
2016-08-28 10:05:21 +08:00
$this->closet = new Closet(session('uid'));
2016-07-21 22:01:57 +08:00
}
public function index()
{
return view('user.closet')->with('user', app('user.current'));
}
public function getClosetData(Request $request)
2016-07-21 22:01:57 +08:00
{
$category = $request->input('category', 'skin');
$page = abs($request->input('page', 1));
2017-11-06 11:07:24 +08:00
$per_page = (int) $request->input('perPage', 6);
2016-10-16 20:04:21 +08:00
$q = $request->input('q', null);
2016-07-21 22:01:57 +08:00
2017-11-06 11:07:24 +08:00
$per_page = $per_page > 0 ? $per_page : 6;
2017-11-05 16:54:01 +08:00
$items = collect();
2016-07-21 22:01:57 +08:00
if ($q) {
// do search
2017-11-05 16:54:01 +08:00
$items = $this->closet->getItems($category)->filter(function ($item) use ($q) {
return stristr($item['name'], $q);
});
2016-10-16 20:04:21 +08:00
} else {
$items = $this->closet->getItems($category);
2016-10-16 20:04:21 +08:00
}
// pagination
2017-11-06 11:07:24 +08:00
$total_pages = ceil($items->count() / $per_page);
2016-07-21 22:01:57 +08:00
return response()->json([
'category' => $category,
2017-11-06 11:07:24 +08:00
'items' => $items->forPage($page, $per_page)->values(),
'total_pages' => $total_pages
]);
2016-07-21 22:01:57 +08:00
}
public function add(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
'tid' => 'required|integer',
2016-09-25 10:35:16 +08:00
'name' => 'required|no_special_chars'
]);
2016-07-21 22:01:57 +08:00
if (app('user.current')->getScore() < option('score_per_closet_item')) {
return json(trans('user.closet.add.lack-score'), 7);
}
2017-11-04 17:18:01 +08:00
$tid = $request->tid;
if (!Texture::find($tid)) {
return json(trans('user.closet.add.not-found'), 1);
}
if ($this->closet->add($tid, $request->name)) {
$t = Texture::find($tid);
2016-07-21 22:01:57 +08:00
$t->likes += 1;
$t->save();
app('user.current')->setScore(option('score_per_closet_item'), 'minus');
2016-09-24 22:49:20 +08:00
return json(trans('user.closet.add.success', ['name' => $request->input('name')]), 0);
2016-09-10 21:39:45 +08:00
} else {
2016-09-24 22:49:20 +08:00
return json(trans('user.closet.add.repeated'), 1);
2016-07-21 22:01:57 +08:00
}
}
2016-09-25 10:35:16 +08:00
public function rename(Request $request)
{
$this->validate($request, [
'tid' => 'required|integer',
'new_name' => 'required|no_special_chars'
]);
if ($this->closet->rename($request->tid, $request->new_name)) {
return json(trans('user.closet.rename.success', ['name' => $request->new_name]), 0);
} else {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
2016-09-25 10:35:16 +08:00
}
}
public function remove(Request $request)
2016-07-21 22:01:57 +08:00
{
$this->validate($request, [
'tid' => 'required|integer'
]);
2016-07-21 22:01:57 +08:00
if ($this->closet->remove($request->tid)) {
$t = Texture::find($request->tid);
2016-07-21 22:01:57 +08:00
$t->likes = $t->likes - 1;
$t->save();
if (option('return_score'))
2017-04-21 18:44:11 +08:00
app('user.current')->setScore(option('score_per_closet_item'), 'plus');
2016-09-24 22:49:20 +08:00
return json(trans('user.closet.remove.success'), 0);
2016-09-10 21:39:45 +08:00
} else {
2017-11-04 17:18:01 +08:00
return json(trans('user.closet.remove.non-existent'), 1);
2016-07-21 22:01:57 +08:00
}
}
}