generated from DevYeom/swift-app-package
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an entity for certifications (#52)
- Loading branch information
Showing
15 changed files
with
447 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
Sources/BadaData/Models/Certification/CertificationEntity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Sources/BadaData/Repositories/CertificationRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
Sources/BadaDomain/Interfaces/CertificationRepositoryType.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
Sources/BadaDomain/Models/Certification/Certification.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
Sources/BadaDomain/Models/Certification/CertificationID.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
30 changes: 30 additions & 0 deletions
30
Sources/BadaDomain/Models/Certification/CertificationInsertRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
File renamed without changes.
27 changes: 27 additions & 0 deletions
27
Sources/BadaDomain/Models/Certification/CertificationUpdateRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.