SqliteInteractionDao.swift 95 lignes · 3300 octets
import Foundation

struct SqliteInteractionDao: InteractionDao {
    let db: SqliteDatabase

    private static let columns = """
    localId, serverId, contactServerId, type, sujet, description, creePar, createdAt, updatedAt,
    audioPath, transcriptionStatut, transcriptionErreur
    """

    private static func map(_ row: SqliteRow) -> InteractionEntity {
        var e = InteractionEntity()
        e.localId = row.int64(0)
        e.serverId = row.textOrNil(1)
        e.contactServerId = row.text(2)
        e.type = row.text(3)
        e.sujet = row.text(4)
        e.description = row.text(5)
        e.creePar = row.text(6)
        e.createdAt = row.int64(7)
        e.updatedAt = row.int64OrNil(8)
        e.audioPath = row.textOrNil(9)
        e.transcriptionStatut = row.textOrNil(10)
        e.transcriptionErreur = row.textOrNil(11)
        return e
    }

    @discardableResult
    func upsert(_ entity: InteractionEntity) async throws -> Int64 {
        try await db.insert(
            """
            INSERT OR REPLACE INTO interactions (\(Self.columns))
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """,
            [
                .rowId(entity.localId),
                .optText(entity.serverId),
                .text(entity.contactServerId),
                .text(entity.type),
                .text(entity.sujet),
                .text(entity.description),
                .text(entity.creePar),
                .int(entity.createdAt),
                .optInt(entity.updatedAt),
                .optText(entity.audioPath),
                .optText(entity.transcriptionStatut),
                .optText(entity.transcriptionErreur),
            ]
        )
    }

    func getByServerId(_ serverId: String) async throws -> InteractionEntity? {
        try await db.query(
            "SELECT \(Self.columns) FROM interactions WHERE serverId = ?",
            [.text(serverId)],
            map: Self.map
        ).first
    }

    func getByLocalId(_ localId: Int64) async throws -> InteractionEntity? {
        try await db.query(
            "SELECT \(Self.columns) FROM interactions WHERE localId = ?",
            [.int(localId)],
            map: Self.map
        ).first
    }

    func deleteByServerId(_ serverId: String) async throws {
        try await db.write("DELETE FROM interactions WHERE serverId = ?", [.text(serverId)])
    }

    func deleteByLocalId(_ localId: Int64) async throws {
        try await db.write("DELETE FROM interactions WHERE localId = ?", [.int(localId)])
    }

    func listByContactServerId(_ contactServerId: String) async throws -> [InteractionEntity] {
        try await db.query(
            "SELECT \(Self.columns) FROM interactions WHERE contactServerId = ?",
            [.text(contactServerId)],
            map: Self.map
        )
    }

    func fetchByContactServerId(_ contactServerId: String) async throws -> [InteractionEntity] {
        try await db.query(
            "SELECT \(Self.columns) FROM interactions WHERE contactServerId = ? ORDER BY createdAt DESC",
            [.text(contactServerId)],
            map: Self.map
        )
    }

    func listAll() async throws -> [InteractionEntity] {
        try await db.query("SELECT \(Self.columns) FROM interactions", map: Self.map)
    }
}