public function completeItem($id) : JsonResponse { $status = Nullable::fromValue($id)->reject('')->map('intval')->filter(P::lt(0))->flatMap(Item::class . '::findNullable')->map(function ($item) { return $item->setStateId(State::of('completed')->id)->save(); })->getOrThrow(new \RuntimeException("Item by ID {$id} not found!")); return response()->json(['status' => $status, 'id' => $id]); }
/** * Create a new item with the next content * @return Item */ public static function createNewItem(string $content) : Item { return Item::create(['content' => $content, 'complete_by' => Carbon::today()->addYear(), 'state_id' => State::of('new')->id]); }