Rework some code issues
This commit is contained in:
56
.sqlx/query-2cdb2f4bbef873c327c73df8b01cb339fb38ba3e6457edb2a1d81b5defd99087.json
generated
Normal file
56
.sqlx/query-2cdb2f4bbef873c327c73df8b01cb339fb38ba3e6457edb2a1d81b5defd99087.json
generated
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT\n id,\n name,\n publishing_house,\n developer,\n description,\n multiplayer,\n is_free_to_play\nFROM\n games\n",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "id",
|
||||||
|
"type_info": "Int4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "name",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "publishing_house",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "developer",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 4,
|
||||||
|
"name": "description",
|
||||||
|
"type_info": "Text"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 5,
|
||||||
|
"name": "multiplayer",
|
||||||
|
"type_info": "Bool"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 6,
|
||||||
|
"name": "is_free_to_play",
|
||||||
|
"type_info": "Bool"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": []
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "2cdb2f4bbef873c327c73df8b01cb339fb38ba3e6457edb2a1d81b5defd99087"
|
||||||
|
}
|
||||||
14
.sqlx/query-598954fb5ae205a2b8b06c30c5545e709059cf3ba0aaa2a0e598900c49244100.json
generated
Normal file
14
.sqlx/query-598954fb5ae205a2b8b06c30c5545e709059cf3ba0aaa2a0e598900c49244100.json
generated
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "DELETE FROM\n games\nWHERE\n id = $1\n",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Int4"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "598954fb5ae205a2b8b06c30c5545e709059cf3ba0aaa2a0e598900c49244100"
|
||||||
|
}
|
||||||
19
.sqlx/query-656f1dd85ab29925cc54579ee643110e1ef36b707d99e600fae87379a9076437.json
generated
Normal file
19
.sqlx/query-656f1dd85ab29925cc54579ee643110e1ef36b707d99e600fae87379a9076437.json
generated
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO games(\n name,\n publishing_house,\n developer,\n description,\n multiplayer,\n is_free_to_play\n)\nVALUES ($1, $2, $3, $4, $5, $6)\n",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Bool",
|
||||||
|
"Bool"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "656f1dd85ab29925cc54579ee643110e1ef36b707d99e600fae87379a9076437"
|
||||||
|
}
|
||||||
20
.sqlx/query-830a0cee1b7bdca78a623d93450fe3310ce2af8d7dc314750df297c2e4baa560.json
generated
Normal file
20
.sqlx/query-830a0cee1b7bdca78a623d93450fe3310ce2af8d7dc314750df297c2e4baa560.json
generated
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "UPDATE\n games\nSET\n name = $1,\n publishing_house = $2,\n developer = $3,\n description = $4,\n multiplayer = $5,\n is_free_to_play = $6\nWHERE\n id = $7\n",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Text",
|
||||||
|
"Bool",
|
||||||
|
"Bool",
|
||||||
|
"Int4"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "830a0cee1b7bdca78a623d93450fe3310ce2af8d7dc314750df297c2e4baa560"
|
||||||
|
}
|
||||||
@ -6,4 +6,4 @@ services:
|
|||||||
POSTGRES_USER: ${POSTGRES_USER}
|
POSTGRES_USER: ${POSTGRES_USER}
|
||||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||||
ports:
|
ports:
|
||||||
- ${POSTGRES_PORT}:${POSTGRES_PORT}
|
- ${POSTGRES_PORT}:5432
|
||||||
|
|||||||
2
migrations/20251113124629_initial.down.sql
Normal file
2
migrations/20251113124629_initial.down.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
-- Add down migration script here
|
||||||
|
DROP TABLE games;
|
||||||
@ -1,4 +1,6 @@
|
|||||||
CREATE TABLE IF NOT EXISTS games(
|
-- Add up migration script here
|
||||||
|
|
||||||
|
CREATE TABLE games(
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
publishing_house TEXT NOT NULL,
|
publishing_house TEXT NOT NULL,
|
||||||
4
queries/delete.sql
Normal file
4
queries/delete.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
DELETE FROM
|
||||||
|
games
|
||||||
|
WHERE
|
||||||
|
id = $1
|
||||||
10
queries/get_all.sql
Normal file
10
queries/get_all.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
SELECT
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
publishing_house,
|
||||||
|
developer,
|
||||||
|
description,
|
||||||
|
multiplayer,
|
||||||
|
is_free_to_play
|
||||||
|
FROM
|
||||||
|
games
|
||||||
9
queries/insert.sql
Normal file
9
queries/insert.sql
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
INSERT INTO games(
|
||||||
|
name,
|
||||||
|
publishing_house,
|
||||||
|
developer,
|
||||||
|
description,
|
||||||
|
multiplayer,
|
||||||
|
is_free_to_play
|
||||||
|
)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
11
queries/update.sql
Normal file
11
queries/update.sql
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
UPDATE
|
||||||
|
games
|
||||||
|
SET
|
||||||
|
name = $1,
|
||||||
|
publishing_house = $2,
|
||||||
|
developer = $3,
|
||||||
|
description = $4,
|
||||||
|
multiplayer = $5,
|
||||||
|
is_free_to_play = $6
|
||||||
|
WHERE
|
||||||
|
id = $7
|
||||||
@ -1,7 +1,14 @@
|
|||||||
use sqlx::PgPool;
|
use std::env;
|
||||||
use std::{env, fs};
|
|
||||||
|
|
||||||
pub fn database_url() -> String {
|
use sqlx::PgPool;
|
||||||
|
|
||||||
|
pub async fn create_pool() -> PgPool {
|
||||||
|
PgPool::connect(&database_url())
|
||||||
|
.await
|
||||||
|
.expect("Could not connect to database")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn database_url() -> String {
|
||||||
let _ = dotenvy::dotenv();
|
let _ = dotenvy::dotenv();
|
||||||
let database_url = format!(
|
let database_url = format!(
|
||||||
"postgres://{}:{}@{}:{}/{}",
|
"postgres://{}:{}@{}:{}/{}",
|
||||||
@ -11,19 +18,11 @@ pub fn database_url() -> String {
|
|||||||
env::var("POSTGRES_PORT").unwrap(),
|
env::var("POSTGRES_PORT").unwrap(),
|
||||||
env::var("POSTGRES_DB").unwrap(),
|
env::var("POSTGRES_DB").unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if database_url != env::var("DATABASE_URL").unwrap() {
|
if database_url != env::var("DATABASE_URL").unwrap() {
|
||||||
panic!("DATABASE_URL is not set correctly");
|
panic!("DATABASE_URL is not set correctly");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("DATABASE_URL={database_url}");
|
println!("DATABASE_URL={database_url}");
|
||||||
database_url
|
database_url
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_table(pool: &PgPool) {
|
|
||||||
let sql_up =
|
|
||||||
fs::read_to_string("migrations/create_tables.sql").expect("Failed to read SQL file");
|
|
||||||
|
|
||||||
sqlx::query(&sql_up)
|
|
||||||
.execute(pool)
|
|
||||||
.await
|
|
||||||
.expect("Could not create database tables");
|
|
||||||
}
|
|
||||||
|
|||||||
10
src/main.rs
10
src/main.rs
@ -1,8 +1,8 @@
|
|||||||
use sqlx::PgPool;
|
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
mod app_state;
|
mod app_state;
|
||||||
mod database;
|
mod database;
|
||||||
|
mod models;
|
||||||
mod router;
|
mod router;
|
||||||
|
|
||||||
use app_state::AppState;
|
use app_state::AppState;
|
||||||
@ -10,15 +10,15 @@ use app_state::AppState;
|
|||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let state = AppState {
|
let state = AppState {
|
||||||
pool: PgPool::connect(&database::database_url())
|
pool: database::create_pool().await,
|
||||||
.await
|
|
||||||
.expect("Could not connect to database"),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
database::create_table(&state.pool);
|
sqlx::migrate!().run(&state.pool).await.unwrap();
|
||||||
|
|
||||||
let addr = "127.0.0.1:8000";
|
let addr = "127.0.0.1:8000";
|
||||||
let listener = TcpListener::bind(addr).await.unwrap();
|
let listener = TcpListener::bind(addr).await.unwrap();
|
||||||
|
|
||||||
println!("Listening at {addr}");
|
println!("Listening at {addr}");
|
||||||
|
|
||||||
axum::serve(listener, router::router(state)).await.unwrap();
|
axum::serve(listener, router::router(state)).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct Game {
|
pub struct Game {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -19,7 +21,7 @@ pub struct Game {
|
|||||||
// tags: Option<Vec<String>>,
|
// tags: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, serde::Serialize, serde::Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct CreateGame {
|
pub struct CreateGame {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub publishing_house: String,
|
pub publishing_house: String,
|
||||||
@ -29,7 +31,7 @@ pub struct CreateGame {
|
|||||||
pub is_free_to_play: bool,
|
pub is_free_to_play: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct GetGameQuery {
|
pub struct GamePathParams {
|
||||||
pub id: Option<i32>,
|
pub id: i32,
|
||||||
}
|
}
|
||||||
@ -1,19 +1,75 @@
|
|||||||
use axum::routing::{get, post};
|
use axum::routing::{get, post};
|
||||||
|
|
||||||
pub mod funcs;
|
use axum::{
|
||||||
pub mod game;
|
Json,
|
||||||
|
extract::{Path, State},
|
||||||
|
response::IntoResponse,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::app_state::AppState;
|
use crate::app_state::AppState;
|
||||||
|
use crate::models::{CreateGame, Game, GamePathParams};
|
||||||
|
|
||||||
pub fn router(state: AppState) -> axum::Router {
|
pub fn router(state: AppState) -> axum::Router {
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
.route(
|
.route("/api/games", get(games_list).post(insert_game))
|
||||||
"/api/games",
|
.route("/api/games/{id}", post(update_game).delete(delete_game))
|
||||||
get(funcs::games_list).post(funcs::insert_game),
|
|
||||||
)
|
|
||||||
.route(
|
|
||||||
"/api/games/{id}",
|
|
||||||
post(funcs::insert_game).delete(funcs::delete_game),
|
|
||||||
)
|
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn games_list(State(state): State<AppState>) -> impl IntoResponse {
|
||||||
|
let games: Vec<Game> = sqlx::query_file_as!(Game, "queries/get_all.sql")
|
||||||
|
.fetch_all(&state.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Json(games)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn insert_game(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Json(game): Json<CreateGame>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
sqlx::query_file!(
|
||||||
|
"queries/insert.sql",
|
||||||
|
game.name,
|
||||||
|
game.publishing_house,
|
||||||
|
game.developer,
|
||||||
|
game.description,
|
||||||
|
game.multiplayer,
|
||||||
|
game.is_free_to_play,
|
||||||
|
)
|
||||||
|
.execute(&state.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
axum::http::StatusCode::CREATED
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_game(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
Path(params): Path<GamePathParams>,
|
||||||
|
Json(game): Json<CreateGame>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
sqlx::query_file!(
|
||||||
|
"queries/update.sql",
|
||||||
|
game.name,
|
||||||
|
game.publishing_house,
|
||||||
|
game.developer,
|
||||||
|
game.description,
|
||||||
|
game.multiplayer,
|
||||||
|
game.is_free_to_play,
|
||||||
|
params.id,
|
||||||
|
)
|
||||||
|
.execute(&state.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_game(State(state): State<AppState>, Path(id): Path<i32>) -> impl IntoResponse {
|
||||||
|
sqlx::query_file!("queries/delete.sql", id)
|
||||||
|
.execute(&state.pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
axum::http::StatusCode::OK
|
||||||
|
}
|
||||||
|
|||||||
@ -1,53 +0,0 @@
|
|||||||
use axum::{
|
|
||||||
Json,
|
|
||||||
extract::{Path, Query, State},
|
|
||||||
response::IntoResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::game::{CreateGame, Game, GetGameQuery};
|
|
||||||
use crate::app_state::AppState;
|
|
||||||
|
|
||||||
pub async fn games_list(State(state): State<AppState>) -> impl IntoResponse {
|
|
||||||
let games: Vec<Game> = sqlx::query_as!(Game, "SELECT * FROM games")
|
|
||||||
.fetch_all(&state.pool)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Json(games)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn insert_game(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
Query(query): Query<GetGameQuery>,
|
|
||||||
Json(game): Json<CreateGame>,
|
|
||||||
) -> impl IntoResponse {
|
|
||||||
let sql_up = if query.id.is_some() {
|
|
||||||
"UPDATE games SET name=$1, publishing_house=$2, developer=$3, description=$4, multiplayer=$5, is_free_to_play=$6 WHERE id=$7"
|
|
||||||
} else {
|
|
||||||
"INSERT INTO games(name, publishing_house, developer, description, multiplayer, is_free_to_play) VALUES ($1, $2, $3, $4, $5, $6)"
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut sql_query = sqlx::query(sql_up)
|
|
||||||
.bind(game.name)
|
|
||||||
.bind(game.publishing_house)
|
|
||||||
.bind(game.developer)
|
|
||||||
.bind(game.description)
|
|
||||||
.bind(game.multiplayer)
|
|
||||||
.bind(game.is_free_to_play);
|
|
||||||
|
|
||||||
if let Some(id) = query.id {
|
|
||||||
sql_query = sql_query.bind(id);
|
|
||||||
}
|
|
||||||
sql_query.execute(&state.pool).await.unwrap();
|
|
||||||
|
|
||||||
axum::http::StatusCode::CREATED
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_game(State(state): State<AppState>, Path(id): Path<i32>) -> impl IntoResponse {
|
|
||||||
sqlx::query!("DELETE FROM games WHERE id = $1", id)
|
|
||||||
.execute(&state.pool)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
axum::http::StatusCode::OK
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user