mngfortune.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <TreeFrogModel>
  2. #include "mngfortune.h"
  3. #include "mngfortuneobject.h"
  4. MngFortune::MngFortune()
  5. : TAbstractModel(), d(new MngFortuneObject)
  6. {
  7. d->id = 0;
  8. }
  9. MngFortune::MngFortune(const MngFortune &other)
  10. : TAbstractModel(), d(new MngFortuneObject(*other.d))
  11. { }
  12. MngFortune::MngFortune(const MngFortuneObject &object)
  13. : TAbstractModel(), d(new MngFortuneObject(object))
  14. { }
  15. MngFortune::~MngFortune()
  16. {
  17. // If the reference count becomes 0,
  18. // the shared data object 'MngFortuneObject' is deleted.
  19. }
  20. int MngFortune::id() const
  21. {
  22. return d->id;
  23. }
  24. QString MngFortune::message() const
  25. {
  26. return d->message;
  27. }
  28. void MngFortune::setMessage(const QString &message)
  29. {
  30. d->message = message;
  31. }
  32. MngFortune &MngFortune::operator=(const MngFortune &other)
  33. {
  34. d = other.d; // increments the reference count of the data
  35. return *this;
  36. }
  37. MngFortune MngFortune::create(const QString &message)
  38. {
  39. MngFortuneObject obj;
  40. obj.message = message;
  41. if (!obj.create()) {
  42. return MngFortune();
  43. }
  44. return MngFortune(obj);
  45. }
  46. MngFortune MngFortune::create(const QVariantMap &values)
  47. {
  48. MngFortune model;
  49. model.setProperties(values);
  50. if (!model.d->create()) {
  51. model.d->clear();
  52. }
  53. return model;
  54. }
  55. MngFortune MngFortune::get(const QString &id)
  56. {
  57. TMongoODMapper<MngFortuneObject> mapper;
  58. return MngFortune(mapper.findByObjectId(id));
  59. }
  60. int MngFortune::count()
  61. {
  62. TMongoODMapper<MngFortuneObject> mapper;
  63. return mapper.findCount();
  64. }
  65. QList<MngFortune> MngFortune::getAll()
  66. {
  67. return tfGetModelListByMongoCriteria<MngFortune, MngFortuneObject>(TCriteria());
  68. }
  69. QJsonArray MngFortune::getAllJson()
  70. {
  71. QJsonArray array;
  72. TMongoODMapper<MngFortuneObject> mapper;
  73. if (mapper.find()) {
  74. while (mapper.next()) {
  75. array.append(QJsonValue(QJsonObject::fromVariantMap(MngFortune(mapper.value()).toVariantMap())));
  76. }
  77. }
  78. return array;
  79. }
  80. TModelObject *MngFortune::modelData()
  81. {
  82. return d.data();
  83. }
  84. const TModelObject *MngFortune::modelData() const
  85. {
  86. return d.data();
  87. }