r2_data_persistence/operations/
map.rs1use crate::entities::{Map, Point};
2use crate::models::map::{CreateMapModel, GetAllMapsModel, GetMapModel, MapDto, UpdateMapModel};
3use crate::operations;
4use crate::schema::maps::dsl::*;
5use crate::schema::points::dsl::points;
6use crate::schema::points::map_id;
7use diesel::associations::HasTable;
8use diesel::ExpressionMethods;
9use diesel::{PgConnection, QueryDsl, QueryResult, RunQueryDsl, SelectableHelper};
10use uuid::Uuid;
11
12pub fn create_map(
13 connection: &mut PgConnection,
14 model: CreateMapModel,
15) -> Result<GetMapModel, String> {
16 let result = diesel::insert_into(maps::table())
17 .values(to_entity(model))
18 .returning(Map::as_returning())
19 .get_result(connection);
20
21 match result {
22 Ok(map) => {
23 match points
24 .filter(map_id.eq(&map.id))
25 .select(Point::as_select())
26 .load(connection)
27 {
28 Ok(map_points) => Ok(to_model(map, map_points)),
29 Err(error) => Err(format!("Error saving map: {}", error)),
30 }
31 }
32 Err(error) => Err(format!("Error saving map: {}", error)),
33 }
34}
35
36pub fn get_map(connection: &mut PgConnection, key: &str) -> Option<GetMapModel> {
37 let result: QueryResult<Map> = maps.find(key).select(Map::as_select()).first(connection);
38
39 if let Ok(map) = result {
40 if let Ok(map_points) = points
41 .filter(map_id.eq(&map.id))
42 .select(Point::as_select())
43 .load(connection)
44 {
45 return Some(to_model(map, map_points));
46 }
47
48 return None;
49 }
50
51 None
52}
53
54pub fn get_all_maps(connection: &mut PgConnection) -> Result<Vec<GetAllMapsModel>, String> {
55 let result = maps.select(Map::as_select()).load(connection);
56
57 match result {
58 Ok(entities) => Ok(entities.into_iter().map(to_get_all_maps_model).collect()),
59 Err(error) => Err(format!("Error loading maps: {}", error)),
60 }
61}
62
63pub fn update_map(
64 connection: &mut PgConnection,
65 key: &str,
66 model: UpdateMapModel,
67) -> Result<GetMapModel, String> {
68 let result = diesel::update(maps.find(key))
69 .set::<UpdateMapModel>(model)
70 .returning(Map::as_returning())
71 .get_result(connection);
72
73 match result {
74 Ok(map) => {
75 match points
76 .filter(map_id.eq(&map.id))
77 .select(Point::as_select())
78 .load(connection)
79 {
80 Ok(map_points) => Ok(to_model(map, map_points)),
81 Err(error) => Err(format!("Error updating map: {}", error)),
82 }
83 }
84 Err(error) => Err(format!("Error updating map: {}", error)),
85 }
86}
87
88pub fn delete_map(connection: &mut PgConnection, key: &str) -> Result<(), String> {
89 let result = diesel::delete(maps.find(key)).execute(connection);
90
91 match result {
92 Ok(_) => Ok(()),
93 Err(error) => Err(format!("Error deleting map: {}", error)),
94 }
95}
96
97fn to_model(map: Map, map_points: Vec<Point>) -> GetMapModel {
98 GetMapModel {
99 id: map.id,
100 song: map.song,
101 items: map_points
102 .into_iter()
103 .map(|point| MapDto {
104 time: point.spawn_time,
105 point: operations::point::to_model(point),
106 })
107 .collect(),
108 }
109}
110
111fn to_get_all_maps_model(map: Map) -> GetAllMapsModel {
112 GetAllMapsModel {
113 id: map.id,
114 song: map.song,
115 }
116}
117
118fn to_entity(model: CreateMapModel) -> Map {
119 Map {
120 id: Uuid::new_v4().to_string(),
121 song: model.song,
122 }
123}