api domain repo

This commit is contained in:
Jeremy Yin 2023-07-01 18:36:37 +08:00
parent 78ec96f9ae
commit 52b8d0a235
4 changed files with 77 additions and 29 deletions

33
src/api/create_pokemon.rs Normal file
View File

@ -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),
}
}

View File

@ -1,27 +1,13 @@
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use crate::repo::pokemon::Repository;
use rouille;
use serde::Serialize;
#[derive(Serialize)]
struct Response{
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 {
// rouille::Response::text("Got you!")
// rouille::Response::json(&Response{
// 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())
}
pub fn serve() -> rouille::Response {
rouille::Response::json(&Response {
message: String::from("Gotta catch them all!"),
})
}

View File

@ -3,17 +3,45 @@ use rouille::router;
use crate::repo::pokemon::Repository;
mod health;
mod create_pokemon;
pub fn serve(url: &str, repo: Arc<dyn Repository>) {
rouille::start_server(url, move |req| {
router!(req,
(POST)(/health) => {
health::serve(repo.clone(), req)
health::serve()
},
(POST)(/) => {
create_pokemon::serve(repo.clone(), req)
},
_ => {
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,
}
}
}

View File

@ -2,19 +2,20 @@ use std::sync::Arc;
use crate::domain::entity::{PokemonName, PokemonNumber, PokemonTypes};
use crate::repo::pokemon::{Insert, Repository};
struct Request {
number: u16,
name: String,
types: Vec<String>,
pub struct Request {
pub(crate) number: u16,
pub(crate) name: String,
pub(crate) types: Vec<String>,
}
enum Response {
pub enum Response {
Ok(u16),
BadRequest,
Conflict,
Error,
}
fn execute(repo: Arc<dyn Repository>, req: Request) -> Response {
pub fn execute(repo: Arc<dyn Repository>, req: Request) -> Response {
println!("execute");
match (
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){
Insert::Ok(num) => Response::Ok(u16::from(num)),
Insert::Conflict => Response::Conflict,
Insert::Error => Response::BadRequest,
Insert::Error => Response::Error,
},
_ => Response::BadRequest,
}