/lib/model.dart
part of controllers;
class Person extends PersistentObject {
// String get collectionName => "Name";
String get firstName => getProperty('first_name');
set firstName(String value) => setProperty('first_name', value);
String get lastName => getProperty('last_name');
set lastName(String value) => setProperty('last_name', value);
String toString() => "$firstName $lastName";
Future<bool> isDuplicate() {
Completer completer = new Completer();
bool answer;
objectory.initDomainModel().then((success) {
objectory[Person].find(where.eq('first_name', this.firstName)
.and(where.eq('last_name', this.lastName))).then((matches) {
if (matches.length > 0) {
answer = true;
} else {
answer = false;
}
completer.complete(answer);
});
});
return completer.future;
}
}
add(Person person) {
/* objectory =
new ObjectoryDirectConnectionImpl('mongodb://127.0.0.1/forceobject2',
() { objectory.registerClass(Person, () => new Person(),
() => new List<Person>()); }, false); */
// init();
objectory.initDomainModel().then((_) {
objectory.save(person);
});
}
Future<Person> newPerson(String first, String last) {
Completer completer = new Completer();
/* objectory =
new ObjectoryDirectConnectionImpl('mongodb://127.0.0.1/forceobject2',
() => objectory.registerClass(Person, () => new Person(),
() => new List<Person>()), false);
*/ //init();
objectory.initDomainModel().then((_) {
Person person = new Person()
..firstName = first
..lastName = last;
completer.complete(person);
// objectory.close();
});
return completer.future;
}
Future<List<Person>> allPeople() {
Completer completer = new Completer();
/* objectory =
new ObjectoryDirectConnectionImpl('mongodb://127.0.0.1/forceobject2',
() => objectory.registerClass(Person, () => new Person(),
() => new List<Person>()), false);
*/
// List<Person> names = new List();
// init();
objectory.initDomainModel().then((success) =>
objectory[Person].find().then((List<Person> people){
completer.complete(people);
// objectory.close();
})
);
return completer.future;
}
Future<bool> isDuplicate(Person person) {
Completer completer = new Completer();
bool answer;
objectory.initDomainModel().then((success) {
objectory[Person].find(where.eq('first_name', person.firstName)
.and(where.eq('last_name', person.lastName))).then((matches) {
if (matches.length > 0) {
answer = true;
} else {
answer = false;
}
completer.complete(answer);
});
});
return completer.future;
}
/lib/controllers.dart
library controllers;
import 'package:objectory/objectory_console.dart'
;import 'package:forcemvc/force_mvc.dart';
import 'dart:async';
part 'model.dart';
init() {
objectory = new ObjectoryDirectConnectionImpl(
'mongodb://127.0.0.1/forceobjectory2', () =>
objectory.registerClass(
Person, () => new Person(), () => new List<Person>()), false);
}
@Controller
class MainControllers {
@RequestMapping(value: '/', method: RequestMethod.GET)
String index(ForceRequest req, Model model) {
return 'index';
}
@RequestMapping(value: '/verify', method: RequestMethod.POST)
Future<String> verify(ForceRequest req, Model model) {
req.getPostParams().then((Map params) {
init();
String first = params['first_name'];
String last = params['last_name'];
newPerson(first, last).then((Person person) {
model.addAttribute("person", person);
// return isDuplicate(person).then((bool match) {
return person.isDuplicate().then((bool match) {
if (!match) {
return person.save();
}
});
}).then((_) {
return allPeople().then((List people) =>
model.addAttribute("people", people));
}).then((_) {
model.addAttribute("message", "It works!");
req.async('verify');
});
});
return req.asyncFuture;
}
}
No comments:
Post a Comment