1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use sqlx::{Postgres, QueryBuilder};

use crate::DbResult;

const TABLE_NAME: &str = "npc_corporations";

#[derive(Debug, serde::Serialize, serde::Deserialize, sqlx::FromRow)]
pub struct CorporationDb {
  #[serde(rename = "corporationId")]
  pub corporation_id: i32,
  #[serde(rename = "corporationName")]
  pub corporation_name: String,
  pub size: String,
  pub extent: String,
  #[serde(rename = "solarSystemId", skip_serializing_if = "Option::is_none")]
  pub system_id: Option<i32>,
  #[serde(rename = "friendId", skip_serializing_if = "Option::is_none")]
  pub friend_id: Option<i32>,
  #[serde(rename = "enemyId", skip_serializing_if = "Option::is_none")]
  pub enemy_id: Option<i32>,
  #[serde(rename = "publicShares")]
  pub public_shares: i32,
  #[serde(rename = "initialPrice")]
  pub initial_price: i32,
  #[serde(rename = "minSecurity")]
  pub min_security: f32,
  #[serde(rename = "factionId", skip_serializing_if = "Option::is_none")]
  pub faction_id: Option<i32>,
  #[serde(rename = "sizeFactor", skip_serializing_if = "Option::is_none")]
  pub size_factor: Option<f32>,
  #[serde(rename = "stationCount", skip_serializing_if = "Option::is_none")]
  pub station_count: Option<i32>,
  #[serde(rename = "stationSystemCount", skip_serializing_if = "Option::is_none")]
  pub station_system_count: Option<i32>,
  pub description: String,
  #[serde(rename = "iconId", skip_serializing_if = "Option::is_none")]
  pub icon_id: Option<i32>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct CorporationFilter {
  #[serde(rename = "corporationId")]
  pub corporation_id: Option<String>,
  pub page: Option<i64>,
  pub limit: Option<i64>,
}

impl CorporationDb {
  pub async fn get_by_id(corporation_id: i32) -> DbResult<Self> {
    let pool = crate::pool();
    let corporation = sqlx::query_as::<_, Self>(&format!(
      r#"
      SELECT * FROM {}
      WHERE corporation_id = $1
      "#,
      TABLE_NAME
    ))
    .bind(corporation_id)
    .fetch_one(pool)
    .await?;

    Ok(corporation)
  }

  pub async fn count(filter: &CorporationFilter) -> DbResult<i64> {
    let pool = crate::pool();
    let mut query = QueryBuilder::new(format!("SELECT COUNT(*) FROM {}", TABLE_NAME));
    query = Self::build_query(query, filter);
    let query = query.build_query_scalar();

    let count = query.fetch_one(pool).await?;
    Ok(count)
  }

  pub async fn get_multiple(filter: &CorporationFilter) -> DbResult<Vec<Self>> {
    let page = filter.page.unwrap_or(1);
    let limit = filter.limit.unwrap_or(100);

    let pool = crate::pool();
    let mut query = QueryBuilder::new(format!("SELECT * FROM {}", TABLE_NAME));
    query = Self::build_query(query, filter);
    query.push(" ORDER BY corporation_id LIMIT ");
    query.push_bind(limit);
    query.push(" OFFSET ");
    query.push_bind((page - 1) * limit);
    let query = query.build_query_as();

    let corporations = query.fetch_all(pool).await?;
    Ok(corporations)
  }

  fn build_query<'a>(
    mut query: QueryBuilder<'a, Postgres>,
    filter: &'a CorporationFilter,
  ) -> QueryBuilder<'a, Postgres> {
    let has_where = false;

    // Parse the corporation_id query parameter
    if let Some(corporation_ids) = &filter.corporation_id {
      let corporation_ids: Vec<&str> = corporation_ids.split(',').collect();
      if !has_where {
        query.push(" WHERE");
      } else {
        query.push(" AND");
      }
      query.push(" (");
      for (i, corporation_id) in corporation_ids.iter().enumerate() {
        if i > 0 {
          query.push(" OR");
        }
        query.push(" corporation_id ILIKE ");
        query.push_bind(format!("%{}%", corporation_id));
      }
      query.push(" )");
    }

