NCManageDatabase+Groupfolders.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // NCManageDatabase+LayoutForView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/11/22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import RealmSwift
  25. import NextcloudKit
  26. class TableGroupfolders: Object {
  27. @Persisted var account = ""
  28. @Persisted var acl: Bool = false
  29. @Persisted var groups: List<TableGroupfoldersGroups>
  30. @Persisted var id: Int = 0
  31. @Persisted var manage: Data?
  32. @Persisted var mountPoint = ""
  33. @Persisted var quota: Int = 0
  34. @Persisted var size: Int = 0
  35. }
  36. class TableGroupfoldersGroups: Object {
  37. @Persisted var account = ""
  38. @Persisted var group = ""
  39. @Persisted var permission: Int = 0
  40. convenience init(account: String, group: String, permission: Int) {
  41. self.init()
  42. self.account = account
  43. self.group = group
  44. self.permission = permission
  45. }
  46. }
  47. extension NCManageDatabase {
  48. func addGroupfolders(account: String, groupfolders: [NKGroupfolders]) {
  49. do {
  50. let realm = try Realm()
  51. try realm.write {
  52. let tableGroupfolders = realm.objects(TableGroupfolders.self).filter("account == %@", account)
  53. realm.delete(tableGroupfolders)
  54. let tableGroupfoldersGroups = realm.objects(TableGroupfoldersGroups.self).filter("account == %@", account)
  55. realm.delete(tableGroupfoldersGroups)
  56. for groupfolder in groupfolders {
  57. let obj = TableGroupfolders()
  58. obj.account = account
  59. obj.acl = groupfolder.acl
  60. for group in groupfolder.groups ?? [:] {
  61. let objGroups = TableGroupfoldersGroups(account: account, group: group.key, permission: (group.value as? Int ?? 0))
  62. obj.groups.append(objGroups)
  63. }
  64. obj.id = groupfolder.id
  65. obj.manage = groupfolder.manage
  66. obj.mountPoint = groupfolder.mountPoint
  67. obj.quota = groupfolder.quota
  68. obj.size = groupfolder.size
  69. realm.add(obj)
  70. }
  71. }
  72. } catch let error {
  73. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  74. }
  75. }
  76. }