This commit is contained in:
j3d1 2020-01-18 20:53:46 +01:00
parent d4e0d3859e
commit 836fb96f42
4 changed files with 77 additions and 27 deletions

View file

@ -37,6 +37,6 @@ class Container extends Model
static function find($id){ static function find($id){
return Container::leftJoin('items','items.cid','=','containers.cid') return Container::leftJoin('items','items.cid','=','containers.cid')
->select('containers.cid', 'name', DB::raw('count(items.iid) as itemCount')) ->select('containers.cid', 'name', DB::raw('count(items.iid) as itemCount'))
->groupBy('containers.cid', 'name')->where('items.cid', '=', $id)->first(); ->groupBy('containers.cid', 'name')->where(Container::primaryKey, '=', $id)->first();
} }
} }

View file

@ -20,10 +20,7 @@ class ItemController extends Controller
public function showByEvent($event) public function showByEvent($event)
{ {
$eid = Event::where('slug','=',$event)->first()->eid; $eid = Event::where('slug','=',$event)->first()->eid;
$q = Item::where('eid','=',$eid)->whereNull('returned_at') $q = Item::byEvent($eid);
->join('containers','items.cid','=','containers.cid')
->leftJoin('currentfiles','items.iid','=','currentfiles.iid')
->select('items.*','currentfiles.hash as file', 'containers.name as box');
return response()->json($q->get()); return response()->json($q->get());
} }
@ -31,11 +28,9 @@ class ItemController extends Controller
{ {
$eid = Event::where('slug','=',$event)->first()->eid; $eid = Event::where('slug','=',$event)->first()->eid;
$query_tokens = explode(" ",base64_decode ( $query , true)); $query_tokens = explode(" ",base64_decode ( $query , true));
$q = Item::where('eid','=',$eid)->whereNull('returned_at') $q = Item::byEvent($eid);
->join('containers','items.cid','=','containers.cid')
->leftJoin('currentfiles','items.iid','=','currentfiles.iid')
->select('items.*','currentfiles.hash as file', 'containers.name as box');
foreach ($query_tokens as $token) foreach ($query_tokens as $token)
if(!empty($token))
$q = $q->where('items.description','like','%'.$token.'%'); $q = $q->where('items.description','like','%'.$token.'%');
return response()->json($q->get()); return response()->json($q->get());
} }
@ -43,17 +38,16 @@ class ItemController extends Controller
public function showOneItem($event, $id) public function showOneItem($event, $id)
{ {
$eid = Event::where('slug','=',$event)->first()->eid; $eid = Event::where('slug','=',$event)->first()->eid;
return response()->json(Item::find($id)); return response()->json(Item::byEvent($eid)->where('item_uid', '=', $id)->first());
} }
public function create($event, Request $request) public function create($event, Request $request)
{ {
$eid = Event::where('slug','=',$event)->first()->eid; $eid = Event::where('slug','=',$event)->first()->eid;
$uid = Item::withTrashed()->where('eid',$eid)->max('item_uid') + 1;
$newitem = $request->except(['dataImage']); $newitem = $request->except(['dataImage']);
$newitem['eid'] = "".$eid; $newitem['eid'] = "".$eid;
$newitem['item_uid'] = $uid;
$newitem['wo'] = ""; $newitem['wo'] = "";
$newitem['wann'] = "";
$item = Item::create($newitem); $item = Item::create($newitem);
if ($request->get('dataImage')) { if ($request->get('dataImage')) {
@ -95,7 +89,7 @@ class ItemController extends Controller
$item['file'] = $hash; $item['file'] = $hash;
} }
} }
return response()->json($item, 200); return response()->json(Item::find($item['item_uid']), 200);
} }
public function delete($event, $id) public function delete($event, $id)

View file

@ -31,4 +31,35 @@ class Item extends Model
public static function restored($callback) public static function restored($callback)
{ {
} }
public static function create(array $attributes = [])
{
$uid = static::query()->where('eid',$attributes['eid'])->max('item_uid') + 1;
$attributes['item_uid'] = $uid;
static::query()->create($attributes);
return Item::find($uid);
}
protected static function extended($columns=Array()){
return Item::whereNull('returned_at')
->join('containers','items.cid','=','containers.cid')
->leftJoin('currentfiles','items.iid','=','currentfiles.iid');
}
static function byEvent($eid){
return Item::extended()->where('eid','=',$eid)
->select('items.*','currentfiles.hash as file', 'containers.name as box');
}
static function all($columns=Array()){
return Item::extended($columns)
->select('items.*','currentfiles.hash as file', 'containers.name as box')
->get();
}
static function find($id){
return Item::extended()
->select('items.*','currentfiles.hash as file', 'containers.name as box')
->where('items.iid', '=', $id)->first();
}
} }

View file

