summaryrefslogtreecommitdiffstats
path: root/lib/Db/FolderMapper.php
blob: 75b749974353a91358e685f77c401b25dd7ee424 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
 * Nextcloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author    Alessandro Cosentino <cosenal@gmail.com>
 * @author    Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright 2012 Alessandro Cosentino
 * @copyright 2012-2014 Bernhard Posselt
 */

namespace OCA\News\Db;

use OCA\News\Utility\Time;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IDBConnection;
use OCP\AppFramework\Db\Entity;

/**
 * Class LegacyFolderMapper
 *
 * @package OCA\News\Db
 * @deprecated use FolderMapper
 */
class FolderMapper extends NewsMapper
{

    const TABLE_NAME = 'news_folders';

    public function __construct(IDBConnection $db, Time $time)
    {
        parent::__construct($db, $time, Folder::class);
    }

    public function find(string $userId, int $id)
    {
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
            'WHERE `id` = ? ' .
            'AND `user_id` = ?';

        return $this->findEntity($sql, [$id, $userId]);
    }


    public function findAllFromUser(string $userId): array
    {
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
            'WHERE `user_id` = ? ' .
            'AND `deleted_at` = 0';
        $params = [$userId];

        return $this->findEntities($sql, $params);
    }


    public function findByName(string $folderName, string $userId)
    {
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
            'WHERE `name` = ? ' .
            'AND `user_id` = ?';
        $params = [$folderName, $userId];

        return $this->findEntities($sql, $params);
    }


    public function delete(Entity $entity): Entity
    {
        parent::delete($entity);

        // someone please slap me for doing this manually :P
        // we needz CASCADE + FKs please
        $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?';
        $params = [$entity->getId()];
        $stmt = $this->execute($sql, $params);
        $stmt->closeCursor();

        $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN ' .
            '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)';

        $stmt = $this->execute($sql);
        $stmt->closeCursor();

        return $entity;
    }


    /**
     * @param int    $deleteOlderThan if given gets all entries with a delete date
     *                                older than that timestamp
     * @param string $userId          if given returns only entries from the given user
     * @return array with the database rows
     */
    public function getToDelete($deleteOlderThan = null, $userId = null)
    {
        $sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
            'WHERE `deleted_at` > 0 ';
        $params = [];

        // sometimes we want to delete all entries
        if ($deleteOlderThan !== null) {
            $sql .= 'AND `deleted_at` < ? ';
            $params[] = $deleteOlderThan;
        }

        // we need to sometimes only delete feeds of a user
        if ($userId !== null) {
            $sql .= 'AND `user_id` = ?';
            $params[] = $userId;
        }

        return $this->findEntities($sql, $params);
    }


    /**
     * Deletes all folders of a user
     *
     * @param string $userId the name of the user
     */
    public function deleteUser(string $userId)
    {
        $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?';
        $this->execute($sql, [$userId]);
    }

    /**
     * NO-OP
     * @return array
     */
    public function findAll(): array
    {
        return [];
    }

    public function findFromUser(string $userId, int $id): Entity
    {
        return $this->find($id, $userId);
    }
}