diff --git a/app/Container.php b/app/Container.php index aa747c3..d35a0c7 100644 --- a/app/Container.php +++ b/app/Container.php @@ -37,6 +37,6 @@ class Container extends Model static function find($id){ return Container::leftJoin('items','items.cid','=','containers.cid') ->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(); } } diff --git a/app/Http/Controllers/ItemController.php b/app/Http/Controllers/ItemController.php index d5034c6..d790581 100644 --- a/app/Http/Controllers/ItemController.php +++ b/app/Http/Controllers/ItemController.php @@ -20,10 +20,7 @@ class ItemController extends Controller public function showByEvent($event) { $eid = Event::where('slug','=',$event)->first()->eid; - $q = Item::where('eid','=',$eid)->whereNull('returned_at') - ->join('containers','items.cid','=','containers.cid') - ->leftJoin('currentfiles','items.iid','=','currentfiles.iid') - ->select('items.*','currentfiles.hash as file', 'containers.name as box'); + $q = Item::byEvent($eid); return response()->json($q->get()); } @@ -31,29 +28,26 @@ class ItemController extends Controller { $eid = Event::where('slug','=',$event)->first()->eid; $query_tokens = explode(" ",base64_decode ( $query , true)); - $q = Item::where('eid','=',$eid)->whereNull('returned_at') - ->join('containers','items.cid','=','containers.cid') - ->leftJoin('currentfiles','items.iid','=','currentfiles.iid') - ->select('items.*','currentfiles.hash as file', 'containers.name as box'); + $q = Item::byEvent($eid); foreach ($query_tokens as $token) - $q = $q->where('items.description','like','%'.$token.'%'); + if(!empty($token)) + $q = $q->where('items.description','like','%'.$token.'%'); return response()->json($q->get()); } public function showOneItem($event, $id) { $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) { $eid = Event::where('slug','=',$event)->first()->eid; - $uid = Item::withTrashed()->where('eid',$eid)->max('item_uid') + 1; $newitem = $request->except(['dataImage']); $newitem['eid'] = "".$eid; - $newitem['item_uid'] = $uid; $newitem['wo'] = ""; + $newitem['wann'] = ""; $item = Item::create($newitem); if ($request->get('dataImage')) { @@ -95,7 +89,7 @@ class ItemController extends Controller $item['file'] = $hash; } } - return response()->json($item, 200); + return response()->json(Item::find($item['item_uid']), 200); } public function delete($event, $id) diff --git a/app/Item.php b/app/Item.php index 0914dc8..348d416 100644 --- a/app/Item.php +++ b/app/Item.php @@ -31,4 +31,35 @@ class Item extends Model 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(); + } } diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 7ced8e3..bec89b5 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -25,7 +25,7 @@ class ItemTest extends TestCase { $event = Event::create(['slug'=>'EVENT','name'=>'Event']); $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'); $response = $this->response->getOriginalContent(); @@ -43,7 +43,7 @@ class ItemTest extends TestCase { $event = Event::create(['slug'=>'EVENT','name'=>'Event']); $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']); $this->get('/1/EVENT/items'); @@ -62,9 +62,9 @@ class ItemTest extends TestCase { $event = Event::create(['slug'=>'EVENT','name'=>'Event']); $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, 'item_uid'=>2, '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'=>'1']); + Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']); + Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'3']); $this->get('/1/EVENT/items'); $response = $this->response->getOriginalContent(); @@ -77,13 +77,13 @@ class ItemTest extends TestCase Event::create(['slug'=>'EVENT','name'=>'Event']); $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(); $this->assertResponseStatus(201); $this->assertEquals(1, $response['item_uid']); $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('filename', $response['file']); @@ -91,7 +91,7 @@ class ItemTest extends TestCase $this->assertEquals(1, count($items)); $this->assertEquals(1, $items[0]['item_uid']); $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('filename', $items[0]['file']); } @@ -110,7 +110,7 @@ class ItemTest extends TestCase $event = Event::create(['slug'=>'EVENT','name'=>'Event']); $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['description']); @@ -122,7 +122,7 @@ class ItemTest extends TestCase $this->assertResponseOk(); $this->assertEquals(1, $response['item_uid']); $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('filename', $response['file']); @@ -130,7 +130,7 @@ class ItemTest extends TestCase $this->assertEquals(1, count($items)); $this->assertEquals(1, $items[0]['item_uid']); $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('filename', $items[0]['file']); } @@ -138,8 +138,8 @@ class ItemTest extends TestCase public function testDeleteItem(){ $event = Event::create(['slug'=>'EVENT','name'=>'Event']); $box = Container::create(['name'=>'BOX']); - $item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>1, 'wann'=>'', 'wo'=>'','description'=>'1']); - Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'item_uid'=>2, 'wann'=>'', 'wo'=>'','description'=>'2']); + $item = Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'1']); + Item::create(['cid'=>$box->cid, 'eid' => $event->eid, 'wann'=>'', 'wo'=>'','description'=>'2']); $this->assertEquals(2, count(Item::all())); @@ -149,4 +149,29 @@ class ItemTest extends TestCase $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())); + } + }