~cypheon/nextcloud-chores-app

ref: d19a6603c01e63c7545582e5ff313ef4aec0bf9f nextcloud-chores-app/lib/Db/MemberMapper.php -rw-r--r-- 2.0 KiB
d19a6603 — Johann Rudloff Make overdue sidebar badge dynamic, switch badges to `CounterBubble` component 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace OCA\Chores\Db;

use OCP\IDBConnection;
use OCP\AppFramework\Db\QBMapper;

class MemberMapper extends QBMapper {

  public function __construct(IDBConnection $db) {
    parent::__construct($db, 'chores_team_members', Member::class);
  }

  public function teamHasMember(int $teamId, string $userId): bool {
    $qb = $this->db->getQueryBuilder();

    $qb->select('m.team_id')
       ->from($this->tableName, 'm')
       ->where(
         $qb->expr()->eq('m.member_user_id', $qb->createNamedParameter($userId))
       )
       ->andWhere(
         $qb->expr()->eq('m.team_id', $qb->createNamedParameter($teamId))
       )
       ;
    $found = $this->findEntities($qb);
    return count($found) > 0;
  }

  public function findByTeam(int $teamId) {
    $qb = $this->db->getQueryBuilder();

    $qb->select('m.*')
       ->from($this->tableName, 'm')
       ->where(
         $qb->expr()->eq('m.team_id', $qb->createNamedParameter($teamId))
       )
       ;
    return $this->findEntities($qb);
  }

  public function addPoints(string $memberUid, int $points) {
    $qb = $this->db->getQueryBuilder();

    $affectedRows =
      $qb->update($this->tableName)
       ->where(
         $qb->expr()->eq('member_user_id', $qb->createNamedParameter($memberUid))
       )
       ->set('points',
         $qb->func()->add('points', $qb->createNamedParameter($points))
       )
       ->execute();
    assert(is_int($affectedRows));
    if ($affectedRows != 1) {
      throw new \Exception("could not add points to user");
    }
  }

  public function addTeamMember(int $teamId, string $userId) {
    $qb = $this->db->getQueryBuilder();

    $affectedRows =
      $qb->insert($this->tableName)
         ->values([
           "team_id" => $qb->createNamedParameter($teamId),
           "member_user_id" => $qb->createNamedParameter($userId),
           "points" => $qb->createNamedParameter(0),
         ])
         ->execute();
    assert(is_int($affectedRows));
    if ($affectedRows != 1) {
      throw new \Exception("could not add user to team");
    }
  }

}