1
1
use std:: collections:: HashMap ;
2
2
3
- use sqlx:: { PgConnection , Postgres , QueryBuilder } ;
3
+ use sqlx:: PgConnection ;
4
4
5
5
use crate :: types:: api:: ApiError ;
6
6
@@ -19,134 +19,6 @@ pub struct Tag {
19
19
}
20
20
21
21
impl Tag {
22
- pub async fn get_tag_ids (
23
- tags : Vec < String > ,
24
- pool : & mut PgConnection ,
25
- ) -> Result < Vec < FetchedTag > , ApiError > {
26
- let db_tags = match sqlx:: query_as!(
27
- FetchedTag ,
28
- "SELECT id, name FROM mod_tags WHERE is_readonly = false"
29
- )
30
- . fetch_all ( & mut * pool)
31
- . await
32
- {
33
- Ok ( tags) => tags,
34
- Err ( e) => {
35
- log:: error!( "{}" , e) ;
36
- return Err ( ApiError :: DbError ) ;
37
- }
38
- } ;
39
-
40
- let mut ret = Vec :: new ( ) ;
41
- for tag in tags {
42
- if let Some ( t) = db_tags. iter ( ) . find ( |t| t. name == tag. to_lowercase ( ) ) {
43
- ret. push ( t. clone ( ) )
44
- } else {
45
- return Err ( ApiError :: BadRequest ( format ! (
46
- "Tag '{}' isn't allowed. Only the following are allowed: '{}'" ,
47
- tag,
48
- db_tags
49
- . iter( )
50
- . map( |t| t. name. clone( ) )
51
- . collect:: <Vec <String >>( )
52
- . join( ", " )
53
- ) ) ) ;
54
- }
55
- }
56
-
57
- Ok ( ret)
58
- }
59
-
60
- pub async fn update_mod_tags (
61
- mod_id : & str ,
62
- tags : Vec < i32 > ,
63
- pool : & mut PgConnection ,
64
- ) -> Result < ( ) , ApiError > {
65
- let existing = match sqlx:: query!(
66
- "SELECT mod_id, tag_id FROM mods_mod_tags WHERE mod_id = $1" ,
67
- mod_id,
68
- )
69
- . fetch_all ( & mut * pool)
70
- . await
71
- {
72
- Ok ( existing) => existing,
73
- Err ( e) => {
74
- log:: error!( "{}" , e) ;
75
- return Err ( ApiError :: DbError ) ;
76
- }
77
- } ;
78
-
79
- let insertable = tags
80
- . iter ( )
81
- . filter ( |t| !existing. iter ( ) . any ( |e| e. tag_id == * * t) )
82
- . collect :: < Vec < _ > > ( ) ;
83
-
84
- let deletable = existing
85
- . iter ( )
86
- . filter ( |e| !tags. iter ( ) . any ( |t| e. tag_id == * t) )
87
- . map ( |x| x. tag_id )
88
- . collect :: < Vec < _ > > ( ) ;
89
-
90
- Tag :: delete_tags_for_mod ( mod_id, deletable, pool) . await ?;
91
-
92
- if insertable. is_empty ( ) {
93
- return Ok ( ( ) ) ;
94
- }
95
-
96
- let mut query_builder: QueryBuilder < Postgres > =
97
- QueryBuilder :: new ( "INSERT INTO mods_mod_tags (mod_id, tag_id) VALUES (" ) ;
98
-
99
- for ( index, tag) in insertable. iter ( ) . enumerate ( ) {
100
- if existing. iter ( ) . any ( |e| e. tag_id == * * tag) {
101
- continue ;
102
- }
103
- let mut separated = query_builder. separated ( ", " ) ;
104
- separated. push_bind ( mod_id) ;
105
- separated. push_bind ( tag) ;
106
- query_builder. push ( ")" ) ;
107
-
108
- if index != insertable. len ( ) - 1 {
109
- query_builder. push ( ", (" ) ;
110
- }
111
- }
112
-
113
- if let Err ( e) = query_builder. build ( ) . execute ( & mut * pool) . await {
114
- log:: error!( "{}" , e) ;
115
- return Err ( ApiError :: DbError ) ;
116
- }
117
- Ok ( ( ) )
118
- }
119
-
120
- pub async fn delete_tags_for_mod (
121
- mod_id : & str ,
122
- tags : Vec < i32 > ,
123
- pool : & mut PgConnection ,
124
- ) -> Result < ( ) , ApiError > {
125
- if tags. is_empty ( ) {
126
- return Ok ( ( ) ) ;
127
- }
128
- let mut query_builder: QueryBuilder < Postgres > =
129
- QueryBuilder :: new ( "DELETE FROM mods_mod_tags WHERE (mod_id, tag_id) IN ((" ) ;
130
-
131
- for ( index, tag) in tags. iter ( ) . enumerate ( ) {
132
- let mut separated = query_builder. separated ( ", " ) ;
133
- separated. push_bind ( mod_id) ;
134
- separated. push_bind ( tag) ;
135
- query_builder. push ( ")" ) ;
136
-
137
- if index != tags. len ( ) - 1 {
138
- query_builder. push ( ", (" ) ;
139
- }
140
- }
141
- query_builder. push ( ")" ) ;
142
-
143
- if let Err ( e) = query_builder. build ( ) . execute ( & mut * pool) . await {
144
- log:: error!( "{}" , e) ;
145
- return Err ( ApiError :: DbError ) ;
146
- }
147
- Ok ( ( ) )
148
- }
149
-
150
22
pub async fn get_tags_for_mod (
151
23
mod_id : & str ,
152
24
pool : & mut PgConnection ,
0 commit comments