File indexing completed on 2024-05-12 05:22:27

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
0003  * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only
0006  * SPDX-License-Identifier: LGPL-3.0-only
0007  * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
0008  */
0009 
0010 #include "occupation.h"
0011 
0012 #include "fieldmetadata.h"
0013 #include "peopleservice.h"
0014 
0015 #include <QJsonArray>
0016 #include <QJsonObject>
0017 #include <QJsonValue>
0018 #include <QSharedData>
0019 
0020 #include <algorithm>
0021 
0022 namespace KGAPI2::People
0023 {
0024 class Occupation::Private : public QSharedData
0025 {
0026 public:
0027     explicit Private() = default;
0028     Private(const Private &) = default;
0029     Private(Private &&) noexcept = delete;
0030     Private &operator=(const Private &) = delete;
0031     Private &operator=(Private &&) noexcept = delete;
0032     ~Private() = default;
0033 
0034     bool operator==(const Private &other) const
0035     {
0036         return value == other.value && metadata == other.metadata;
0037     }
0038 
0039     bool operator!=(const Private &other) const
0040     {
0041         return !(*this == other);
0042     }
0043 
0044     QString value{};
0045     FieldMetadata metadata{};
0046 };
0047 
0048 Occupation::Occupation()
0049     : d(new Private)
0050 {
0051 }
0052 
0053 Occupation::Occupation(const Occupation &) = default;
0054 Occupation::Occupation(Occupation &&) noexcept = default;
0055 Occupation &Occupation::operator=(const Occupation &) = default;
0056 Occupation &Occupation::operator=(Occupation &&) noexcept = default;
0057 Occupation::~Occupation() = default;
0058 
0059 bool Occupation::operator==(const Occupation &other) const
0060 {
0061     return *d == *other.d;
0062 }
0063 
0064 bool Occupation::operator!=(const Occupation &other) const
0065 {
0066     return !(*this == other);
0067 }
0068 
0069 QString Occupation::value() const
0070 {
0071     return d->value;
0072 }
0073 
0074 void Occupation::setValue(const QString &value)
0075 {
0076     d->value = value;
0077 }
0078 FieldMetadata Occupation::metadata() const
0079 {
0080     return d->metadata;
0081 }
0082 
0083 void Occupation::setMetadata(const FieldMetadata &value)
0084 {
0085     d->metadata = value;
0086 }
0087 
0088 Occupation Occupation::fromJSON(const QJsonObject &obj)
0089 {
0090     Occupation occupation;
0091 
0092     if(!obj.isEmpty()) {
0093         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0094         occupation.setMetadata(FieldMetadata::fromJSON(metadata));
0095         occupation.setValue(obj.value(QStringLiteral("value")).toString());
0096     }
0097 
0098     return occupation;
0099 }
0100 
0101 QList<Occupation> Occupation::fromJSONArray(const QJsonArray &data)
0102 {
0103     QList<Occupation> occupations;
0104 
0105     for(const auto &occupation : data) {
0106         if(occupation.isObject()) {
0107             const auto objectifiedOccupation = occupation.toObject();
0108             occupations.append(fromJSON(objectifiedOccupation));
0109         }
0110     }
0111 
0112     return occupations;
0113 }
0114 
0115 QJsonValue Occupation::toJSON() const
0116 {
0117     QJsonObject obj;
0118 
0119     PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
0120     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata"}, d->metadata.toJSON());
0121     return obj;
0122 }
0123 
0124 } // namespace KGAPI2::People