    query
  }

  pub async fn insert(&self) -> DbResult<()> {
    let pool = crate::pool();
    sqlx::query(&format!(
      r#"
        INSERT INTO {} (
          corporation_id,
          corporation_name,
          size,
          extent,
          system_id,
          friend_id,
          enemy_id,
          public_shares,
          initial_price,
          min_security,
          faction_id,
          size_factor,
          station_count,
          station_system_count,
          description,
          icon_id
        ) VALUES (
          $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, 
          $13, $14, $15, $16
        )
        "#,
      TABLE_NAME
    ))
    .bind(&self.corporation_id)
    .bind(&self.corporation_name)
    .bind(&self.size)
    .bind(&self.extent)
    .bind(&self.system_id)
    .bind(&self.friend_id)
    .bind(&self.enemy_id)
    .bind(&self.public_shares)
    .bind(&self.initial_price)
    .bind(&self.min_security)
    .bind(&self.faction_id)
    .bind(&self.size_factor)
    .bind(&self.station_count)
    .bind(&self.station_system_count)
    .bind(&self.description)
    .bind(&self.icon_id)
    .execute(pool)
    .await?;
    Ok(())
  }

  pub async fn insert_multiple(corporations: &Vec<Self>) -> DbResult<()> {
    let pool = crate::pool();
    let step = 1000;
    for i in (0..corporations.len()).step_by(step) {
      let mut query = sqlx::QueryBuilder::new(format!(
        r#"
        INSERT INTO {} (corporation_id, corporation_name, size, extent, system_id, friend_id, 
          enemy_id, public_shares, initial_price, min_security, faction_id, size_factor, 
          station_count, station_system_count, description, icon_id)
        VALUES
        "#,
        TABLE_NAME
      ));

      for j in 0..step {
        if i + j >= corporations.len() {
          break;
        }
        if j > 0 {
          query.push(", ");
        }
        let corporation = &corporations[i + j];
        query
          .push(" (")
          .push_bind(corporation.corporation_id)
          .push(", ")
          .push_bind(&corporation.corporation_name)
          .push(", ")
          .push_bind(&corporation.size)
          .push(", ")
          .push_bind(&corporation.extent)
          .push(", ")
          .push_bind(corporation.system_id)
          .push(", ")
          .push_bind(corporation.friend_id)
          .push(", ")
          .push_bind(corporation.enemy_id)
          .push(", ")
          .push_bind(corporation.public_shares)
          .push(", ")
          .push_bind(corporation.initial_price)
          .push(", ")
          .push_bind(corporation.min_security)
          .push(", ")
          .push_bind(corporation.faction_id)
          .push(", ")
          .push_bind(corporation.size_factor)
          .push(", ")
          .push_bind(corporation.station_count)
          .push(", ")
          .push_bind(corporation.station_system_count)
          .push(", ")
          .push_bind(&corporation.description)
          .push(", ")
          .push_bind(corporation.icon_id)
          .push(") ");
      }
      query
        .push("ON CONFLICT (corporation_id) DO UPDATE SET ")
        .push("corporation_name = EXCLUDED.corporation_name, ")
        .push("size = EXCLUDED.size, ")
        .push("extent = EXCLUDED.extent, ")
        .push("system_id = EXCLUDED.system_id, ")
        .push("friend_id = EXCLUDED.friend_id, ")
        .push("enemy_id = EXCLUDED.enemy_id, ")
        .push("public_shares = EXCLUDED.public_shares, ")
        .push("initial_price = EXCLUDED.initial_price, ")
        .push("min_security = EXCLUDED.min_security, ")
        .push("faction_id = EXCLUDED.faction_id, ")
        .push("size_factor = EXCLUDED.size_factor, ")
        .push("station_count = EXCLUDED.station_count, ")
        .push("station_system_count = EXCLUDED.station_system_count, ")
        .push("description = EXCLUDED.description, ")
        .push("icon_id = EXCLUDED.icon_id");
      query.build().execute(pool).await?;
    }
    Ok(())
  }

  pub async fn update(&self) -> DbResult<()> {
    let pool = crate::pool();
    sqlx::query(&format!(
      r#"
        UPDATE {} SET
          corporation_name = $2,
          size = $3,
          extent = $4,
          system_id = $5,
          friend_id = $6,
          enemy_id = $7,
          public_shares = $8,
          initial_price = $9,
          min_security = $10,
          faction_id = $11,
          size_factor = $12,
          station_count = $13,
          station_system_count = $14,
          description = $15,
          icon_id = $16
        WHERE corporation_id = $1
        "#,
      TABLE_NAME
    ))
    .bind(&self.corporation_id)
    .bind(&self.corporation_name)
    .bind(&self.size)
    .bind(&self.extent)
    .bind(&self.system_id)
    .bind(&self.friend_id)
    .bind(&self.enemy_id)
    .bind(&self.public_shares)
    .bind(&self.initial_price)
    .bind(&self.min_security)
    .bind(&self.faction_id)
    .bind(&self.size_factor)
    .bind(&self.station_count)
    .bind(&self.station_system_count)
    .bind(&self.description)
    .bind(&self.icon_id)
    .execute(pool)
    .await?;
    Ok(())
  }

  pub async fn delete(corporation_id: i32) -> DbResult<()> {
    let pool = crate::pool();
    sqlx::query(&format!(
      r#"
        DELETE FROM {}
        WHERE corporation_id = $1
        "#,
      TABLE_NAME
    ))
    .bind(corporation_id)
    .execute(pool)
    .await?;
    Ok(())
  }
}