Added lib to service

This commit is contained in:
Benjamin Sherriff
2023-10-04 19:58:54 -04:00
parent cee9dbdc81
commit ecc65222b6
20 changed files with 169 additions and 166 deletions

View File

@@ -1,7 +1,8 @@
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use siren::ServiceError;
use crate::{db::schema::messages::{self}, error_handler::ServiceError};
use crate::db::schema::messages::{self};
#[derive(Queryable, Selectable, Serialize, Deserialize)]
#[diesel(table_name = messages)]

View File

@@ -1,8 +1,9 @@
use actix_web::{get, post, web, HttpResponse, HttpRequest, ResponseError};
use log::error;
use serde::{Serialize, Deserialize};
use siren::{GetResponse, Metadata, ServiceError};
use crate::{db::{messages::{QueryMessage, QueryFilters, InsertMessage}, GetResponse, Metadata}, error_handler::ServiceError};
use crate::db::messages::{QueryMessage, QueryFilters, InsertMessage};
#[derive(Serialize, Deserialize)]
struct GetAllParams {

View File

@@ -1,6 +1,5 @@
use crate::error_handler::ServiceError;
use diesel::{r2d2::ConnectionManager, PgConnection};
use serde::{Deserialize, Serialize};
use siren::ServiceError;
use crate::diesel_migrations::MigrationHarness;
use lazy_static::lazy_static;
use log::{error, info};
@@ -55,18 +54,3 @@ pub fn connection() -> Result<DbConnection, ServiceError> {
pub fn load_data() {
spells::load_data();
}
#[derive(Serialize, Deserialize)]
pub struct GetResponse<T> {
pub data: T,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>
}
#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub total: i32,
pub limit: i32,
pub page: i32,
pub pages: i32
}

View File

@@ -1,7 +1,8 @@
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use siren::ServiceError;
use crate::{db::{schema::spells::{self}, classes::AbilityType, conditions::ConditionType}, error_handler::ServiceError};
use crate::db::{schema::spells::{self}, classes::AbilityType, conditions::ConditionType};
use super::{SchoolType, CastingTime, CastingType, SpellAttackType, SpellDamageType, Range, Area, Components, Duration, Source, Description, DurationType};

View File

@@ -1,8 +1,9 @@
use actix_web::{get, post, put, delete, web, HttpResponse, HttpRequest, ResponseError};
use log::error;
use serde::{Serialize, Deserialize};
use siren::{GetResponse, Metadata, ServiceError};
use crate::{db::{spells::{QuerySpell, QueryFilters}, GetResponse, Metadata}, error_handler::ServiceError};
use crate::db::spells::{QuerySpell, QueryFilters};
use super::{Spell, InsertSpell};

View File

@@ -1,9 +1,38 @@
use actix_web::{ResponseError, HttpResponse};
use diesel::result::Error as DieselError;
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use serde::{Serialize, Deserialize};
use std::fmt;
#[derive(Serialize, Deserialize)]
pub struct Message {
pub id: String,
pub guild_id: i64,
pub channel_id: i64,
pub user_id: i64,
pub created: i64,
pub model: String,
pub request: String,
pub response: String,
pub request_tags: Vec<String>,
pub response_tags: Vec<String>,
}
#[derive(Serialize, Deserialize)]
pub struct GetResponse<T> {
pub data: T,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>
}
#[derive(Serialize, Deserialize)]
pub struct Metadata {
pub total: i32,
pub limit: i32,
pub page: i32,
pub pages: i32
}
#[derive(Debug, Deserialize, Serialize)]
pub struct ServiceError {
pub status: u16,
@@ -40,6 +69,18 @@ impl From<DieselError> for ServiceError {
}
}
impl From<reqwest::Error> for ServiceError {
fn from(error: reqwest::Error) -> ServiceError {
ServiceError::new(500, format!("Unknown reqwest error: {}", error))
}
}
impl From<serde_json::Error> for ServiceError {
fn from(error: serde_json::Error) -> ServiceError {
ServiceError::new(500, format!("Unknown serde_json error: {}", error))
}
}
impl ResponseError for ServiceError {
fn error_response(&self) -> HttpResponse {
let status_code = match StatusCode::from_u16(self.status) {
@@ -54,4 +95,4 @@ impl ResponseError for ServiceError {
HttpResponse::build(status_code).json(serde_json::json!({ "status": status_code.as_u16(), "message": error_message }))
}
}
}

View File

@@ -9,7 +9,6 @@ use actix_web::{HttpServer, App};
use dotenv::dotenv;
use log::{error, info};
mod error_handler;
mod db;
#[actix_web::main]