-
Notifications
You must be signed in to change notification settings - Fork 194
/
ChannelMuteDTO.swift
81 lines (65 loc) 路 3.09 KB
/
ChannelMuteDTO.swift
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
//
// Copyright 漏 2021 Stream.io Inc. All rights reserved.
//
import CoreData
import Foundation
@objc(ChannelMuteDTO)
final class ChannelMuteDTO: NSManagedObject {
@NSManaged var createdAt: Date
@NSManaged var updatedAt: Date?
@NSManaged var channel: ChannelDTO
@NSManaged var user: UserDTO
static func fetchRequest(userId: String) -> NSFetchRequest<ChannelMuteDTO> {
let request = NSFetchRequest<ChannelMuteDTO>(entityName: ChannelMuteDTO.entityName)
request.predicate = NSPredicate(format: "user.id == %@", userId)
return request
}
static func fetchRequest(for cid: ChannelId) -> NSFetchRequest<ChannelMuteDTO> {
let request = NSFetchRequest<ChannelMuteDTO>(entityName: ChannelMuteDTO.entityName)
request.predicate = NSPredicate(format: "channel.cid == %@", cid.rawValue)
return request
}
static func fetchRequest(for cid: ChannelId, userId: String) -> NSFetchRequest<ChannelMuteDTO> {
let request = NSFetchRequest<ChannelMuteDTO>(entityName: ChannelMuteDTO.entityName)
request.predicate = NSPredicate(format: "channel.cid == %@ && user.id == %@", cid.rawValue, userId)
return request
}
static func load(userId: String, context: NSManagedObjectContext) -> [ChannelMuteDTO] {
load(by: fetchRequest(userId: userId), context: context)
}
static func load(cid: ChannelId, context: NSManagedObjectContext) -> [ChannelMuteDTO] {
load(by: fetchRequest(for: cid), context: context)
}
static func load(cid: ChannelId, userId: String, context: NSManagedObjectContext) -> ChannelMuteDTO? {
load(by: fetchRequest(for: cid, userId: userId), context: context).first
}
static func loadOrCreate(cid: ChannelId, userId: String, context: NSManagedObjectContext) -> ChannelMuteDTO {
if let existing = load(cid: cid, userId: userId, context: context) {
return existing
}
let new = NSEntityDescription.insertNewObject(forEntityName: Self.entityName, into: context) as! ChannelMuteDTO
new.channel = ChannelDTO.loadOrCreate(cid: cid, context: context)
new.user = UserDTO.loadOrCreate(id: userId, context: context)
return new
}
}
extension NSManagedObjectContext {
@discardableResult
func saveChannelMute(payload: MutedChannelPayload) throws -> ChannelMuteDTO {
let dto = ChannelMuteDTO.loadOrCreate(cid: payload.mutedChannel.cid, userId: payload.user.id, context: self)
dto.user = try saveUser(payload: payload.user)
dto.channel = try saveChannel(payload: payload.mutedChannel, query: nil)
dto.createdAt = payload.createdAt
dto.updatedAt = payload.updatedAt
return dto
}
func loadChannelMute(cid: ChannelId, userId: String) -> ChannelMuteDTO? {
ChannelMuteDTO.load(cid: cid, userId: userId, context: self)
}
func loadChannelMutes(for userId: UserId) -> [ChannelMuteDTO] {
ChannelMuteDTO.load(userId: userId, context: self)
}
func loadChannelMutes(for cid: ChannelId) -> [ChannelMuteDTO] {
ChannelMuteDTO.load(cid: cid, context: self)
}
}