@ -25,7 +25,7 @@ class ItemTest extends TestCase
{ {
$event = Event::create(['slug'=>'EVENT','name'=>'Event']); $event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
$this->get('/1/EVENT/items'); $this->get('/1/EVENT/items');
$response = $this->response->getOriginalContent(); $response = $this->response->getOriginalContent();
@ -43,7 +43,7 @@ class ItemTest extends TestCase
{ {
$event = Event::create(['slug'=>'EVENT','name'=>'Event']); $event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); $item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
File::create(['iid'=>$item->iid, 'hash'=>'filename']); File::create(['iid'=>$item->iid, 'hash'=>'filename']);
$this->get('/1/EVENT/items'); $this->get('/1/EVENT/items');
@ -62,9 +62,9 @@ class ItemTest extends TestCase
{ {
$event = Event::create(['slug'=>'EVENT','name'=>'Event']); $event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>2, 'wann'=>'', 'wo'=>'','description'=>'2']); Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>3, 'wann'=>'', 'wo'=>'','description'=>'3']); Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'3']);
$this->get('/1/EVENT/items'); $this->get('/1/EVENT/items');
$response = $this->response->getOriginalContent(); $response = $this->response->getOriginalContent();
@ -77,13 +77,13 @@ class ItemTest extends TestCase
Event::create(['slug'=>'EVENT','name'=>'Event']); Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
$this->post('/1/EVENT/item',['cid'=>$box->cid, 'wann'=>'', 'description'=>'1']); $this->post('/1/EVENT/item',['cid'=>$box->cid, 'description'=>'1']);
$response = $this->response->getOriginalContent(); $response = $this->response->getOriginalContent();
$this->assertResponseStatus(201); $this->assertResponseStatus(201);
$this->assertEquals(1, $response['item_uid']); $this->assertEquals(1, $response['item_uid']);
$this->assertEquals('1', $response['description']); $this->assertEquals('1', $response['description']);
//$this->assertEquals($box->name, $response['box']); $this->assertEquals($box->name, $response['box']);
$this->assertEquals($box->cid, $response['cid']); $this->assertEquals($box->cid, $response['cid']);
//$this->assertEquals('filename', $response['file']); //$this->assertEquals('filename', $response['file']);
@ -91,7 +91,7 @@ class ItemTest extends TestCase
$this->assertEquals(1, count($items)); $this->assertEquals(1, count($items));
$this->assertEquals(1, $items[0]['item_uid']); $this->assertEquals(1, $items[0]['item_uid']);
$this->assertEquals('1', $items[0]['description']); $this->assertEquals('1', $items[0]['description']);
//$this->assertEquals($box->name, $items[0]['box']); $this->assertEquals($box->name, $items[0]['box']);
$this->assertEquals($box->cid, $items[0]['cid']); $this->assertEquals($box->cid, $items[0]['cid']);
//$this->assertEquals('filename', $items[0]['file']); //$this->assertEquals('filename', $items[0]['file']);
} }
@ -110,7 +110,7 @@ class ItemTest extends TestCase
$event = Event::create(['slug'=>'EVENT','name'=>'Event']); $event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); $item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
$this->assertEquals(1, $item['item_uid']); $this->assertEquals(1, $item['item_uid']);
$this->assertEquals('1', $item['description']); $this->assertEquals('1', $item['description']);
@ -122,7 +122,7 @@ class ItemTest extends TestCase
$this->assertResponseOk(); $this->assertResponseOk();
$this->assertEquals(1, $response['item_uid']); $this->assertEquals(1, $response['item_uid']);
$this->assertEquals('2', $response['description']); $this->assertEquals('2', $response['description']);
//$this->assertEquals($box->name, $response['box']); $this->assertEquals($box->name, $response['box']);
$this->assertEquals($box->cid, $response['cid']); $this->assertEquals($box->cid, $response['cid']);
//$this->assertEquals('filename', $response['file']); //$this->assertEquals('filename', $response['file']);
@ -130,7 +130,7 @@ class ItemTest extends TestCase
$this->assertEquals(1, count($items)); $this->assertEquals(1, count($items));
$this->assertEquals(1, $items[0]['item_uid']); $this->assertEquals(1, $items[0]['item_uid']);
$this->assertEquals('2', $items[0]['description']); $this->assertEquals('2', $items[0]['description']);
//$this->assertEquals($box->name, $items[0]['box']); $this->assertEquals($box->name, $items[0]['box']);
$this->assertEquals($box->cid, $items[0]['cid']); $this->assertEquals($box->cid, $items[0]['cid']);
//$this->assertEquals('filename', $items[0]['file']); //$this->assertEquals('filename', $items[0]['file']);
} }
@ -138,8 +138,8 @@ class ItemTest extends TestCase
public function testDeleteItem(){ public function testDeleteItem(){
$event = Event::create(['slug'=>'EVENT','name'=>'Event']); $event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']); $box = Container::create(['name'=>'BOX']);
$item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); $item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>2, 'wann'=>'', 'wo'=>'','description'=>'2']); Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
$this->assertEquals(2, count(Item::all())); $this->assertEquals(2, count(Item::all()));
@ -149,4 +149,29 @@ class ItemTest extends TestCase
$this->assertEquals(1, count(Item::all())); $this->assertEquals(1, count(Item::all()));
} }
public function testDeleteItem2(){
$event = Event::create(['slug'=>'EVENT','name'=>'Event']);
$box = Container::create(['name'=>'BOX']);
$item1 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']);
$item2 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']);
$this->assertEquals(2, count(Item::all()));
$this->delete('/1/EVENT/item/'.$item1->item_uid);
$this->assertResponseOk();
$this->assertEquals(1, count(Item::all()));
$item3 = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'3']);
$this->assertEquals(3, $item3['item_uid']);
$this->assertEquals(2, count(Item::all()));
$this->delete('/1/EVENT/item/'.$item2->item_uid);
$this->assertResponseOk();
$this->assertEquals(1, count(Item::all()));
}
} }