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

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 "skill.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 Skill::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 metadata == other.metadata && value == other.value;
0037     }
0038 
0039     bool operator!=(const Private &other) const
0040     {
0041         return !(*this == other);
0042     }
0043 
0044     FieldMetadata metadata{};
0045     QString value{};
0046 };
0047 
0048 Skill::Skill()
0049     : d(new Private)
0050 {
0051 }
0052 
0053 Skill::Skill(const Skill &) = default;
0054 Skill::Skill(Skill &&) noexcept = default;
0055 Skill &Skill::operator=(const Skill &) = default;
0056 Skill &Skill::operator=(Skill &&) noexcept = default;
0057 Skill::~Skill() = default;
0058 
0059 bool Skill::operator==(const Skill &other) const
0060 {
0061     return *d == *other.d;
0062 }
0063 
0064 bool Skill::operator!=(const Skill &other) const
0065 {
0066     return !(*this == other);
0067 }
0068 
0069 FieldMetadata Skill::metadata() const
0070 {
0071     return d->metadata;
0072 }
0073 
0074 void Skill::setMetadata(const FieldMetadata &value)
0075 {
0076     d->metadata = value;
0077 }
0078 QString Skill::value() const
0079 {
0080     return d->value;
0081 }
0082 
0083 void Skill::setValue(const QString &value)
0084 {
0085     d->value = value;
0086 }
0087 
0088 Skill Skill::fromJSON(const QJsonObject &obj)
0089 {
0090     Skill skill;
0091 
0092     if(!obj.isEmpty()) {
0093         const auto metadata = obj.value(QStringLiteral("metadata")).toObject();
0094         skill.setMetadata(FieldMetadata::fromJSON(metadata));
0095         skill.setValue(obj.value(QStringLiteral("value")).toString());
0096     }
0097 
0098     return skill;
0099 }
0100 
0101 QList<Skill> Skill::fromJSONArray(const QJsonArray &data)
0102 {
0103     QList<Skill> skills;
0104 
0105     for(const auto &skill : data) {
0106         if(skill.isObject()) {
0107             const auto objectifiedSkill = skill.toObject();
0108             skills.append(fromJSON(objectifiedSkill));
0109         }
0110     }
0111 
0112     return skills;
0113 }
0114 
0115 QJsonValue Skill::toJSON() const
0116 {
0117     QJsonObject obj;
0118 
0119     // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
0120     PeopleUtils::addValueToJsonObjectIfValid(obj, "value", d->value);
0121     return obj;
0122 }
0123 
0124 } // namespace KGAPI2::People