Skip to content

Commit

Permalink
Merge pull request #41 from Serveis-Neby/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
JayexDesigns authored Mar 29, 2024
2 parents 6e3bb61 + 21c6580 commit aaded12
Show file tree
Hide file tree
Showing 56 changed files with 2,206 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
39 changes: 32 additions & 7 deletions CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.18)

project(backend)

set(CMAKE_CXX_STANDARD 20)

# Add executable
add_executable(backend main.cpp
routes/test_routes.cpp
controllers/test_controller.cpp
# Agrega las opciones de compilación
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Encuentra las bibliotecas necesarias
find_library(PQXX_LIB pqxx)
find_library(PQ_LIB pq)
find_library(BCRYPT_LIB bcrypt)

include(FetchContent)
fetchcontent_declare(jwt-cpp
GIT_REPOSITORY https://github.com/Thalhammer/jwt-cpp.git
GIT_TAG 08bcf77a687fb06e34138e9e9fa12a4ecbe12332 # v0.7.0 release
)
set(JWT_BUILD_EXAMPLES OFF CACHE BOOL "disable building examples" FORCE)
fetchcontent_makeavailable(jwt-cpp)
# Si bcrypt está instalado en un lugar no estándar, puedes usar find_path para encontrar la carpeta de inclusión
# find_path(BCRYPT_INCLUDE_DIR bcrypt/bcrypt.h)

# Establece las fuentes de tu proyecto
file(GLOB_RECURSE SOURCES "src/*.cpp")

# Crea el ejecutable
add_executable(backend ${SOURCES})

# Agrega las carpetas de inclusión
target_include_directories(backend PUBLIC include)

# Enlaza las bibliotecas necesarias
target_link_libraries(backend ${PQXX_LIB} ${PQ_LIB} ${BCRYPT_LIB} jwt-cpp::jwt-cpp)

# Include Crow header files
target_include_directories(backend PUBLIC ${CROW_INCLUDE_DIRS})
# Si bcrypt está en un lugar no estándar, también agrega el directorio de inclusión
# target_include_directories(backend PUBLIC ${BCRYPT_INCLUDE_DIR})
27 changes: 27 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM gcc:latest

RUN apt-get update && apt-get install -y libpqxx-dev libboost-dev cmake inotify-tools git

WORKDIR /app

COPY crow-v1.0+5.deb .

RUN dpkg -i crow-v1.0+5.deb

COPY . .

RUN apt install -y nlohmann-json3-dev libgtest-dev libssl-dev

RUN git clone https://github.com/arun11299/cpp-jwt.git &&\
git clone https://github.com/trusch/libbcrypt.git && \
cd libbcrypt && \
mkdir build && \
cd build && \
cmake .. && \
make && \
make install && \
ldconfig

RUN chmod 777 hot-reload.sh

CMD ["./hot-reload.sh", "./backend"]
1 change: 0 additions & 1 deletion Dockerfile → Dockerfile.prod
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ RUN dpkg -i crow-v1.0+5.deb

COPY . .

# RUN g++ -std=c++20 -o main main.cpp -lpqxx -lpq
RUN cmake .

RUN make
Expand Down
17 changes: 17 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM gcc:latest

RUN apt-get update && apt-get install -y libpqxx-dev libboost-dev cmake inotify-tools git

WORKDIR /app

COPY . .

RUN cd test && \
git submodule add https://github.com/nlohmann/json.git extern/json && \
git submodule add https://github.com/google/googletest.git extern/googletest &&\
git submodule add https://github.com/whoshuu/cpr.git extern/cpr && \
ldconfig

RUN chmod 777 hot-reload.sh

CMD ["./hot-reload.sh", "./test", "./test/"]
Empty file modified LICENSE
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Backend
5 changes: 0 additions & 5 deletions controllers/test_controller.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions controllers/test_controller.h

This file was deleted.

Empty file modified crow-v1.0+5.deb
100644 → 100755
Empty file.
48 changes: 48 additions & 0 deletions hot-reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

if [ $# -eq 0 ]; then
echo "Usage: $0 <file_path> [<directory_path>]"
exit 1
fi

compile_backend() {
echo "Compiling..."
cmake .
make
}

run_backend() {
"$1" &
BACKEND_PID=$!
echo "Running process with PID: $BACKEND_PID"
}

monitor_changes() {
echo "Monitoring for changes..."
while true; do
inotifywait -r -e modify,move,create,delete ./
kill_backend
compile_backend
run_backend $1
done
}

kill_backend() {
if [[ -n $BACKEND_PID ]]; then
echo "Killing process with PID: $BACKEND_PID"
kill $BACKEND_PID
wait $BACKEND_PID 2>/dev/null
fi
}

main() {
if [ -d "$2" ]; then
cd "$2" || exit 1
fi

compile_backend
run_backend $1
monitor_changes $1
}

main $1 $2
17 changes: 17 additions & 0 deletions include/controllers/auth_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <crow.h>
#include <models/community_model.h>
#include <models/user_model.h>
#include <utils/common.h>
#include <utils/utils.h>
#include <format>
#include <memory>
#include <pqxx/pqxx>
#include <string>

class AuthController {
public:
static void register_user(pqxx::connection& db, const crow::request& req, crow::response& res);
static void login_user(pqxx::connection& db, const crow::request& req, crow::response& res);
};
26 changes: 26 additions & 0 deletions include/controllers/service_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <crow.h>
#include <format>
#include <memory>
#include <pqxx/pqxx>
#include <string>
#include <vector>

// ** custom includes
#include <models/service_model.h>
#include <models/user_model.h>
#include <utils/errors.h>
#include <utils/utils.h>
#include <utils/validations.h>
// ** ---------------------------------------------

class ServiceController {
public:
/* static void get_users(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_user_by_id(pqxx::connection& db, const crow::request& req, const std::string& user_id, crow::response& res);
static void delete_user_by_id(pqxx::connection& db, const std::string& user_id, crow::response& res); */

static void create_service(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_services(pqxx::connection& db, const crow::request& req, crow::response& res);
};
19 changes: 19 additions & 0 deletions include/controllers/user_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <crow.h>
#include <models/user_model.h>
#include <utils/errors.h>
#include <utils/user_validations.h>
#include <utils/utils.h>
#include <format>
#include <pqxx/pqxx>
#include <string>
#include <vector>

class UserController {
public:
static void get_users(pqxx::connection& db, const crow::request& req, crow::response& res);
static void get_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
static void delete_user_by_id(pqxx::connection& db, crow::response& res, const std::string& user_id);
static void update_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
};
51 changes: 51 additions & 0 deletions include/middlewares/verify_jwt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#pragma once

#include <crow.h>
#include <jwt-cpp/jwt.h>
#include <utils/auth.h>
#include <utils/utils.h>

struct VerifyJWT : crow::ILocalMiddleware {
struct context {};

void before_handle(crow::request& req, crow::response& res, context& ctx) {
std::string token = get_token_cookie(req);

if (!validate_token(token)) {
handle_error(res, "invalid token", 401);
return;
}

auto decoded = jwt::decode(token);

std::string id;
std::string type;

// Acceder al payload del token decodificado
for (auto& e : decoded.get_payload_json()) {
if (e.first == "id") {
id = e.second.get<std::string>();
} else if (e.first == "type") {
type = e.second.get<std::string>();
}
}

if (req.body == "") {
crow::json::wvalue body;

body["id"] = id;
body["isAdmin"] = (type == "admin");

req.body = body.dump();
} else {
crow::json::wvalue body = crow::json::load(req.body);

body["id"] = id;
body["isAdmin"] = (type == "admin");

req.body = body.dump();
}
}

void after_handle(crow::request& req, crow::response& res, context& ctx) {}
};
36 changes: 36 additions & 0 deletions include/models/community_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <utils/errors.h>
#include <ctime>
#include <format>
#include <memory>
#include <pqxx/pqxx>
#include <string>

class CommunityModel {
private:
std::string _id;
std::string _name;
std::string _code;
std::string _created_at;
std::string _updated_at;

public:
CommunityModel(std::string id, std::string name, std::string code, std::string created_at, std::string updated_at);

std::string get_id() const;
std::string get_name() const;
std::string get_code() const;
std::string get_created_at() const;
std::string get_updated_at() const;

static std::string generate_community_code();

static std::unique_ptr<CommunityModel> create_community(pqxx::connection& db, const std::string& name, bool throw_when_null = false);

static bool exists_name(pqxx::connection& db, const std::string& name);
static bool exists_code(pqxx::connection& db, const std::string& code);

static std::unique_ptr<CommunityModel> get_community_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false);
static std::unique_ptr<CommunityModel> get_community_by_code(pqxx::connection& db, const std::string& code, bool throw_when_null = false);
};
23 changes: 23 additions & 0 deletions include/models/notification_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <utils/common.h>

class NotificationModel {
private:
std::string _id;
std::string _sender_id;
std::string _receiver_id;
std::string _service_id;
std::string _status;
std::string _created_at;
std::string _updated_at;

public:
NotificationModel(std::string id, std::string sender_id, std::string receiver_id, std::string service_id, std::string status);

std::string get_id();
std::string get_sender_id();
std::string get_receiver_id();
std::string get_service_id();
std::string get_status();
};
44 changes: 44 additions & 0 deletions include/models/service_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <utils/errors.h>
#include <memory>
#include <optional>
#include <pqxx/pqxx>
#include <string>
#include <vector>

class ServiceModel {
private:
std::string _id;
std::string _community_id;
std::string _creator_id;
std::optional<std::string> _buyer_id;
std::string _title;
std::string _description;
int _price;
std::string _status;
std::string _type;
std::optional<std::string> _image_url;
std::string _created_at;
std::string _updated_at;

public:
ServiceModel(std::string id, std::string community_id, std::string creator_id, std::optional<std::string> buyer_id, std::string title, std::string description, int price, std::string status, std::string type, std::optional<std::string> image_url, std::string created_at, std::string updated_at);

std::string get_id() const;
std::string get_community_id() const;
std::string get_creator_id() const;
std::optional<std::string> get_buyer_id() const;
std::string get_title() const;
std::string get_description() const;
int get_price() const;
std::string get_status() const;
std::string get_type() const;
std::optional<std::string> get_image_url() const;
std::string get_created_at() const;
std::string get_updated_at() const;

static std::unique_ptr<ServiceModel> create_service(pqxx::connection& db, const std::string& community_id, const std::string& creator_id, const std::string& title, const std::string& description, const int price, const std::string& type, const std::optional<std::string>& image_url, bool isThrow = false);

static std::vector<std::unique_ptr<ServiceModel>> get_open_services_by_community_id(pqxx::connection& db, const std::string& community_id);
};
Loading

0 comments on commit aaded12

Please sign in to comment.