c3lf-system-3/app/Http/Controllers/ItemController.php

84 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2019-11-15 21:26:54 +00:00
<?php
namespace App\Http\Controllers;
use App\Container;
use App\File;
2019-11-15 21:26:54 +00:00
use App\Item;
2019-12-02 17:41:40 +00:00
use App\Event;
2019-11-15 21:26:54 +00:00
use Illuminate\Http\Request;
2019-12-28 03:31:13 +00:00
use Illuminate\Support\Facades\DB;
2019-11-15 21:26:54 +00:00
class ItemController extends Controller
{
2019-11-30 12:47:54 +00:00
public function showAllItems()
2019-11-15 21:26:54 +00:00
{
return response()->json(Item::all());
}
2019-12-01 17:30:25 +00:00
public function showByEvent($event)
2019-11-30 12:47:54 +00:00
{
2019-12-02 17:41:40 +00:00
$eid = Event::where('slug','=',$event)->first()->eid;
2020-01-18 19:53:46 +00:00
$q = Item::byEvent($eid);
2019-12-28 03:31:13 +00:00
return response()->json($q->get());
2019-11-30 12:47:54 +00:00
}
2019-12-01 17:30:25 +00:00
public function searchByEvent($event, $query)
{
2019-12-11 20:36:43 +00:00
$eid = Event::where('slug','=',$event)->first()->eid;
2019-12-26 19:21:40 +00:00
$query_tokens = explode(" ",base64_decode ( $query , true));
2020-01-18 19:53:46 +00:00
$q = Item::byEvent($eid);
2019-12-26 19:21:40 +00:00
foreach ($query_tokens as $token)
2020-01-18 19:53:46 +00:00
if(!empty($token))
$q = $q->where('items.description','like','%'.$token.'%');
2019-12-26 19:21:40 +00:00
return response()->json($q->get());
2019-12-01 17:30:25 +00:00
}
public function showOneItem($event, $id)
2019-11-15 21:26:54 +00:00
{
2019-12-17 00:33:40 +00:00
$eid = Event::where('slug','=',$event)->first()->eid;
2020-01-18 20:37:02 +00:00
return response()->json(Item::byEvent($eid)->where('uid', '=', $id)->first());
2019-11-15 21:26:54 +00:00
}
2019-12-01 17:30:25 +00:00
public function create($event, Request $request)
2019-11-30 12:47:54 +00:00
{
2019-12-17 00:33:40 +00:00
$eid = Event::where('slug','=',$event)->first()->eid;
$newitem = $request->except(['dataImage']);
$newitem['eid'] = "".$eid;
$item = Item::create($newitem);
2019-12-27 00:25:55 +00:00
2020-01-19 00:07:08 +00:00
if($request->get('dataImage')) {
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
$item['file'] = $file['hash'];
2019-12-27 00:25:55 +00:00
}
2020-01-19 00:07:08 +00:00
2019-11-15 21:26:54 +00:00
return response()->json($item, 201);
}
2019-12-01 17:30:25 +00:00
public function update($event, $id, Request $request)
2019-11-15 21:26:54 +00:00
{
2019-12-19 15:22:57 +00:00
$eid = Event::where('slug', $event)->first()->eid;
2020-01-18 20:37:02 +00:00
$item = Item::where('eid', $eid)->where('uid', $id)->first();
2019-12-28 03:31:13 +00:00
$item->update($request->except(['file', 'box', 'dataImage']));
2019-12-27 00:25:55 +00:00
2019-12-28 03:31:13 +00:00
if($request->get('returned')===true){
$item->update(['returned_at' => DB::raw( 'current_timestamp' )]);
}
2020-01-19 00:07:08 +00:00
if($request->get('dataImage')) {
$file = File::create(array('data' => $request->get('dataImage'), 'iid' => $item['iid']));
$item['file'] = $file['hash'];
2019-12-27 00:25:55 +00:00
}
2020-01-19 00:07:08 +00:00
2020-01-18 20:37:02 +00:00
return response()->json(Item::find($item['uid']), 200);
2019-11-15 21:26:54 +00:00
}
2019-12-01 17:30:25 +00:00
public function delete($event, $id)
2019-11-15 21:26:54 +00:00
{
2019-12-17 00:33:40 +00:00
$eid = Event::where('slug','=',$event)->first()->eid;
2020-01-18 20:37:02 +00:00
Item::where('eid', $eid)->where('uid', $id)->first()->delete();
return response()->json(array("status"=>'Deleted Successfully'), 200);
2019-11-15 21:26:54 +00:00
}
2019-11-30 12:47:54 +00:00
}