AMIVEvent.swift 3.14 KB
Newer Older
domenicw's avatar
domenicw committed
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
82
83
84
//
//  AMIVEvent.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 23.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation

public struct AMIVEvent {
    
    public let id: String
    public let title: String
    public let catchPhrase: String
    public let location: String
    public let startTime: Date
    public let endTime: Date
    public let description: String
    public let price: Double
    public let spots: Int
    public let signupCount: Int
    public let image: AMIVMedia?
    
}

extension AMIVEvent: Decodable {
    
    private enum EventItemResponseCodingKeys: String, CodingKey {
        case id = "_id"
        case titleEn = "title_en"
        case titleDe = "title_de"
        case catchPhraseEn = "catchphrase_en"
        case catchPhraseDe = "catchphrase_de"
        case location = "location"
        case startTime = "time_start"
        case endTime = "time_end"
        case descriptionEn = "description_en"
        case descriptionDe = "description_de"
        case price = "price"
        case spots = "spots"
        case signupCount = "signup_count"
        case image = "img_poster"
    }
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: EventItemResponseCodingKeys.self)
        
        self.id = try container.decode(String.self, forKey: .id)
        
        let titleEn = try container.decode(String.self, forKey: .titleEn)
        let titleDe = try container.decode(String.self, forKey: .titleDe)
        self.title = Locale.getLocalizedString(english: titleEn, german: titleDe)
        
        let catchPhraseEn = try container.decode(String.self, forKey: .catchPhraseEn)
        let catchPhraseDe = try container.decode(String.self, forKey: .catchPhraseDe)
        self.catchPhrase = Locale.getLocalizedString(english: catchPhraseEn, german: catchPhraseDe)
        
        self.location = try container.decode(String.self, forKey: .location)
        
        if let startTime = try container.decode(String.self, forKey: .startTime).toDate() {
            self.startTime = startTime
        } else {
            throw DecodingError.dataCorrupted(.init(codingPath: [EventItemResponseCodingKeys.startTime], debugDescription: "Invalid date string"))
        }
        
        if let endTime = try container.decode(String.self, forKey: .endTime).toDate() {
            self.endTime = endTime
        } else {
            throw DecodingError.dataCorrupted(.init(codingPath: [EventItemResponseCodingKeys.endTime], debugDescription: "Invalid date string"))
        }
        
        let descriptionEn = try container.decode(String.self, forKey: .descriptionEn)
        let descriptionDe = try container.decode(String.self, forKey: .descriptionDe)
        self.description = Locale.getLocalizedString(english: descriptionEn, german: descriptionDe)
        
        self.price = try container.decode(Double.self, forKey: .price)
        self.spots = try container.decode(Int.self, forKey: .spots)
        self.signupCount = try container.decode(Int.self, forKey: .signupCount)
        
        self.image = try? container.decode(AMIVMedia.self, forKey: .image)
    }
    
}