~cypheon/nextcloud-chores-app

ref: d19a6603c01e63c7545582e5ff313ef4aec0bf9f nextcloud-chores-app/lib/Db/InviteMapper.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
77
78
<?php
namespace OCA\Chores\Db;

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

const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

class InviteMapper extends QBMapper {

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

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

    $qb->select('i.*')
       ->from($this->tableName, 'i')
       ->where(
         $qb->expr()->eq('i.team_id', $qb->createNamedParameter($teamId))
       )
       ->andWhere(
         $qb->expr()->eq('i.user_id', $qb->createNamedParameter($userId))
       )
       ;
    $found = $this->findEntities($qb);
    if (count($found) > 0) {
      return $found[0];
    } else {
      return null;
    }
  }

  public function createInvite(int $teamId, string $userId): Invite {
    $existing = $this->findByTeamAndUser($teamId, $userId);
    if ($existing !== null) {
      return $existing;
    }

    $randomId = implode("", array_map(function($i){
      return ALPHABET[random_int(0, 61)];
    }, range(0, 16)));
    $invite = Invite::fromParams([
      "teamId" => $teamId,
      "userId" => $userId,
      "id" => $randomId,
    ]);

    return $this->insert($invite);
  }

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

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

  public function findByUser(string $userId) {
    $qb = $this->db->getQueryBuilder();

    $qb->select('i.*', 't.team_name')
       ->from($this->tableName, 'i')
       ->where(
         $qb->expr()->eq('i.user_id', $qb->createNamedParameter($userId))
       )
       ->innerJoin('i', 'chores_teams', 't', 't.id = i.team_id')
       ;
    return $this->findEntities($qb);
  }

}