Commit 2a93a019 authored by Andrea Ruggiero's avatar Andrea Ruggiero
Browse files

Improve performance

parent 18acc531
Pipeline #1556 failed with stage
in 0 seconds
......@@ -27,7 +27,36 @@ class EventController extends Controller
$user = $sessionRepository->getUserByToken($request->headers->get('X-Auth-Token'));
$skip = [];
foreach ($events as $event) {
if (isset($skip[$event->event_id])) continue;
$places = [];
foreach ($eventRepository->getSimilarEvents($event) as $e) {
$skip[$e->event_id] = false;
if ($event->place_id === null) {
$places = array_map(function($place) {
return [
"place" => $place->name,
"capacity" => $place->capacity,
"latitude" => $place->latitude,
"longitude" => $place->longitude,
"address" => $place->address
];
}, $placeRepository->all());
break;
} else {
$place = $placeRepository->getById($e->place_id);
$places[] = [
"place" => $place->name,
"capacity" => $place->capacity,
"latitude" => $place->latitude,
"longitude" => $place->longitude,
"address" => $place->address
];
}
}
$temp = [
"id" => $event->event_id,
"title" => $event->title,
......@@ -38,9 +67,9 @@ class EventController extends Controller
"type" => $event->type,
"theme" => $event->theme,
"bookings" => count($bookingRepository->getBookingsByEvent($event->event_id)),
"places" => $eventRepository->getPlaces($event, $placeRepository)
"places" => $places
];
if ($user !== null) {
$temp["hasBooked"] = $bookingRepository->getBookingByEventAndUser($user->user_id, $event->event_id) !== null;
}
......
......@@ -19,7 +19,7 @@ class BaseRepository
}
public function all() {
return $this->db->table($this->table)->get();
return $this->db()->table($this->table)->get();
}
protected function db() {
......
......@@ -21,7 +21,7 @@ class BookingRepository extends BaseRepository
}
public function getBookingByEventAndUser($user_id, $event_id) {
return $this->db()->where('event_id', (int) $event_id)->where('user_id', $user_id)->first();
return $this->db()->where('event_id', $event_id)->where('user_id', $user_id)->first();
}
}
\ No newline at end of file
......@@ -12,32 +12,8 @@ class EventRepository extends BaseRepository
return $this->db()->where('event_id', $id)->first();
}
public function getPlaces($event, PlaceRepository $placeRepository) {
$events = $this->db()->where('title', $event->title)->where('date', $event->date)->where('start', $event->start)->where('end', $event->end)->get();
$places = [];
foreach ($events as $event) {
if ($event->place_id === null) {
return array_map(function($place) {
return [
"place" => $place->name,
"capacity" => $place->capacity,
"latitude" => $place->latitude,
"longitude" => $place->longitude,
"address" => $place->address
];
}, $placeRepository->all());
} else {
$place = $placeRepository->getById($event->place_id);
$places[] = [
"place" => $place->name,
"capacity" => $place->capacity,
"latitude" => $place->latitude,
"longitude" => $place->longitude,
"address" => $place->address
];
}
}
return $places;
public function getSimilarEvents($event) {
return $this->db()->where('title', $event->title)->where('date', $event->date)->where('start', $event->start)->where('end', $event->end)->get();
}
}
\ No newline at end of file
......@@ -6,9 +6,22 @@ class PlaceRepository extends BaseRepository
{
protected $table = 'places';
protected $all = null;
public function getById($id) {
if ($this->all !== null) {
foreach ($this->all as $place) {
if ($place->place_id === $id) return $place;
}
}
return $this->db()->where('place_id', $id)->first();
}
public function all() {
if ($this->all === null) {
$all = $this->db()->table($this->table)->get();
}
return $this->all;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment