Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luisvt committed Feb 6, 2015
0 parents commit 82fb68f
Show file tree
Hide file tree
Showing 41 changed files with 958 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.buildlog
.DS_Store
.idea
.pub/
build/
packages
pubspec.lock
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.0.1

- Initial version, created by Stagehand
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2015, <your name>.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: /app/dart-sdk/bin/dart server.dart

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# drails_sample_app

A minimal command-line application.
25 changes: 25 additions & 0 deletions bin/controllers/employee_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of drails_example;

@AuthorizeRoles(const ['ADMIN'])
class EmployeesController {

@AuthorizeRoles(const ['PUBLIC'])
Employee get(int id) => g_employees[id];

@AuthorizeRoles(const ['PUBLIC'])
List<Employee> getAll() => g_employees.values.toList();

Employee save(int id, @RequestBody Employee employee) => g_employees[id] = employee;

Iterable<Employee> saveAll(@RequestBody List<Employee> employees) =>
employees..forEach((employee) {
if(employee.id == null) {
employee.id = ++lastEmployeeId;
}
g_employees[employee.id] = employee;
});

void delete(int id) { g_employees.remove(id); }

void deleteAll(@RequestBody List<int> ids) { ids.forEach((id) => g_employees.remove(id)); }
}
21 changes: 21 additions & 0 deletions bin/controllers/persons_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
part of drails_example;

class PersonsController {
Person get(int id) => g_persons[id];

List<Person> getAll() => g_persons.values.toList();

Person save(int id, @RequestBody Person person) => g_persons[id] = person;

Iterable<Person> saveAll(@RequestBody List<Person> persons) =>
persons..forEach((person) {
if(person.id == null) {
person.id = ++lastId;
}
g_persons[person.id] = person;
});

void delete(int id) { g_persons.remove(id); }

void deleteAll(@RequestBody List<int> ids) { ids.forEach((id) => g_persons.remove(id)); }
}
57 changes: 57 additions & 0 deletions bin/data/persons_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
part of drails_example;

int lastId = 3;
Map<int, Person> g_persons = {
1: new Person()
..id = 1
..firstName = 'Jhon'
..lastName = 'Doe'
..dob = new DateTime.utc(1988, 4, 1),
2: new Person()
..id = 2
..firstName = 'Jimmy'
..lastName = 'Who'
..dob = new DateTime.utc(1988, 4, 1),
3: new Person()
..id = 3
..firstName = 'Jake'
..lastName = 'Britman'
..dob = new DateTime.utc(1988, 8, 28)
};


Map<int, User> g_users = {
1: new User()
..id = 1
..name = 'jhon'
..password = 'jhon'
..roles = ['ADMIN'],
2: new User()
..id = 2
..name = 'jake'
..password = 'jake'
..roles = ['PUBLIC']
};

int lastEmployeeId = 3;
Map<int, Employee> g_employees = {
1: new Employee()
..id = 1
..firstName = 'Jade'
..lastName = 'wan'
..dob = new DateTime(1988, 4, 1)
..salary = 1000,
2: new Employee()
..id = 2
..firstName = 'David'
..lastName = 'Dolittle'
..dob = new DateTime(1988, 4, 1)
..salary = 1000,
3: new Employee()
..id = 3
..firstName = 'Mark'
..lastName = 'Brito'
..dob = new DateTime(1989, 8, 28)
..salary = 3000
};

43 changes: 43 additions & 0 deletions bin/server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
library drails_example;

import 'package:drails/drails.dart';
import 'package:logging/logging.dart';
import 'dart:io';
import 'package:drails_sample_app/models.dart';

part 'controllers/persons_controller.dart';
part 'controllers/employee_controller.dart';
part 'data/persons_data.dart';


initLogger() {
Logger.root.level = Level.OFF;
hierarchicalLoggingEnabled = true;
new Logger('server_init').level = Level.INFO;

Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
}

