ProfileDataHelper.swift 5.61 KB
Newer Older
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
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

import SQLite
import SwiftyBeaver

typealias Profile = (
    id: Int64,
    uri: String,
    alias: String?,
    photo: String?,
    type: String,
    status: String
)

final class ProfileDataHelper {
    let table = RingDB.instance.tableProfiles
    let id = Expression<Int64>("id")
    let uri = Expression<String>("uri")
    let alias = Expression<String?>("alias")
    let photo = Expression<String?>("photo")
    let type = Expression<String>("type")
    let status = Expression<String>("status")
    private let log = SwiftyBeaver.self

    func createTable() throws {
        guard let dataBase = RingDB.instance.ringDB else {
            throw DataAccessError.datastoreConnectionError
        }
        do {
            try dataBase.run(table.create(ifNotExists: true) { table in
                table.column(id, primaryKey: .autoincrement)
                table.column(uri)
                table.column(alias)
                table.column(photo)
                table.column(type)
                table.column(status)
            })
        } catch _ {
            log.error("Table exists")
        }
    }

    func insert(item: Profile) -> Bool {
        guard let dataBase = RingDB.instance.ringDB else {
            return false
        }

        let query = table.insert(uri <- item.uri,
                                  alias <- item.alias,
                                  photo <- item.photo,
                                  type <- item.type,
                                  status <- item.status)
        do {
            let rowId = try dataBase.run(query)
            guard rowId > 0 else {
                return false
            }
            return true
        } catch _ {
            return false
        }

    }

    func delete(item: Profile) -> Bool {
        guard let dataBase = RingDB.instance.ringDB  else {
            return false
        }
        let profileId = item.id
        let query = table.filter(id == profileId)
        do {
            let deletedRows = try dataBase.run(query.delete())
            guard deletedRows == 1 else {
                return false
            }
            return true
        } catch _ {
            return false
        }
    }

    func selectProfile(profileId: Int64) throws -> Profile? {
        guard let dataBase = RingDB.instance.ringDB else {
            throw DataAccessError.datastoreConnectionError
        }
        let query = table.filter(id == profileId)
        let items = try dataBase.prepare(query)
        for item in  items {
            return Profile(id: item[id], uri: item[uri], alias: item[alias],
                           photo: item[photo], type: item[type], status: item[status])
        }
        return nil
    }

    func selectAll() throws -> [Profile]? {
        guard let dataBase = RingDB.instance.ringDB else {
            throw DataAccessError.datastoreConnectionError
        }
        var profiles = [Profile]()
        let items = try dataBase.prepare(table)
        for item in items {
            profiles.append(Profile(id: item[id], uri: item[uri], alias: item[alias],
                                    photo: item[photo], type: item[type], status: item[status]))
        }
        return profiles
    }

    func selectProfile(accountURI: String) throws -> Profile? {
        guard let dataBase = RingDB.instance.ringDB else {
            throw DataAccessError.datastoreConnectionError
        }
        let query = table.filter(uri == accountURI)
        let items = try dataBase.prepare(query)
        // for one URI we should have only one profile
        for item in  items {
            return Profile(id: item[id], uri: item[uri], alias: item[alias],
                           photo: item[photo], type: item[type], status: item[status])
        }
        return nil
    }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    func insertOrUpdateProfile(item: Profile) throws {
        guard let dataBase = RingDB.instance.ringDB else {
            throw DataAccessError.datastoreConnectionError
        }
        try dataBase.transaction {
            let selectQuery = table.filter(uri == item.uri)
            let rows = try dataBase.run(selectQuery.update(alias <- item.alias,
                                                           photo <- item.photo,
                                                           status <- item.status))
            if rows > 0 {
                return
            }
            let insertQuery = table.insert(uri <- item.uri,
                                           alias <- item.alias,
                                           photo <- item.photo,
                                           type <- item.type,
                                           status <- item.status)
            let rowId = try dataBase.run(insertQuery)
            guard rowId > 0 else {
                throw DataAccessError.databaseError
            }
        }
    }
163
}