api domain repo
This commit is contained in:
parent
78ec96f9ae
commit
52b8d0a235
|
@ -0,0 +1,33 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use crate::api::Status;
|
||||||
|
use crate::domain::create_pokemon;
|
||||||
|
use crate::repo::pokemon::Repository;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Response{
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Request {
|
||||||
|
number: u16,
|
||||||
|
name: String,
|
||||||
|
types: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serve(repo: Arc<dyn Repository>, req: &rouille::Request) -> rouille::Response {
|
||||||
|
let req= match rouille::input::json_input::<Request>(req) {
|
||||||
|
Ok(req) => create_pokemon::Request{
|
||||||
|
number: req.number,
|
||||||
|
name: req.name,
|
||||||
|
types: req.types,
|
||||||
|
},
|
||||||
|
_ => return rouille::Response::from(Status::BadRequest),
|
||||||
|
};
|
||||||
|
match create_pokemon::execute(repo, req) {
|
||||||
|
create_pokemon::Response::Ok(number) => rouille::Response::json(&Response{message: number.to_string()}),
|
||||||
|
create_pokemon::Response::BadRequest => rouille::Response::from(Status::BadRequest),
|
||||||
|
create_pokemon::Response::Conflict => rouille::Response::from(Status::Conflict),
|
||||||
|
create_pokemon::Response::Error => rouille::Response::from(Status::InternalServerError),
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,27 +1,13 @@
|
||||||
use std::sync::Arc;
|
use rouille;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::Serialize;
|
||||||
use crate::repo::pokemon::Repository;
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Response{
|
struct Response {
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct Request {
|
|
||||||
number: u16,
|
|
||||||
name: String,
|
|
||||||
types: Vec<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn serve(repo: Arc<dyn Repository>, req: &rouille::Request) -> rouille::Response {
|
pub fn serve() -> rouille::Response {
|
||||||
// rouille::Response::text("Got you!")
|
rouille::Response::json(&Response {
|
||||||
// rouille::Response::json(&Response{
|
message: String::from("Gotta catch them all!"),
|
||||||
// message: "Got me!".to_string(),
|
})
|
||||||
// })
|
|
||||||
match rouille::input::json_input::<Request>(req) {
|
|
||||||
Ok(_) => rouille::Response::json(&Response{
|
|
||||||
message: "Got json".to_string()
|
|
||||||
}),
|
|
||||||
_ => return rouille::Response::from(rouille::Response::empty_400())
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -3,17 +3,45 @@ use rouille::router;
|
||||||
use crate::repo::pokemon::Repository;
|
use crate::repo::pokemon::Repository;
|
||||||
|
|
||||||
mod health;
|
mod health;
|
||||||
|
mod create_pokemon;
|
||||||
|
|
||||||
|
|
||||||
pub fn serve(url: &str, repo: Arc<dyn Repository>) {
|
pub fn serve(url: &str, repo: Arc<dyn Repository>) {
|
||||||
rouille::start_server(url, move |req| {
|
rouille::start_server(url, move |req| {
|
||||||
router!(req,
|
router!(req,
|
||||||
(POST)(/health) => {
|
(POST)(/health) => {
|
||||||
health::serve(repo.clone(), req)
|
health::serve()
|
||||||
|
},
|
||||||
|
(POST)(/) => {
|
||||||
|
create_pokemon::serve(repo.clone(), req)
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
rouille::Response::from(rouille::Response::empty_404())
|
rouille::Response::from(rouille::Response::empty_404())
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Status {
|
||||||
|
BadRequest,
|
||||||
|
NotFound,
|
||||||
|
Conflict,
|
||||||
|
InternalServerError,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Status> for rouille::Response {
|
||||||
|
fn from(value: Status) -> Self {
|
||||||
|
let status_code = match value {
|
||||||
|
Status::BadRequest => 400,
|
||||||
|
Status::NotFound => 404,
|
||||||
|
Status::Conflict => 409,
|
||||||
|
Status::InternalServerError => 500,
|
||||||
|
};
|
||||||
|
Self {
|
||||||
|
status_code,
|
||||||
|
headers: vec![],
|
||||||
|
data: rouille::ResponseBody::empty(),
|
||||||
|
upgrade: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,19 +2,20 @@ use std::sync::Arc;
|
||||||
use crate::domain::entity::{PokemonName, PokemonNumber, PokemonTypes};
|
use crate::domain::entity::{PokemonName, PokemonNumber, PokemonTypes};
|
||||||
use crate::repo::pokemon::{Insert, Repository};
|
use crate::repo::pokemon::{Insert, Repository};
|
||||||
|
|
||||||
struct Request {
|
pub struct Request {
|
||||||
number: u16,
|
pub(crate) number: u16,
|
||||||
name: String,
|
pub(crate) name: String,
|
||||||
types: Vec<String>,
|
pub(crate) types: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Response {
|
pub enum Response {
|
||||||
Ok(u16),
|
Ok(u16),
|
||||||
BadRequest,
|
BadRequest,
|
||||||
Conflict,
|
Conflict,
|
||||||
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute(repo: Arc<dyn Repository>, req: Request) -> Response {
|
pub fn execute(repo: Arc<dyn Repository>, req: Request) -> Response {
|
||||||
println!("execute");
|
println!("execute");
|
||||||
match (
|
match (
|
||||||
PokemonNumber::try_from(req.number),
|
PokemonNumber::try_from(req.number),
|
||||||
|
@ -24,7 +25,7 @@ fn execute(repo: Arc<dyn Repository>, req: Request) -> Response {
|
||||||
(Ok(number), Ok(name), Ok(types)) => match repo.insert(number, name, types){
|
(Ok(number), Ok(name), Ok(types)) => match repo.insert(number, name, types){
|
||||||
Insert::Ok(num) => Response::Ok(u16::from(num)),
|
Insert::Ok(num) => Response::Ok(u16::from(num)),
|
||||||
Insert::Conflict => Response::Conflict,
|
Insert::Conflict => Response::Conflict,
|
||||||
Insert::Error => Response::BadRequest,
|
Insert::Error => Response::Error,
|
||||||
},
|
},
|
||||||
_ => Response::BadRequest,
|
_ => Response::BadRequest,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue