Skip to content

Commit

Permalink
[endpoints] Adding new endpoint /films and Get Post handler for it. A…
Browse files Browse the repository at this point in the history
…dding new sql raw script for film database creating
  • Loading branch information
Nikolay Ryzhov committed Apr 23, 2024
1 parent 39fb36a commit 633fd2c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/postgres.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## commands:

CREATE DATABASE Filmoteka;
CREATE DATABASE filmoteka;

sudo -u postgres psql -d Filmoteka
sudo -u postgres psql -d filmoteka
8 changes: 8 additions & 0 deletions docs/sql/CreateFilmTable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE film (
id SERIAL PRIMARY KEY,
title VARCHAR(150) NOT NULL,
description TEXT,
release_date DATE,
rating INT,
actors INT[]
);
30 changes: 30 additions & 0 deletions src/server/endpoints/endpoints.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,34 @@ response EndPointsHandler::GetActors() {
return {200, response.dump(4)};
}

response EndPointsHandler::AddFilm(const Film& film) {
json response;
response["message"] = "Successfully adding a new user " + film.title;
const char *paramValues[5] = {film.title.c_str(), film.description.c_str(), film.release_date.c_str(), film.rating.c_str(), film.ids.c_str()};

prepare(database, "insert_query", "INSERT INTO film (title, description, release_date, rating, actors) VALUES ($1, $2, $3, $4, $5)", 5, nullptr);
execPrepared(database, "insert_query", 5, paramValues);

return {200, response.dump(4)};
}

response EndPointsHandler::GetFilms() {
json response;

PGresult* res = executeQuery(database, "SELECT * FROM film");

int32_t rows = PQntuples(res);
int32_t cols = PQnfields(res);
for (int32_t i = 0; i < rows; ++i) {
json part;
for (int32_t j = 0; j < cols; ++j) {
part[PQfname(res, j)] = PQgetvalue(res, i, j);
}
response += part;
}
PQclear(res);

return {200, response.dump(4)};
}

} // namespace fm
6 changes: 4 additions & 2 deletions src/server/endpoints/endpoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ struct Actor {
};

struct Film {
std::string id;
std::string title;
std::string description;
std::string release_date;
std::string rating;
std::vector<std::string> ids; // of actors
std::string ids; // of actors
};

class EndPointsHandler {
public:
EndPointsHandler();
response AddActor(const Actor& actor);
response GetActors();
response AddFilm(const Film& film);
response GetFilms();
private:
PGConnection database;
};
Expand Down
35 changes: 27 additions & 8 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,35 @@ void Server::Actors() {
});

CROW_ROUTE(app, "/actors").methods(crow::HTTPMethod::Post)([&](const crow::request& request) {
json req = json::parse(request.body);
json req = json::parse(request.body);

Actor actor{req["name"], req["gender"], req["date"]};
auto [code, body] = endpointsHandler.AddActor(actor);
Actor actor{req["name"], req["gender"], req["date"]};
auto [code, body] = endpointsHandler.AddActor(actor);

crow::response response(code, body);
response.set_header("Content-Type", "application/json");
return response;
});
crow::response response(code, body);
response.set_header("Content-Type", "application/json");
return response;
});
}
void Server::Films() {
CROW_ROUTE(app, "/films").methods(crow::HTTPMethod::Get)([&]() {
auto [code, body] = endpointsHandler.GetFilms();

crow::response response(code, body);
response.set_header("Content-Type", "application/json");
return response;
});

CROW_ROUTE(app, "/films").methods(crow::HTTPMethod::Post)([&](const crow::request& request) {
json req = json::parse(request.body);

Film film{req["title"], req["desription"], req["date"], req["rating"], req["actors"]};
auto [code, body] = endpointsHandler.AddFilm(film);

crow::response response(code, body);
response.set_header("Content-Type", "application/json");
return response;
});
}
void Server::Films(){/* TODO */};

} // namespace fm

0 comments on commit 633fd2c

Please sign in to comment.