web-queue-api / src / api.cpp
api.cpp
Raw
#include <iostream>
#include <list>
#include <iterator>
// #include <List.h>
#include "json.hpp"

using nlohmann::json;
using namespace std;

class OHQueue {
public:
  void run();
  void read_request();
  // void read_list();
  // void write_list();
  void write_request();
  json create_output();
  void update_positions();
  void error_400();
private:
  struct Student {
    string location;
    int position;
    string uniqname;
  };
  std::list<Student> queue;
  //json output;
  string method;
  string endpoint;
  string trash;
  Student student;
};

int main() {
  OHQueue OH;
  OH.run();
}

void OHQueue::run() {
  while (cin >> method >> endpoint >> 
        trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash) {
    read_request();
    // write_list();
    write_request();
  }
}

void OHQueue::read_request() {
  // if (method == "GET" || method == "DELETE") {
  //   cin >> trash >> trash >> trash >> trash >> trash >> trash >> trash >> trash;
  // }
  if (method == "POST" && endpoint == "/api/queue/tail/") {
    json j;
    cin >> j;
    student.uniqname = j["uniqname"];
    student.location = j["location"];
    student.position = queue.size() + 1;
  }
}

// void OHQueue::read_list() {

// }

// void OHQueue::write_list() {
//   if (method == "POST" && endpoint == "/api/queue/tail/") {
//     queue.push_back(student);
//   }
//   if (method == "DELETE" && endpoint == "/api/queue/head/") {

//   }
// }

void OHQueue::write_request() {
  if (method == "GET") {
    if (endpoint == "/api/") {
      json j = {
        {"queue_head_url", "http://localhost/queue/head/"},
        {"queue_list_url", "http://localhost/queue/"},
        {"queue_tail_url", "http://localhost/queue/tail/"}
      };
      string j_str = j.dump(4) + "\n";
      cout << "HTTP/1.1 200 OK" << endl <<
              "Content-Type: application/json; charset=utf-8" << endl <<
              "Content-Length: " << j_str.length() << endl << endl;
      cout << j_str;
    }
    else if (endpoint == "/api/queue/") {
      json j = {
        {"count", queue.size()},
        {"results", create_output()}
      };
      string j_str = j.dump(4) + "\n";
      cout << "HTTP/1.1 200 OK" << endl <<
              "Content-Type: application/json; charset=utf-8" << endl <<
              "Content-Length: " << j_str.length() << endl << endl;
      cout << j_str; 
    }
    else if (endpoint == "/api/queue/head/") {
      json j = {
        {"location", queue.front().location},
        {"position", queue.front().position},
        {"uniqname", queue.front().uniqname}
      };
      string j_str = j.dump(4) + "\n";
      cout << "HTTP/1.1 200 OK" << endl <<
              "Content-Type: application/json; charset=utf-8" << endl <<
              "Content-Length: " << j_str.length() << endl << endl;
      cout << j_str;  
    }
    else {error_400();}
  }
  else if (method == "POST" && endpoint == "/api/queue/tail/") {
    queue.push_back(student);
    json j = {
      {"location", student.location},
      {"position", student.position},
      {"uniqname", student.uniqname}
    };
    //output.push_back(j);
    string j_str = j.dump(4) + "\n";
    cout << "HTTP/1.1 201 Created" << endl <<
            "Content-Type: application/json; charset=utf-8" << endl <<
            "Content-Length: " << j_str.length() << endl << endl;
    cout << j_str;
  }
  else if (method == "DELETE" && endpoint == "/api/queue/head/") {
    if (queue.size() == 0) {error_400();}
    else {
      //output.pop_front();
      queue.pop_front();
      update_positions();
      cout << "HTTP/1.1 204 No Content" << endl <<
              "Content-Type: application/json; charset=utf-8" << endl <<
              "Content-Length: 0" << endl << endl;
    }
  }
  else {error_400();}
}

json OHQueue::create_output() {
  json output;
  list<Student>::iterator it;
  for (it = queue.begin(); it != queue.end(); it++) {
    json j_it = {
      {"location", (*it).location},
      {"position", (*it).position},
      {"uniqname", (*it).uniqname}
    };
    output.push_back(j_it);
  }
  return output;
}

void OHQueue::update_positions() {
  list<Student>::iterator it;
  for (it = queue.begin(); it != queue.end(); it++) {
    (*it).position--;
  }
}

void OHQueue::error_400() {
  cout << "HTTP/1.1 400 Bad Request" << endl <<
          "Content-Type: application/json; charset=utf-8" << endl <<
          "Content-Length: 0"  << endl << endl;
}