~misterio/BSI-SCC0540-projeto

a81ea18167d1fea7a162eb54c005186438401eb7 — Gabriel Fontes 5 months ago b074a4f 1.4.0
permite retornar json nas rotas também
M Cargo.lock => Cargo.lock +1 -1
@@ 1338,7 1338,7 @@ dependencies = [

[[package]]
name = "projeto-bd"
version = "1.3.7"
version = "1.4.0"
dependencies = [
 "normpath",
 "postgres-types",

M Cargo.toml => Cargo.toml +1 -1
@@ 1,7 1,7 @@
[package]
name = "projeto-bd"
description = "Projeto da disciplina de Banco de Dados"
version = "1.3.7"
version = "1.4.0"
authors = [ "Gabriel Fontes <g.fontes@usp.br>", "Lucas Carneiro <lcarneiro5992@usp.br>", "Victor Kenji <victorkenji12@usp.br>" ]
readme = "README.md"
edition = "2018"

M default.nix => default.nix +1 -1
@@ 2,7 2,7 @@

rustPlatform.buildRustPackage rec {
  pname = "projeto-bd";
  version = "1.3.7";
  version = "1.4.0";

  src = lib.cleanSource ./.;


M src/common/mod.rs => src/common/mod.rs +1 -0
@@ 3,4 3,5 @@ pub mod database;
pub mod error;
pub mod pagination;
pub mod post_as_delete;
pub mod template_or_json;
pub mod tera;

A src/common/template_or_json.rs => src/common/template_or_json.rs +19 -0
@@ 0,0 1,19 @@
use rocket::serde::json::Json;
use rocket::{http, request, response};
use rocket_dyn_templates::Template;
use serde::Serialize;

#[derive(Debug)]
pub struct TemplateOrJson<D>(pub Template, pub Json<D>);

impl<'r, T: Serialize> response::Responder<'r, 'static> for TemplateOrJson<T> {
    fn respond_to(self, req: &'r request::Request<'_>) -> response::Result<'static> {
        let media_type = req.accept().map(|a| a.preferred().media_type());

        if media_type == Some(&http::MediaType::JSON) {
            self.1.respond_to(req)
        } else {
            self.0.respond_to(req)
        }
    }
}

M src/lib.rs => src/lib.rs +1 -1
@@ 1,5 1,5 @@
pub mod common;
pub use common::{assets, database, error, pagination, post_as_delete, tera};
pub use common::{assets, database, error, pagination, post_as_delete, template_or_json, tera};

pub mod routes;
pub mod schema;

M src/routes/apoios.rs => src/routes/apoios.rs +12 -8
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,6 14,7 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Apoio, ApoioFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<apoiador>/<ano>")]


@@ 19,10 22,12 @@ async fn get(
    db: Connection<Database>,
    apoiador: String,
    ano: i16,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Apoio>, ServerError> {
    let apoio = Apoio::obter(&db, &apoiador, ano).await?;

    Ok(Template::render("routes/apoio", context! {apoio}))
    let json = Json(apoio.clone());
    let template = Template::render("routes/apoio", context! {apoio});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<apoiador>/<ano>")]


@@ 46,13 51,12 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: ApoioFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Apoio>>, ServerError> {
    let apoios = Apoio::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
        "routes/apoios",
        context! {apoios, filtro, paginas, flash},
    ))
    let json = Json(apoios.clone());
    let template = Template::render("routes/apoios", context! {apoios,filtro,paginas,flash});
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/candidaturas.rs => src/routes/candidaturas.rs +13 -9
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,6 14,7 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Candidatura, CandidaturaFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<candidato>/<ano>")]


@@ 19,13 22,12 @@ async fn get(
    db: Connection<Database>,
    candidato: String,
    ano: i16,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Candidatura>, ServerError> {
    let candidatura = Candidatura::obter(&db, &candidato, ano).await?;

    Ok(Template::render(
        "routes/candidatura",
        context! {candidatura},
    ))
    let json = Json(candidatura.clone());
    let template = Template::render("routes/candidatura", context! {candidatura});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<candidato>/<ano>")]


@@ 49,13 51,15 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: CandidaturaFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Candidatura>>, ServerError> {
    let candidaturas = Candidatura::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
    let json = Json(candidaturas.clone());
    let template = Template::render(
        "routes/candidaturas",
        context! {candidaturas, filtro, paginas, flash},
    ))
    );
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/cargos.rs => src/routes/cargos.rs +12 -8
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,6 14,7 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Cargo, CargoFiltro, TipoCargo},
    template_or_json::TemplateOrJson,
};

#[get("/<tipo>/<local>")]


@@ 19,10 22,12 @@ async fn get(
    db: Connection<Database>,
    tipo: TipoCargo,
    local: String,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Cargo>, ServerError> {
    let cargo = Cargo::obter(&db, tipo, &local).await?;

    Ok(Template::render("routes/cargo", context! {cargo}))
    let json = Json(cargo.clone());
    let template = Template::render("routes/cargo", context! {cargo});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<tipo>/<local>")]


@@ 46,13 51,12 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: CargoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Cargo>>, ServerError> {
    let cargos = Cargo::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
        "routes/cargos",
        context! {cargos, filtro, paginas, flash},
    ))
    let json = Json(cargos.clone());
    let template = Template::render("routes/cargos", context! {cargos, filtro, paginas, flash});
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/doacoes.rs => src/routes/doacoes.rs +12 -8
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,13 14,16 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Doacao, DoacaoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<id>")]
async fn get(db: Connection<Database>, id: i32) -> Result<Template, ServerError> {
async fn get(db: Connection<Database>, id: i32) -> Result<TemplateOrJson<Doacao>, ServerError> {
    let doacao = Doacao::obter(&db, id).await?;

    Ok(Template::render("routes/doacao", context! {doacao}))
    let json = Json(doacao.clone());
    let template = Template::render("routes/doacao", context! {doacao});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<id>")]


@@ 38,13 43,12 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: DoacaoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Doacao>>, ServerError> {
    let doacoes = Doacao::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
        "routes/doacoes",
        context! {doacoes, filtro, paginas, flash},
    ))
    let json = Json(doacoes.clone());
    let template = Template::render("routes/doacoes", context! {doacoes, filtro, paginas, flash});
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/individuos.rs => src/routes/individuos.rs +16 -6
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,13 14,19 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Individuo, IndividuoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<cpfcnpj>")]
async fn get(db: Connection<Database>, cpfcnpj: String) -> Result<Template, ServerError> {
async fn get(
    db: Connection<Database>,
    cpfcnpj: String,
) -> Result<TemplateOrJson<Individuo>, ServerError> {
    let individuo = Individuo::obter(&db, &cpfcnpj).await?;

    Ok(Template::render("routes/individuo", context! {individuo}))
    let json = Json(individuo.clone());
    let template = Template::render("routes/individuo", context! {individuo});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<cpfcnpj>")]


@@ 38,13 46,15 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: IndividuoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Individuo>>, ServerError> {
    let individuos = Individuo::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
    let json = Json(individuos.clone());
    let template = Template::render(
        "routes/individuos",
        context! {individuos, filtro, paginas, flash},
    ))
    );
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/julgamentos.rs => src/routes/julgamentos.rs +13 -6
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,6 14,7 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Julgamento, JulgamentoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<processo>/<instancia>")]


@@ 19,10 22,12 @@ async fn get(
    db: Connection<Database>,
    processo: i32,
    instancia: String,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Julgamento>, ServerError> {
    let julgamento = Julgamento::obter(&db, processo, &instancia).await?;

    Ok(Template::render("routes/julgamento", context! {julgamento}))
    let json = Json(julgamento.clone());
    let template = Template::render("routes/julgamento", context! {julgamento});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<processo>/<instancia>")]


@@ 46,13 51,15 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: JulgamentoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Julgamento>>, ServerError> {
    let julgamentos = Julgamento::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
    let json = Json(julgamentos.clone());
    let template = Template::render(
        "routes/julgamentos",
        context! {julgamentos, filtro, paginas, flash},
    ))
    );
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/partidos.rs => src/routes/partidos.rs +16 -6
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,13 14,19 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Partido, PartidoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<numero>")]
async fn get(db: Connection<Database>, numero: i16) -> Result<Template, ServerError> {
async fn get(
    db: Connection<Database>,
    numero: i16,
) -> Result<TemplateOrJson<Partido>, ServerError> {
    let partido = Partido::obter(&db, numero).await?;

    Ok(Template::render("routes/partido", context! {partido}))
    let json = Json(partido.clone());
    let template = Template::render("routes/partido", context! {partido});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<numero>")]


@@ 38,13 46,15 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: PartidoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Partido>>, ServerError> {
    let partidos = Partido::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
    let json = Json(partidos.clone());
    let template = Template::render(
        "routes/partidos",
        context! {partidos, filtro, paginas, flash},
    ))
    );
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/pleitos.rs => src/routes/pleitos.rs +12 -8
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,6 14,7 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Pleito, PleitoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<candidato>/<ano>/<turno>")]


@@ 20,10 23,12 @@ async fn get(
    candidato: String,
    ano: i16,
    turno: i16,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Pleito>, ServerError> {
    let pleito = Pleito::obter(&db, &candidato, ano, turno).await?;

    Ok(Template::render("routes/pleito", context! {pleito}))
    let json = Json(pleito.clone());
    let template = Template::render("routes/pleito", context! {pleito});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<candidato>/<ano>/<turno>")]


@@ 48,13 53,12 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: PleitoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Pleito>>, ServerError> {
    let pleitos = Pleito::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
        "routes/pleitos",
        context! {pleitos, filtro, paginas, flash},
    ))
    let json = Json(pleitos.clone());
    let template = Template::render("routes/pleitos", context! {pleitos, filtro, paginas, flash});
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {

M src/routes/processos.rs => src/routes/processos.rs +13 -6
@@ 2,7 2,9 @@ use rocket::{
    delete, get,
    request::FlashMessage,
    response::{Flash, Redirect},
    routes, Route,
    routes,
    serde::json::Json,
    Route,
};
use rocket_db_pools::Connection;
use rocket_dyn_templates::{context, Template};


@@ 12,13 14,16 @@ use crate::{
    error::ServerError,
    pagination::Pages,
    schema::{Processo, ProcessoFiltro},
    template_or_json::TemplateOrJson,
};

#[get("/<id>")]
async fn get(db: Connection<Database>, id: i32) -> Result<Template, ServerError> {
async fn get(db: Connection<Database>, id: i32) -> Result<TemplateOrJson<Processo>, ServerError> {
    let processo = Processo::obter(&db, id).await?;

    Ok(Template::render("routes/processo", context! {processo}))
    let json = Json(processo.clone());
    let template = Template::render("routes/processo", context! {processo});
    Ok(TemplateOrJson(template, json))
}

#[delete("/<id>")]


@@ 38,13 43,15 @@ async fn list(
    flash: Option<FlashMessage<'_>>,
    filtro: ProcessoFiltro,
    paginas: Pages,
) -> Result<Template, ServerError> {
) -> Result<TemplateOrJson<Vec<Processo>>, ServerError> {
    let processos = Processo::listar(&db, filtro.clone(), paginas.current, 50).await?;

    Ok(Template::render(
    let json = Json(processos.clone());
    let template = Template::render(
        "routes/processos",
        context! {processos, filtro, paginas, flash},
    ))
    );
    Ok(TemplateOrJson(template, json))
}

pub fn routes() -> Vec<Route> {