use super::*;
use schema::unit::dsl::*;

#[command]
#[aliases("fnd")]
#[description = "Find unit registered in bot"]
#[usage = "unit"]
#[example = "darth"]
fn find(ctx: &mut Context, msg: &Message, args: Args) -> CommandResult {
    let unit_name = args.rest();

    let conn = establish_connection();

    let units: Vec<String> = unit
        .select(name)
        .filter(name.like(format!("%{}%", unit_name.replace(" ", "%"))))
        .order(name.asc())
        .load(&conn)?;

    msg.channel_id.send_message(&ctx.http, |m| {
        m.embed(|e| e.field("Units", units.join("\n"), true))
    })?;

    Ok(())
}
