-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from trydirect/issue-3
Issue 3
- Loading branch information
Showing
14 changed files
with
209 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
application_port: 8000 | ||
auth_url: https://65190108818c4e98ac6000e4.mockapi.io/user/1 | ||
database: | ||
host: localhost | ||
port: 5432 | ||
username: postgres | ||
password: "postgres" | ||
database_name: stacker | ||
database_name: stacker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod serialize_datetime; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use chrono::{DateTime, TimeZone, Utc}; | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
const FORMAT: &'static str = "%Y-%m-%d %H:%M:%S"; | ||
|
||
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let s = format!("{}", date.format(FORMAT)); | ||
serializer.serialize_str(&s) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
Utc.datetime_from_str(&s, FORMAT) | ||
.map_err(serde::de::Error::custom) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod configuration; | ||
pub mod forms; | ||
pub mod helpers; | ||
mod middleware; | ||
pub mod models; | ||
pub mod routes; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use actix_web::{HttpRequest, HttpResponse}; | ||
use actix_web::{get, HttpRequest, HttpResponse}; | ||
|
||
pub async fn health_check(req: HttpRequest) -> HttpResponse { | ||
#[get("")] | ||
pub async fn health_check(_req: HttpRequest) -> HttpResponse { | ||
HttpResponse::Ok().finish() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::models; | ||
use actix_web::get; | ||
use actix_web::{web, Responder, Result}; | ||
use serde_derive::Serialize; | ||
use sqlx::PgPool; | ||
use tracing::Instrument; | ||
|
||
// workflow | ||
// add, update, list, get(user_id), ACL, | ||
// ACL - access to func for a user | ||
// ACL - access to objects for a user | ||
|
||
#[derive(Serialize)] | ||
struct JsonResponse { | ||
status: String, | ||
message: String, | ||
code: u32, | ||
rating: Option<models::Rating>, | ||
} | ||
|
||
#[tracing::instrument(name = "Get rating.")] | ||
#[get("/{id}")] | ||
pub async fn get_handler( | ||
path: web::Path<(i32,)>, | ||
pool: web::Data<PgPool>, | ||
) -> Result<impl Responder> { | ||
let rate_id = path.0; | ||
let query_span = tracing::info_span!("Search for rate id={}.", rate_id); | ||
match sqlx::query_as!( | ||
models::Rating, | ||
r"SELECT * FROM rating WHERE id=$1 LIMIT 1", | ||
rate_id | ||
) | ||
.fetch_one(pool.get_ref()) | ||
.instrument(query_span) | ||
.await | ||
{ | ||
Ok(rating) => { | ||
tracing::info!("rating found: {:?}", rating.id,); | ||
return Ok(web::Json(JsonResponse { | ||
status: "Success".to_string(), | ||
code: 200, | ||
message: "".to_string(), | ||
rating: Some(rating), | ||
})); | ||
} | ||
Err(sqlx::Error::RowNotFound) => { | ||
return Ok(web::Json(JsonResponse { | ||
status: "Error".to_string(), | ||
code: 404, | ||
message: format!("Not Found"), | ||
rating: None, | ||
})); | ||
} | ||
Err(e) => { | ||
tracing::error!("Failed to fetch rating, error: {:?}", e); | ||
return Ok(web::Json(JsonResponse { | ||
status: "Error".to_string(), | ||
code: 500, | ||
message: format!("Internal Server Error"), | ||
rating: None, | ||
})); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod add; | ||
mod get; | ||
pub use add::*; | ||
pub use get::*; |
Oops, something went wrong.