void main() {
initLogger();

ENV = 'prod';
CLIENT_DIR['prod'] = '/../web/';

POST['/login'] = (HttpSession session, @RequestBody User user) {
var cu = g_users.values.singleWhere((u) => u.name == user.name && u.password == user.password);
if(cu == null) throw Exception;
session['user'] = cu;
return new User()
..name = cu.name
..roles = cu.roles;
};

GET['/logout'] = (HttpSession session) {
session['user'] = null;
};

initServer([#drails_example]);
}
7 changes: 7 additions & 0 deletions build.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

// This file is only used by Dart Editor. It displays errors and warnings after
// analyzing a polymer.dart app.

export 'package:polymer/default_build.dart';
38 changes: 38 additions & 0 deletions lib/PolymerElementExt.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'package:polymer/polymer.dart';
import 'package:polymer_expressions/filter.dart';
import 'package:sprintf/sprintf.dart' as SPRINTF;


class PolymerElementExt extends PolymerElement {
PolymerElementExt.created() : super.created();

String sprintf(String fmt, var args) =>
SPRINTF.sprintf(fmt, args);

final asInt = new _AsInt();

final asDate = new _AsDate();

String asSlashedDate(DateTime v) =>
v == null ? '' : sprintf('%02d/%02d/%04d', [v.month, v.day, v.year]);
}

class _AsInt extends Transformer<String, int> {
String forward(int i) => '$i';
int reverse(String s) =>
s == null || s.isEmpty ? 0 : int.parse(s);
}

class _AsDate extends Transformer<String, DateTime> {

@override
String forward(DateTime v) {
if(v == null)
v = new DateTime.now();
return SPRINTF.sprintf('%04d-%02d-%02d', [v.year, v.month, v.day]);
}

@override
DateTime reverse(String t) =>
t == null || t.isEmpty ? new DateTime.now() : DateTime.parse(t);
}
9 changes: 9 additions & 0 deletions lib/drails_sample_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

/// The drails_sample_app library.
library drails_sample_app;

int calculate() {
return 6 * 7;
}
40 changes: 40 additions & 0 deletions lib/main_app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'package:polymer/polymer.dart';
import 'package:drails_sample_app/models.dart';
import 'dart:html';
import 'package:dson/dson.dart';

/// A Polymer `<main-app>` element.
@CustomTag('main-app')
class MainApp extends PolymerElement {

@observable User user;

/// Constructor used to create instance of MainApp.
MainApp.created() : super.created();

ready() {
var usrStr = window.sessionStorage['user'];
if(usrStr != null)
user = deserialize(usrStr, User);
else
user = new User();
}

login(Event e, details, Element target) {
HttpRequest.request("login", method: 'POST', sendData: serialize(user)).then((request) {
window.sessionStorage['user'] = request.response;
user = deserialize(request.response, User);
});
}

logout(Event e, details, Element target) {
HttpRequest.request("logout", method: 'GET').then((req) {
user = new User();
$['core_menu'].selected = 0;
window.sessionStorage.clear();
});
}
}
93 changes: 93 additions & 0 deletions lib/main_app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!--
Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
is governed by a BSD-style license that can be found in the LICENSE file.
-->

<!-- import polymer-element's definition -->
<link rel="import" href="../../packages/polymer/polymer.html">

<link rel="import" href="../../packages/paper_elements/paper_input.html">
<link rel="import" href="../../packages/core_elements/core_scaffold.html">
<link rel="import" href="../../packages/core_elements/core_header_panel.html">
<link rel="import" href="../../packages/core_elements/core_menu.html">
<link rel="import" href="../../packages/core_elements/core_item.html">
<link rel="import" href="../../packages/core_elements/core_icon_button.html">
<link rel="import" href="../../packages/core_elements/core_toolbar.html">
<link rel="import" href="../../packages/core_elements/core_submenu.html">
<link rel="import" href="../../packages/core_elements/core_pages.html">

<link rel="import" href="sample_persons/sample_persons.html">
<link rel="import" href="sample_employees/sample_employees.html">

<polymer-element name="main-app">
<template>
<style>
:host {
position: absolute;
width: 100%;
height: 100%;
box-sizing: border-box;
}
#core_scaffold {
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
width: 100%;
height: 100%;
}
#core_header_panel {
background-color: rgb(255, 255, 255);
}
#core_toolbar {
color: rgb(255, 255, 255);
background-color: rgb(79, 125, 201);
}
#core_menu {
font-size: 16px;
}
#core_pages {
width: 100%;
height: 99.5%;
border: 1px solid silver;
left: 0px;
top: 0px;
position: absolute;
overflow: auto;
}

paper-input {
color: black;
background-color: white;
}
</style>
<core-scaffold id="core_scaffold">
<div id="div">Drails Sample</div>
<core-header-panel mode="seamed" id="core_header_panel" navigation flex>
<core-toolbar id="core_toolbar"></core-toolbar>
<core-menu selected="0" selectedindex="{{ $['core_pages'].selected }}" id="core_menu" theme="core-light-theme">
<core-item id="core_item" icon="settings" label="Persons" horizontal center layout active></core-item>
<template if="{{user.roles != null}}">
<core-item id="core_item1" icon="settings" label="Employees" horizontal center layout></core-item>
</template>
</core-menu>
<template if="{{user == null || user.roles == null}}">
<paper-input label="User Name" inputvalue="{{user.name}}" style="width: 90%"></paper-input>
<paper-input type="password" label="Password" inputvalue="{{user.password}}" style="width: 90%"></paper-input>
<paper-button style="background-color: green" on-click="{{login}}">Login</paper-button>
</template>
<template if="{{user != null && user.roles != null}}">
<paper-button style="background-color: red" on-click="{{logout}}">Logout</paper-button>
</template>
</core-header-panel>
<core-pages selected="0" selectedindex="0" notap id="core_pages">
<section id="section" active><sample-persons></sample-persons></section>
<template if="{{user.roles != null}}">
<section id="section1"><sample-employees user="{{user}}"></sample-employees></section>
</template>
</core-pages>
</core-scaffold>
</template>
<script type="application/dart" src="main_app.dart"></script>
</polymer-element>
Loading

0 comments on commit 82fb68f

Please sign in to comment.