diff --git a/api/src/Server.hx b/api/src/Server.hx index cf021f5..eb782a4 100644 --- a/api/src/Server.hx +++ b/api/src/Server.hx @@ -4,27 +4,35 @@ import tink.http.Response; import tink.web.routing.*; import tink.sql.drivers.Sqlite; import db.Db; +import haxe.Json; import model.MUser; import controller.IDGen; import controller.User; -import db.Db.Database as Db; - - +typedef T_project = { + name : String, + description : String, + url : String +} +typedef T_projects = { + version : Int, + projects : Array +} class Server { + @await public static var db:Db; static function main() { var container = new NodeContainer(8080); //var container = PhpContainer.inst; //use PhpContainer instead of NodeContainer when targeting PHP var router = new Router(new Root()); + Db.createTables(); container.run(function(req) { return router.route(Context.ofRequest(req)) .recover(OutgoingResponse.reportError); }); } - public static var db: } class Root { @@ -65,10 +73,10 @@ class Root { public function user() return new User(); - // @:sub('/projects') - // @:produces('application/json') - // public function projects() - // return new Projects(); + @:sub('/projects') + @:produces('application/json') + public function projects() + return new Projects(); @:get('/hello') @:produces('application/json') @@ -84,3 +92,29 @@ class Root { return new IDGen(); } + +class Projects { + public function new() {} + + public var path:String = "./res/json/projects.json"; + + @:get('/') + @:produces('application/json') + public function projects(){ + var json:String = Utils.getJson(path); + return json; + } + @:get('/project/by_name/$name') + @:produces('application/json') + public function project(name:String){ + var json:String = Utils.getJson(path); + var _projects:T_projects = Json.parse(json); + json = "{}"; + for(_project in _projects.projects){ + if(_project.name == name){ + json = tink.Json.stringify(_project); + } + } + return json; + } +} diff --git a/api/src/controller/User.hx b/api/src/controller/User.hx index a09608f..10aab8d 100644 --- a/api/src/controller/User.hx +++ b/api/src/controller/User.hx @@ -12,11 +12,9 @@ using tink.CoreApi; class User{ - - @await private static var database:Db = new Db_DrivebyCool('db.sqlite', driver); + private static var driver = new tink.sql.drivers.Sqlite(); + @await private static var db:Db = new Db('db.sqlite', driver); public function new() {} - - @:get('/') @:produces('text/plain') @@ -63,7 +61,7 @@ class User{ @:get('/registerDummyUser') @:produces('application/json') public function registerdummyuser(){ - //@await var db = new Db('daba2', driver); + //@await var db = new Db('db.sqlite', driver); // var failed:tink.core.Next> = null return createUserTable().next(function(_){ return db.MUser.insertOne({ @@ -118,9 +116,7 @@ class User{ @:get('/fetchalice') @:produces('application/json') - public function fetchalice(){ - var driver = new tink.sql.drivers.Sqlite(); - + public function fetchalice(){ return db.MUser.select({name: MUser.name, email: MUser.email}).where(MUser.email == 'alice@example.com').first().next(function(row) { return row; }); diff --git a/api/src/db/Db.hx b/api/src/db/Db.hx index 0e082a4..470fcd9 100644 --- a/api/src/db/Db.hx +++ b/api/src/db/Db.hx @@ -1,29 +1,84 @@ package db; -// import tink.sql.drivers.Sqlite; -//import tink.sql.Database; +import tink.sql.drivers.Sqlite; +import tink.sql.Database; import tink.sql.Types; import model.MUser; import model.services.DrivebyCool; +import tink.CoreApi; - @:tables(MUser) - @:tables(MProject) + + + +@:tables(MUser) +@:tables(MProject) // Post related Tables - @:tables(MPost) - @:tables(MTag) - @:tables(MPostTag) +@:tables(MPost) +@:tables(MTag) +@:tables(MTaggedPost) interface DbDef_DrivebyCool extends tink.sql.DatabaseDefinition { //@:procedure var func:Int->{x:Int, point:tink.s2d.Point}; @:table('user') var UserTbl:MUser; // @:table('project') var ProjectTbl:DrivebyCool.MProject; } -class Database extends tink.sql.Database { + +class Db extends tink.sql.Database { public function new(__name:String,driver:tink.sql.Driver){ super(__name, driver); } + private static var driver = new tink.sql.drivers.Sqlite(); + @async private static var db:Db = new Db('db.sqlite', driver); + @async public static function getDb() + { + return @await db; + } public static function createTables():Void { - + var db = Db.getDb(); + @async var execute = function(){ + return db.MUser.create(true).next(function(_){ + trace("aids"); + return db.MProject.create(true); + }).next(function(_){ + trace("aids"); + return db.MPost.create(true); + }).next(function(_){ + trace("aids"); + return db.MTag.create(true); + }).next(function(_){ + trace("aids"); + return db.MTaggedPost.create(true); + }); + } + @await execute().next(function(_){ + trace(_); + return _; + }).handle(function(_){ + trace(_); + }); + // trace(two()); + // @async var yeet = db.next(function(_db){ + // return db.MUser.create(); + // }); + // yeet.next(function(_){ + // trace("kanker"); + // var test = @await db; + // return test.MProject.create(true); + // }); + // .next(function(_){ + // trace("kanker"); + // return db.MPost.create(true); + // }).next(function(_){ + // return db.MTag.create(true); + // }).next(function(_){ + // return db.MTaggedPost.create(true); + // }).next(function(_){ + // trace(_); + // }); + // @await db.MProject.create(true); + // @await db.MPost.create(true); + // @await db.MTag.create(true); + // @await db.MTaggedPost.create(true); } } \ No newline at end of file diff --git a/api/src/model/MUser.hx b/api/src/model/MUser.hx index 59d1297..81aff9a 100644 --- a/api/src/model/MUser.hx +++ b/api/src/model/MUser.hx @@ -6,6 +6,6 @@ import model.MService; typedef MUser = { @:autoIncrement @:primary public var id(default, null):Id; public var name:VarChar<50>; - public var email:VarChar<50>; - public var password:VarChar<50>; + public var email:VarChar<256>; + public var password:VarChar<256>; } \ No newline at end of file diff --git a/api/src/model/services/DrivebyCool.hx b/api/src/model/services/DrivebyCool.hx index efab80a..2c251b5 100644 --- a/api/src/model/services/DrivebyCool.hx +++ b/api/src/model/services/DrivebyCool.hx @@ -6,55 +6,50 @@ import model.MSubscription; /* - * Project related models + * Project related models */ -// Defines a Project to be listed on the projects page +//Defines a Project to be listed on the projects page typedef MProject = { - @:autoIncrement @:primary public var id(default, null):Id; - // Properties - public var name:VarChar<50>; - public var owner:Id; - public var description:VarChar<1024>; + @:autoIncrement @:primary public var id(default, null):Id; + // Properties + public var name:VarChar<50>; + public var owner:Id; + public var description:VarChar<1024>; } -// Defines a relationship between a Project and a User. The User will be added as a member of the project and can author blogposts +// Defines a relationship between a Project and a User. The User will be added as a member of the project and can author blogposts typedef MMembership = { - @:autoIncrement @:primary public var id(default, null):Id; - // relationships - public var project:Id; - public var user:Id; + @:autoIncrement @:primary public var id(default, null):Id; + // relationships + public var project:Id; + public var user:Id; } -/* - * Defines a Post - */ + +// Defines a Post typedef MPost = { - @:autoIncrement @:primary public var id(default, null):Id; - // Relationships - public var project:Id; - public var owner:Id; - // Properties - public var title:VarChar<1024>; - public var description:Text; - public var content:Text; + @:autoIncrement @:primary public var id(default, null):Id; + // Relationships + public var project:Id; + public var owner:Id; + // Properties + public var title:VarChar<1024>; + public var description:Text; + public var content:Text; } -// -// Defines a tag that can be added to any post. -// +// Defines a tag that can be added to any post. typedef MTag = { - @:autoIncrement @:primary public var id(default, null):Id; - public var title:VarChar<32>; + @:autoIncrement @:primary public var id(default, null):Id; + public var title:VarChar<32>; } -// -// Defines a relationship between a tag and a post. -// +// Defines a relationship between a tag and a post. typedef MTaggedPost = { - @:autoIncrement @:primary public var id(default, null):Id; - public var post:Id; - public var tag:Id; + @:autoIncrement @:primary public var id(default, null):Id; + public var post:Id; + public var tag:Id; } \ No newline at end of file