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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
// *****************************************************************************
/*!
  \file      src/document.hpp
  \copyright 2022-2025 J. Bakosi,
             All rights reserved. See the LICENSE file for details.
  \brief     Piac document classes to interact with database library
*/
// *****************************************************************************

#pragma once

#include "jsonbase.hpp"

namespace piac {

//! Document class to hold a database document and help with JSON serialization
class Document : public JSONBase {
  public:
    //! Deserialize document state from JSON object
    bool deserialize( const rapidjson::Value& obj ) override;

    bool deserialize( const std::string& s ) override {
      return JSONBase::deserialize( s );
    }

    bool serialize( rapidjson::Writer<rapidjson::StringBuffer>* writer )
      const override;

    [[nodiscard]] std::string serialize() const override {
      return JSONBase::serialize();
    }

    int id() const { return m_id; }
    void id( int i ) { m_id = i; }

    const std::string& title() const { return m_title; }
    void title( const std::string& t ) { m_title = t; }

    const std::string& author() const { return m_author; }
    void author( const std::string& a ) { m_author = a; }

    const std::string& description() const { return m_description; }
    void description( const std::string& d ) { m_description = d; }

    double price() const { return m_price; }
    void price( double p ) { m_price = p; }

    const std::string& category() const { return m_category; }
    void category( const std::string& t ) { m_category = t; }

    const std::string& condition() const { return m_condition; }
    void condition( const std::string& t ) { m_condition = t; }

    const std::string& shipping() const { return m_shipping; }
    void shipping( const std::string& t ) { m_shipping = t; }

    const std::string& format() const { return m_format; }
    void format( const std::string& t ) { m_format = t; }

    const std::string& location() const { return m_location; }
    void location( const std::string& t ) { m_location = t; }

    const std::string& keywords() const { return m_keywords; }
    void keywords( const std::string& t ) { m_keywords = t; }

    // SHA is not serialized, but regenerated when needed
    const std::string& sha() const { return m_sha; }
    void sha( const std::string& s ) { m_sha = s; }

  private:
    int m_id;
    std::string m_title;
    std::string m_author;
    std::string m_description;
    double m_price;
    std::string m_category;
    std::string m_condition;
    std::string m_shipping;
    std::string m_format;
    std::string m_location;
    std::string m_keywords;
    std::string m_sha;
};

//! Multiple document hold a list of database documents
class Documents : public JSONBase {
  public:
    bool deserialize( const std::string& s ) override;

    bool deserialize( const rapidjson::Value& ) override { return false; }

    bool serialize( rapidjson::Writer<rapidjson::StringBuffer>* writer )
      const override;

    [[nodiscard]] std::string serialize() const override {
      return JSONBase::serialize();
    }

    const std::vector< Document >& documents() const { return m_documents; }
    std::vector< Document >& documents() { return m_documents; }

    const Document& operator[]( std::size_t i ) const { return m_documents[i]; }
    Document& operator[]( std::size_t i ) {
      return const_cast< Document& >(
               static_cast< const Documents& >( *this ).operator[]( i ) );
    }

  private:
    std::vector< Document > m_documents;
};

} // piac::