Skip to content

Instantly share code, notes, and snippets.

@felipebastosweb
Created February 20, 2024 23:46
Show Gist options
  • Save felipebastosweb/764650d1c8a74ac84ead74963b056fdb to your computer and use it in GitHub Desktop.
Save felipebastosweb/764650d1c8a74ac84ead74963b056fdb to your computer and use it in GitHub Desktop.
Template para geração de classes Controllers para aplicativos em linguagem D que utilizam o framework vibe.d (W.I.P)
module controllers.#{model_name.asLowerCase()};
import std.stdio;
// Vibed
import vibe.d;
import vibe.db.mongo.mongo;
import std.algorithm: map;
import database;
import models.#{model_name.asLowerCase()};
import translation;
class #{model_name}Controller {
private MongoClient client;
MongoCollection coll;
this() {
client = connectMongoDB("127.0.0.1");
coll = client.getCollection("storeapp.#{model_name.asLowerCase()}s");
}
// GET /#{model_name.asLowerCase()}s
@method(HTTPMethod.GET)
@path("/#{model_name.asLowerCase()}s")
void index()
{
/*
bool authenticated = ms_authenticated;
render!("#{model_name.asLowerCase()}/index.dt", authenticated);
*/
auto #{model_name.asLowerCase()}s = coll.find().map!(bson => deserializeBson!#{model_name}(bson));
//TODO: o nome do arquivo precisa ser convertido de CamelCase para nome1_nome2
render!("#{model_name.asLowerCase()}s_index.dt", #{model_name.asLowerCase()}s);
}
// GET /#{model_name.asLowerCase()}s/:_id
@method(HTTPMethod.GET)
@path("/#{model_name.asLowerCase()}s/:_id")
void show(HTTPServerRequest req, HTTPServerResponse res)
{
struct Q { BsonObjectID _id = BsonObjectID.fromString(req.params["_id"]); }
auto docNullable = coll.findOne!#{model_name}(Q());
if (! docNullable.isNull) {
// Acessar os campos da estrutura #{model_name}
auto #{model_name.asLowerCase()} = #{model_name.asLowerCase()}Nullable.get;
//TODO: o nome do arquivo precisa ser convertido de CamelCase para nome1_nome2
render!("#{model_name.asLowerCase()}s_show.dt", #{model_name.asLowerCase()});
} else {
}
}
// GET /#{model_name.asLowerCase()}s/new
@method(HTTPMethod.GET)
@path("/#{model_name.asLowerCase()}s/new")
void new_form()
{
/*
bool authenticated = ms_authenticated;
render!("#{model_name.asLowerCase()}/index.dt", authenticated);
*/
auto #{model_name.asLowerCase()} = Brand();
//TODO: o nome do arquivo precisa ser convertido de CamelCase para nome1_nome2
render!("#{model_name.asLowerCase()}s_new.dt", brand);
}
// POST /#{model_name.asLowerCase()}s
@method(HTTPMethod.POST)
@path("/#{model_name.asLowerCase()}s")
void create(HTTPServerRequest req, HTTPServerResponse res)
{
#{model_name} #{model_name.asLowerCase()};
#{model_name.asLowerCase()}._id = BsonObjectID.generate; // Gera um ID aleatório para o usuário
- foreach(attribute; attributes)
#{model_name.asLowerCase()}.#{attribute} = req.form["#{attribute}"];
coll.insertOne(#{model_name.asLowerCase()});
res.redirect("/#{model_name.asLowerCase()}s");
}
// GET /#{model_name.asLowerCase()}s/:_id/edit
@method(HTTPMethod.GET)
@path("/#{model_name.asLowerCase()}s/:_id/edit")
void edit_form(HTTPServerRequest req, HTTPServerResponse res)
{
/*
bool authenticated = ms_authenticated;
render!("#{model_name.asLowerCase()}/index.dt", authenticated);
*/
struct Q { BsonObjectID _id = BsonObjectID.fromString(req.params["_id"]); }
auto docNullable = coll.findOne!#{model_name}(Q());
if (! docNullable.isNull) {
// Acessar os campos da estrutura Brand
#{model_name} #{model_name.asLowerCase()} = docNullable.get;
//TODO: o nome do arquivo precisa ser convertido de CamelCase para nome1_nome2
render!("#{model_name.asLowerCase()}s_edit.dt", brand);
}
}
// POST /brands/:_id
@method(HTTPMethod.POST)
@path("/brands/:_id")
void change(HTTPServerRequest req, HTTPServerResponse res)
{
auto _id = BsonObjectID.fromString(req.params["_id"]);
// filter
BsonObjectID[string] filter;
filter["_id"] = _id;
// update
Bson[string][string] update;
- foreach(attribute; attributes)
update["$set"]["#{attribute}"] = req.form["#{attribute}"];
coll.updateOne(filter, update);
res.redirect("/#{model_name.asLowerCase()}s");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment