recent activities via API (#761)

This commit is contained in:
Kevin Papst
2019-05-07 22:24:28 +02:00
committed by GitHub
parent 98f386dad6
commit b1855447b8
40 changed files with 376 additions and 258 deletions

View File

@@ -12,7 +12,6 @@ namespace App\Repository;
use App\Entity\Activity;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Model\ActivityStatistic;
use App\Repository\Query\ActivityQuery;
use Doctrine\ORM\ORMException;
@@ -31,63 +30,6 @@ class ActivityRepository extends AbstractRepository
return $this->find($id);
}
/**
* @param User|null $user
* @param \DateTime|null $startFrom
* @return Timesheet[]
* @throws Query\QueryException
*/
public function getRecentActivities(User $user = null, \DateTime $startFrom = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select($qb->expr()->max('t.id') . ' AS maxid')
->from(Timesheet::class, 't')
->indexBy('t', 't.id')
->join('t.activity', 'a')
->join('t.project', 'p')
->join('p.customer', 'c')
->andWhere($qb->expr()->isNotNull('t.end'))
->andWhere($qb->expr()->eq('a.visible', ':visible'))
->andWhere($qb->expr()->eq('p.visible', ':visible'))
->andWhere($qb->expr()->eq('c.visible', ':visible'))
->groupBy('a.id', 'p.id')
->orderBy('maxid', 'DESC')
->setMaxResults(10)
->setParameter('visible', true, \PDO::PARAM_BOOL)
;
if (null !== $user) {
$qb->andWhere('t.user = :user')
->setParameter('user', $user);
}
if (null !== $startFrom) {
$qb->andWhere($qb->expr()->gt('t.begin', ':begin'))
->setParameter('begin', $startFrom);
}
$results = $qb->getQuery()->getScalarResult();
if (empty($results)) {
return [];
}
$ids = array_column($results, 'maxid');
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t', 'a', 'p', 'c')
->from(Timesheet::class, 't')
->join('t.activity', 'a')
->join('t.project', 'p')
->join('p.customer', 'c')
->andWhere($qb->expr()->in('t.id', $ids))
->orderBy('t.end', 'DESC')
;
return $qb->getQuery()->getResult();
}
/**
* @param null|bool $visible
* @return int

View File

@@ -384,4 +384,62 @@ class TimesheetRepository extends AbstractRepository
return $this->getBaseQueryResult($qb, $query);
}
/**
* @param User|null $user
* @param DateTime|null $startFrom
* @param int $limit
* @return array|mixed
* @throws \Doctrine\ORM\Query\QueryException
*/
public function getRecentActivities(User $user = null, \DateTime $startFrom = null, $limit = 10)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select($qb->expr()->max('t.id') . ' AS maxid')
->from(Timesheet::class, 't')
->indexBy('t', 't.id')
->join('t.activity', 'a')
->join('t.project', 'p')
->join('p.customer', 'c')
->andWhere($qb->expr()->isNotNull('t.end'))
->andWhere($qb->expr()->eq('a.visible', ':visible'))
->andWhere($qb->expr()->eq('p.visible', ':visible'))
->andWhere($qb->expr()->eq('c.visible', ':visible'))
->groupBy('a.id', 'p.id')
->orderBy('maxid', 'DESC')
->setMaxResults($limit)
->setParameter('visible', true, \PDO::PARAM_BOOL)
;
if (null !== $user) {
$qb->andWhere('t.user = :user')
->setParameter('user', $user);
}
if (null !== $startFrom) {
$qb->andWhere($qb->expr()->gt('t.begin', ':begin'))
->setParameter('begin', $startFrom);
}
$results = $qb->getQuery()->getScalarResult();
if (empty($results)) {
return [];
}
$ids = array_column($results, 'maxid');
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('t', 'a', 'p', 'c')
->from(Timesheet::class, 't')
->join('t.activity', 'a')
->join('t.project', 'p')
->join('p.customer', 'c')
->andWhere($qb->expr()->in('t.id', $ids))
->orderBy('t.end', 'DESC')
;
return $qb->getQuery()->getResult();
}
}