123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include "worldcontroller.h"
- #include "world.h"
- WorldController::WorldController(const WorldController &)
- : ApplicationController()
- { }
- void WorldController::index()
- {
- QList<World> worldList = World::getAll();
- texport(worldList);
- render();
- }
- void WorldController::show(const QString &pk)
- {
- World world = World::get(pk.toUInt());
- texport(world);
- render();
- }
- void WorldController::queries(const QString &num)
- {
- QList<QVariantMap> worlds;
- int d = num.toInt();
- for (int i = 0; i < d; ++i) {
- int id = Tf::random(9999) + 1;
- World world = World::get(id);
- worlds << world.toVariantMap();
- }
- setContentType("application/json");
- renderText(jsonEncode(worlds), false);
- }
- void WorldController::random()
- {
- int id = Tf::random(9999) + 1;
- World world = World::get(id);
- setContentType("application/json");
- renderText(jsonEncode(world.toVariantMap()), false);
- }
- void WorldController::entry()
- {
- renderEntry();
- }
- void WorldController::create()
- {
- if (httpRequest().method() != Tf::Post) {
- return;
- }
- QVariantMap form = httpRequest().formItems("world");
- World world = World::create(form);
- if (!world.isNull()) {
- QString notice = "Created successfully.";
- tflash(notice);
- redirect(urla("show", world.id()));
- } else {
- QString error = "Failed to create.";
- texport(error);
- renderEntry(form);
- }
- }
- void WorldController::renderEntry(const QVariantMap &world)
- {
- texport(world);
- render("entry");
- }
- void WorldController::edit(const QString &pk)
- {
- World world = World::get(pk.toUInt());
- if (!world.isNull()) {
- renderEdit(world.toVariantMap());
- } else {
- redirect(urla("entry"));
- }
- }
- void WorldController::save(const QString &pk)
- {
- if (httpRequest().method() != Tf::Post) {
- return;
- }
- QString error;
- World world = World::get(pk.toUInt());
- if (world.isNull()) {
- error = "Original data not found. It may have been updated/removed by another transaction.";
- tflash(error);
- redirect(urla("edit", pk));
- return;
- }
- QVariantMap form = httpRequest().formItems("world");
- world.setProperties(form);
- if (world.save()) {
- QString notice = "Updated successfully.";
- tflash(notice);
- redirect(urla("show", pk));
- } else {
- error = "Failed to update.";
- texport(error);
- renderEdit(form);
- }
- }
- void WorldController::renderEdit(const QVariantMap &world)
- {
- texport(world);
- render("edit");
- }
- void WorldController::remove(const QString &pk)
- {
- if (httpRequest().method() != Tf::Post) {
- return;
- }
- World world = World::get(pk.toUInt());
- world.remove();
- redirect(urla("index"));
- }
- // Don't remove below this line
- T_REGISTER_CONTROLLER(worldcontroller)
|