Skip to content

Commit

Permalink
Add an entity for certifications (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
DevYeom authored Jan 30, 2025
1 parent f824dfb commit 9761d31
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/BadaApp/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct HomeView: View {
}
VStack(spacing: 16) {
InfoRow(
title: "License",
title: "Certification",
value: "Open water"
)
Divider()
Expand Down
74 changes: 74 additions & 0 deletions Sources/BadaData/Models/Certification/CertificationEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore
import BadaDomain
import SwiftData

@Model
final class CertificationEntity {
@Attribute(.unique)
var identifier: CertificationID
var agency: CertificationAgency
var level: CertificationLevel
var number: String
var date: Date

init(
identifier: CertificationID,
agency: CertificationAgency,
level: CertificationLevel,
number: String,
date: Date
) {
self.identifier = identifier
self.agency = agency
self.level = level
self.number = number
self.date = date
}
}

extension CertificationEntity: DomainConvertible {
var domain: BadaDomain.Certification {
BadaDomain.Certification(
identifier: identifier,
agency: agency.domain,
level: level.domain,
number: number,
date: date)
}

convenience init(domain: BadaDomain.Certification) {
self.init(
identifier: domain.identifier,
agency: CertificationAgency(domain: domain.agency),
level: CertificationLevel(domain: domain.level),
number: domain.number,
date: domain.date
)
}
}

extension CertificationEntity {
convenience init(insertRequest: BadaDomain.CertificationInsertRequest) {
self.init(
identifier: CertificationID(),
agency: CertificationAgency(domain: insertRequest.agency),
level: CertificationLevel(domain: insertRequest.level),
number: insertRequest.number,
date: insertRequest.date
)
}

func update(with request: CertificationUpdateRequest) {
agency = CertificationAgency(domain: request.agency)
level = CertificationLevel(domain: request.level)
number = request.number
date = request.date
}
}
17 changes: 14 additions & 3 deletions Sources/BadaData/PersistentStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ extension PersistentStore {
do {
let container = try ModelContainer(
for: DiveLogEntity.self,
CertificationEntity.self,
migrationPlan: PersistentStoreMigrationPlan.self,
configurations: ModelConfiguration(
"com.enuf.badabook.badadata.main",
schema: Schema([DiveLogEntity.self]),
schema: Schema([
DiveLogEntity.self,
CertificationEntity.self,
]),
isStoredInMemoryOnly: false,
allowsSave: true,
groupContainer: ModelConfiguration.GroupContainer.identifier("group.com.enuf.badabook.badadata.main"),
Expand All @@ -65,10 +69,14 @@ extension PersistentStore {
do {
let container = try ModelContainer(
for: DiveLogEntity.self,
CertificationEntity.self,
migrationPlan: PersistentStoreMigrationPlan.self,
configurations: ModelConfiguration(
"com.enuf.badabook.badadata.main-test",
schema: Schema([DiveLogEntity.self]),
schema: Schema([
DiveLogEntity.self,
CertificationEntity.self,
]),
isStoredInMemoryOnly: true,
allowsSave: true,
groupContainer: .none,
Expand Down Expand Up @@ -99,7 +107,10 @@ enum PersistentStoreMigrationPlan: SchemaMigrationPlan {

enum PersistentStoreSchemaV1: VersionedSchema {
static var models: [any PersistentModel.Type] {
[DiveLogEntity.self]
[
DiveLogEntity.self,
CertificationEntity.self,
]
}

static var versionIdentifier: Schema.Version {
Expand Down
76 changes: 76 additions & 0 deletions Sources/BadaData/Repositories/CertificationRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore
import BadaDomain
import SwiftData

package struct CertificationRepository: CertificationRepositoryType {
private let context: ModelContext

package init(persistentStore: PersistentStore) {
self.context = persistentStore.context
}

package func insert(request: CertificationInsertRequest) -> Result<Void, CertificationRepositoryError> {
let entity = CertificationEntity(insertRequest: request)
context.insert(entity)
do {
try context.save()
return .success(())
} catch {
return .failure(.insertFailed(error.localizedDescription))
}
}

package func update(request: CertificationUpdateRequest) -> Result<Void, CertificationRepositoryError> {
var descriptor = FetchDescriptor<CertificationEntity>()
let identifier = request.identifier
descriptor.predicate = #Predicate { certification in
certification.identifier == identifier
}
do {
let certifications = try context.fetch(descriptor)
if let certification = certifications.first {
certification.update(with: request)
try context.save()
return .success(())
} else {
return .failure(.noResult)
}
} catch {
return .failure(.updateFailed(error.localizedDescription))
}
}

package func fetchAll() -> Result<[Certification], CertificationRepositoryError> {
let descriptor = FetchDescriptor<CertificationEntity>()
do {
let certifications = try context.fetch(descriptor)
return .success(certifications.map(\.domain))
} catch {
return .failure(.fetchFailed(error.localizedDescription))
}
}

package func fetch(identifier: CertificationID) -> Result<Certification, CertificationRepositoryError> {
var descriptor = FetchDescriptor<CertificationEntity>()
descriptor.predicate = #Predicate { certification in
certification.identifier == identifier
}
do {
let certifications = try context.fetch(descriptor)
if let certification = certifications.first {
return .success(certification.domain)
} else {
return .failure(.noResult)
}
} catch {
return .failure(.fetchFailed(error.localizedDescription))
}
}
}
21 changes: 21 additions & 0 deletions Sources/BadaDomain/Interfaces/CertificationRepositoryType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore

@Repository
package protocol CertificationRepositoryType {
func insert(request: CertificationInsertRequest) -> Result<Void, CertificationRepositoryError>
func fetchAll() -> Result<[Certification], CertificationRepositoryError>
}

package enum CertificationRepositoryError: Error {
case insertFailed(String)
case fetchFailed(String)
case updateFailed(String)
case noResult
}
30 changes: 30 additions & 0 deletions Sources/BadaDomain/Models/Certification/Certification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import Foundation

package struct Certification: Equatable {
package let identifier: CertificationID
package let agency: CertificationAgency
package let level: CertificationLevel
package let number: String
package let date: Date

package init(
identifier: CertificationID,
agency: CertificationAgency,
level: CertificationLevel,
number: String,
date: Date
) {
self.identifier = identifier
self.agency = agency
self.level = level
self.number = number
self.date = date
}
}
10 changes: 10 additions & 0 deletions Sources/BadaDomain/Models/Certification/CertificationID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import Foundation

package typealias CertificationID = UUID
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore

package struct CertificationUpdateRequest: Equatable {
package let identifier: CertificationID
package let agency: CertificationAgency
package let level: CertificationLevel
package let number: String
package let date: Date

package init(
identifier: CertificationID,
agency: CertificationAgency,
level: CertificationLevel,
number: String,
date: Date
) {
self.identifier = identifier
self.agency = agency
self.level = level
self.number = number
self.date = date
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// BadaBook
// Apache License, Version 2.0
//
// Copyright (c) 2024 Seungyeop Yeom ( https://github.com/OceanPositive ).
//

import BadaCore

package struct CertificationInsertRequest: Equatable {
package let agency: CertificationAgency
package let level: CertificationLevel
package let number: String
package let date: Date

package init(
agency: CertificationAgency,
level: CertificationLevel,
number: String,
date: Date
) {
self.agency = agency
self.level = level
self.number = number
self.date = date
}
}
Loading

0 comments on commit 9761d31

Please sign in to comment.