Skip to content

Commit aaded12

Browse files
authored
Merge pull request #41 from Serveis-Neby/develop
Develop
2 parents 6e3bb61 + 21c6580 commit aaded12

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2206
-65
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

CMakeLists.txt

100644100755
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
1-
cmake_minimum_required(VERSION 3.10)
1+
cmake_minimum_required(VERSION 3.18)
2+
23
project(backend)
34

45
set(CMAKE_CXX_STANDARD 20)
56

6-
# Add executable
7-
add_executable(backend main.cpp
8-
routes/test_routes.cpp
9-
controllers/test_controller.cpp
7+
# Agrega las opciones de compilación
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
9+
10+
# Encuentra las bibliotecas necesarias
11+
find_library(PQXX_LIB pqxx)
12+
find_library(PQ_LIB pq)
13+
find_library(BCRYPT_LIB bcrypt)
14+
15+
include(FetchContent)
16+
fetchcontent_declare(jwt-cpp
17+
GIT_REPOSITORY https://github.com/Thalhammer/jwt-cpp.git
18+
GIT_TAG 08bcf77a687fb06e34138e9e9fa12a4ecbe12332 # v0.7.0 release
1019
)
20+
set(JWT_BUILD_EXAMPLES OFF CACHE BOOL "disable building examples" FORCE)
21+
fetchcontent_makeavailable(jwt-cpp)
22+
# Si bcrypt está instalado en un lugar no estándar, puedes usar find_path para encontrar la carpeta de inclusión
23+
# find_path(BCRYPT_INCLUDE_DIR bcrypt/bcrypt.h)
24+
25+
# Establece las fuentes de tu proyecto
26+
file(GLOB_RECURSE SOURCES "src/*.cpp")
27+
28+
# Crea el ejecutable
29+
add_executable(backend ${SOURCES})
30+
31+
# Agrega las carpetas de inclusión
32+
target_include_directories(backend PUBLIC include)
33+
34+
# Enlaza las bibliotecas necesarias
35+
target_link_libraries(backend ${PQXX_LIB} ${PQ_LIB} ${BCRYPT_LIB} jwt-cpp::jwt-cpp)
1136

12-
# Include Crow header files
13-
target_include_directories(backend PUBLIC ${CROW_INCLUDE_DIRS})
37+
# Si bcrypt está en un lugar no estándar, también agrega el directorio de inclusión
38+
# target_include_directories(backend PUBLIC ${BCRYPT_INCLUDE_DIR})

Dockerfile.dev

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM gcc:latest
2+
3+
RUN apt-get update && apt-get install -y libpqxx-dev libboost-dev cmake inotify-tools git
4+
5+
WORKDIR /app
6+
7+
COPY crow-v1.0+5.deb .
8+
9+
RUN dpkg -i crow-v1.0+5.deb
10+
11+
COPY . .
12+
13+
RUN apt install -y nlohmann-json3-dev libgtest-dev libssl-dev
14+
15+
RUN git clone https://github.com/arun11299/cpp-jwt.git &&\
16+
git clone https://github.com/trusch/libbcrypt.git && \
17+
cd libbcrypt && \
18+
mkdir build && \
19+
cd build && \
20+
cmake .. && \
21+
make && \
22+
make install && \
23+
ldconfig
24+
25+
RUN chmod 777 hot-reload.sh
26+
27+
CMD ["./hot-reload.sh", "./backend"]

Dockerfile renamed to Dockerfile.prod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ RUN dpkg -i crow-v1.0+5.deb
1010

1111
COPY . .
1212

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

1615
RUN make

Dockerfile.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM gcc:latest
2+
3+
RUN apt-get update && apt-get install -y libpqxx-dev libboost-dev cmake inotify-tools git
4+
5+
WORKDIR /app
6+
7+
COPY . .
8+
9+
RUN cd test && \
10+
git submodule add https://github.com/nlohmann/json.git extern/json && \
11+
git submodule add https://github.com/google/googletest.git extern/googletest &&\
12+
git submodule add https://github.com/whoshuu/cpr.git extern/cpr && \
13+
ldconfig
14+
15+
RUN chmod 777 hot-reload.sh
16+
17+
CMD ["./hot-reload.sh", "./test", "./test/"]

LICENSE

100644100755
File mode changed.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Backend

controllers/test_controller.cpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

controllers/test_controller.h

Lines changed: 0 additions & 6 deletions
This file was deleted.

crow-v1.0+5.deb

100644100755
File mode changed.

hot-reload.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
if [ $# -eq 0 ]; then
4+
echo "Usage: $0 <file_path> [<directory_path>]"
5+
exit 1
6+
fi
7+
8+
compile_backend() {
9+
echo "Compiling..."
10+
cmake .
11+
make
12+
}
13+
14+
run_backend() {
15+
"$1" &
16+
BACKEND_PID=$!
17+
echo "Running process with PID: $BACKEND_PID"
18+
}
19+
20+
monitor_changes() {
21+
echo "Monitoring for changes..."
22+
while true; do
23+
inotifywait -r -e modify,move,create,delete ./
24+
kill_backend
25+
compile_backend
26+
run_backend $1
27+
done
28+
}
29+
30+
kill_backend() {
31+
if [[ -n $BACKEND_PID ]]; then
32+
echo "Killing process with PID: $BACKEND_PID"
33+
kill $BACKEND_PID
34+
wait $BACKEND_PID 2>/dev/null
35+
fi
36+
}
37+
38+
main() {
39+
if [ -d "$2" ]; then
40+
cd "$2" || exit 1
41+
fi
42+
43+
compile_backend
44+
run_backend $1
45+
monitor_changes $1
46+
}
47+
48+
main $1 $2

include/controllers/auth_controller.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <crow.h>
4+
#include <models/community_model.h>
5+
#include <models/user_model.h>
6+
#include <utils/common.h>
7+
#include <utils/utils.h>
8+
#include <format>
9+
#include <memory>
10+
#include <pqxx/pqxx>
11+
#include <string>
12+
13+
class AuthController {
14+
public:
15+
static void register_user(pqxx::connection& db, const crow::request& req, crow::response& res);
16+
static void login_user(pqxx::connection& db, const crow::request& req, crow::response& res);
17+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <crow.h>
4+
#include <format>
5+
#include <memory>
6+
#include <pqxx/pqxx>
7+
#include <string>
8+
#include <vector>
9+
10+
// ** custom includes
11+
#include <models/service_model.h>
12+
#include <models/user_model.h>
13+
#include <utils/errors.h>
14+
#include <utils/utils.h>
15+
#include <utils/validations.h>
16+
// ** ---------------------------------------------
17+
18+
class ServiceController {
19+
public:
20+
/* static void get_users(pqxx::connection& db, const crow::request& req, crow::response& res);
21+
static void get_user_by_id(pqxx::connection& db, const crow::request& req, const std::string& user_id, crow::response& res);
22+
static void delete_user_by_id(pqxx::connection& db, const std::string& user_id, crow::response& res); */
23+
24+
static void create_service(pqxx::connection& db, const crow::request& req, crow::response& res);
25+
static void get_services(pqxx::connection& db, const crow::request& req, crow::response& res);
26+
};

include/controllers/user_controller.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <crow.h>
4+
#include <models/user_model.h>
5+
#include <utils/errors.h>
6+
#include <utils/user_validations.h>
7+
#include <utils/utils.h>
8+
#include <format>
9+
#include <pqxx/pqxx>
10+
#include <string>
11+
#include <vector>
12+
13+
class UserController {
14+
public:
15+
static void get_users(pqxx::connection& db, const crow::request& req, crow::response& res);
16+
static void get_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
17+
static void delete_user_by_id(pqxx::connection& db, crow::response& res, const std::string& user_id);
18+
static void update_user_by_id(pqxx::connection& db, const crow::request& req, crow::response& res, const std::string& user_id);
19+
};

include/middlewares/verify_jwt.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <crow.h>
4+
#include <jwt-cpp/jwt.h>
5+
#include <utils/auth.h>
6+
#include <utils/utils.h>
7+
8+
struct VerifyJWT : crow::ILocalMiddleware {
9+
struct context {};
10+
11+
void before_handle(crow::request& req, crow::response& res, context& ctx) {
12+
std::string token = get_token_cookie(req);
13+
14+
if (!validate_token(token)) {
15+
handle_error(res, "invalid token", 401);
16+
return;
17+
}
18+
19+
auto decoded = jwt::decode(token);
20+
21+
std::string id;
22+
std::string type;
23+
24+
// Acceder al payload del token decodificado
25+
for (auto& e : decoded.get_payload_json()) {
26+
if (e.first == "id") {
27+
id = e.second.get<std::string>();
28+
} else if (e.first == "type") {
29+
type = e.second.get<std::string>();
30+
}
31+
}
32+
33+
if (req.body == "") {
34+
crow::json::wvalue body;
35+
36+
body["id"] = id;
37+
body["isAdmin"] = (type == "admin");
38+
39+
req.body = body.dump();
40+
} else {
41+
crow::json::wvalue body = crow::json::load(req.body);
42+
43+
body["id"] = id;
44+
body["isAdmin"] = (type == "admin");
45+
46+
req.body = body.dump();
47+
}
48+
}
49+
50+
void after_handle(crow::request& req, crow::response& res, context& ctx) {}
51+
};

include/models/community_model.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <utils/errors.h>
4+
#include <ctime>
5+
#include <format>
6+
#include <memory>
7+
#include <pqxx/pqxx>
8+
#include <string>
9+
10+
class CommunityModel {
11+
private:
12+
std::string _id;
13+
std::string _name;
14+
std::string _code;
15+
std::string _created_at;
16+
std::string _updated_at;
17+
18+
public:
19+
CommunityModel(std::string id, std::string name, std::string code, std::string created_at, std::string updated_at);
20+
21+
std::string get_id() const;
22+
std::string get_name() const;
23+
std::string get_code() const;
24+
std::string get_created_at() const;
25+
std::string get_updated_at() const;
26+
27+
static std::string generate_community_code();
28+
29+
static std::unique_ptr<CommunityModel> create_community(pqxx::connection& db, const std::string& name, bool throw_when_null = false);
30+
31+
static bool exists_name(pqxx::connection& db, const std::string& name);
32+
static bool exists_code(pqxx::connection& db, const std::string& code);
33+
34+
static std::unique_ptr<CommunityModel> get_community_by_id(pqxx::connection& db, const std::string& id, bool throw_when_null = false);
35+
static std::unique_ptr<CommunityModel> get_community_by_code(pqxx::connection& db, const std::string& code, bool throw_when_null = false);
36+
};

include/models/notification_model.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <utils/common.h>
4+
5+
class NotificationModel {
6+
private:
7+
std::string _id;
8+
std::string _sender_id;
9+
std::string _receiver_id;
10+
std::string _service_id;
11+
std::string _status;
12+
std::string _created_at;
13+
std::string _updated_at;
14+
15+
public:
16+
NotificationModel(std::string id, std::string sender_id, std::string receiver_id, std::string service_id, std::string status);
17+
18+
std::string get_id();
19+
std::string get_sender_id();
20+
std::string get_receiver_id();
21+
std::string get_service_id();
22+
std::string get_status();
23+
};

include/models/service_model.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <utils/errors.h>
4+
#include <memory>
5+
#include <optional>
6+
#include <pqxx/pqxx>
7+
#include <string>
8+
#include <vector>
9+
10+
class ServiceModel {
11+
private:
12+
std::string _id;
13+
std::string _community_id;
14+
std::string _creator_id;
15+
std::optional<std::string> _buyer_id;
16+
std::string _title;
17+
std::string _description;
18+
int _price;
19+
std::string _status;
20+
std::string _type;
21+
std::optional<std::string> _image_url;
22+
std::string _created_at;
23+
std::string _updated_at;
24+
25+
public:
26+
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);
27+
28+
std::string get_id() const;
29+
std::string get_community_id() const;
30+
std::string get_creator_id() const;
31+
std::optional<std::string> get_buyer_id() const;
32+
std::string get_title() const;
33+
std::string get_description() const;
34+
int get_price() const;
35+
std::string get_status() const;
36+
std::string get_type() const;
37+
std::optional<std::string> get_image_url() const;
38+
std::string get_created_at() const;
39+
std::string get_updated_at() const;
40+
41+
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);
42+
43+
static std::vector<std::unique_ptr<ServiceModel>> get_open_services_by_community_id(pqxx::connection& db, const std::string& community_id);
44+
};

0 commit comments

Comments
 (0)