r2_data_persistence/operations/
point.rs

1use crate::entities::Point;
2use crate::models::point::{CreatePointModel, GetPointModel, UpdatePointModel};
3use crate::schema::points::dsl::*;
4use diesel::associations::HasTable;
5use diesel::{PgConnection, QueryDsl, QueryResult, RunQueryDsl, SelectableHelper};
6use uuid::Uuid;
7
8pub fn create_point(
9    connection: &mut PgConnection,
10    model: CreatePointModel,
11) -> Result<GetPointModel, String> {
12    let result = diesel::insert_into(points::table())
13        .values(to_entity(model))
14        .returning(Point::as_returning())
15        .get_result(connection);
16
17    match result {
18        Ok(entity) => Ok(to_model(entity)),
19        Err(error) => Err(format!("Error creating point: {}", error)),
20    }
21}
22
23pub fn get_point(connection: &mut PgConnection, key: &str) -> Option<GetPointModel> {
24    let result: QueryResult<Point> = points
25        .find(key)
26        .select(Point::as_select())
27        .first(connection);
28
29    if let Ok(entity) = result {
30        return Some(to_model(entity));
31    }
32
33    None
34}
35
36pub fn get_all_points(connection: &mut PgConnection) -> Result<Vec<GetPointModel>, String> {
37    let result = points.select(Point::as_select()).load(connection);
38
39    match result {
40        Ok(entities) => Ok(entities.into_iter().map(to_model).collect()),
41        Err(error) => Err(format!("Error getting points: {}", error)),
42    }
43}
44
45pub fn update_point(
46    connection: &mut PgConnection,
47    key: &str,
48    model: UpdatePointModel,
49) -> Result<GetPointModel, String> {
50    let result = diesel::update(points.find(key))
51        .set::<UpdatePointModel>(model)
52        .returning(Point::as_returning())
53        .get_result(connection);
54
55    match result {
56        Ok(entity) => Ok(to_model(entity)),
57        Err(error) => Err(format!("Error updating point: {}", error)),
58    }
59}
60
61pub fn delete_point(connection: &mut PgConnection, key: &str) -> Result<(), String> {
62    let result = diesel::delete(points.find(key)).execute(connection);
63
64    match result {
65        Ok(_) => Ok(()),
66        Err(error) => Err(format!("Error deleting point: {}", error)),
67    }
68}
69
70pub fn to_model(entity: Point) -> GetPointModel {
71    GetPointModel {
72        id: entity.id,
73        map_id: entity.map_id,
74        spawn_x: entity.spawn_x,
75        spawn_y: entity.spawn_y,
76        radius: entity.radius,
77        spawn_time: entity.spawn_time,
78        click_x: entity.click_x,
79        click_y: entity.click_y,
80        click_time: entity.click_time,
81        offset: entity.offset,
82    }
83}
84
85fn to_entity(model: CreatePointModel) -> Point {
86    Point {
87        id: Uuid::new_v4().to_string(),
88        map_id: model.map_id,
89        spawn_x: model.spawn_x,
90        spawn_y: model.spawn_y,
91        radius: model.radius,
92        spawn_time: model.spawn_time,
93        click_x: model.click_x,
94        click_y: model.click_y,
95        click_time: model.click_time,
96        offset: model.offset,
97    }
98}