Skip to content

Commit

Permalink
single responsibility bad added
Browse files Browse the repository at this point in the history
  • Loading branch information
vimukthi-git committed Jun 5, 2016
1 parent aedf0bd commit 694a771
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
16 changes: 15 additions & 1 deletion controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ const render = views(__dirname + '/views', {
map: { html: 'swig' }
});

module.exports.loginPage = function *loginPage(ctx) {
this.body = yield render('login');
};

module.exports.login = function *login(ctx) {
let posted = yield parse(this);
let user = yield model.getUserByUsername(posted.username);
if (user && user.password === posted.password) {
this.redirect('/home');
} else {
this.redirect('/');
}
};

module.exports.home = function *home(ctx) {
let nots = yield model.list();
this.body = yield render('list', {
Expand All @@ -29,5 +43,5 @@ module.exports.fetch = function *fetch(id) {
module.exports.create = function *create() {
const message = yield parse(this);
yield model.create(message);
this.redirect('/');
this.redirect('/home');
};
20 changes: 20 additions & 0 deletions model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ let notices = [
}
];

let users = [
{
username: 'vimukthi',
password: '123'
},
{
username: 'other',
password: '123'
}
];

module.exports.list = function* list() {
let nots = notices.slice();
nots.reverse();
Expand All @@ -25,3 +36,12 @@ module.exports.create = function* create(message) {
message.id = id;
return message;
};

module.exports.getUserByUsername = function* getUserByUsername(username) {
for (let user of users) {
if (user.username === username) {
return user;
}
}
return false;
};
9 changes: 9 additions & 0 deletions public/styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ textarea {
font: 30px helvetica;
font-weight: 100;
}

#login_form {
position: absolute;
top: 30%;
left: 40%;
right: 30%;
bottom: 20%;
font-size: 18px;
}
4 changes: 3 additions & 1 deletion router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const views = require('co-views');
const parse = require('co-body');

module.exports = (app) => {
app.use(route.get('/', controller.home));
app.use(route.get('/', controller.loginPage));
app.use(route.post('/login', controller.login));
app.use(route.get('/home', controller.home));
app.use(route.get('/messages', controller.list));
app.use(route.get('/messages/:id', controller.fetch));
app.use(route.post('/messages', controller.create));
Expand Down
3 changes: 2 additions & 1 deletion views/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<link rel="stylesheet" type="text/css" href="styles/main.css">
</head>
<body>
<h1>Aftership Notice Board</h1>
<section id="content">
{% block content %}
<p>Forgot to add content?</p>
{% endblock %}
</section>
</body>
</html>
</html>
1 change: 0 additions & 1 deletion views/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{% block title %}Messages{% endblock %}

{% block content %}
<h1>Aftership Notice Board</h1>
<ul class="messages">
{% for message in messages %}
<li class="message">{{ message.message }}</li>
Expand Down
20 changes: 20 additions & 0 deletions views/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'layout.html' %}

{% block title %}Login{% endblock %}
{% block content %}
<form id="login_form" action="/login" method="post">
<p>
<label>Username:
<input type="text" name="username" value="test" />
</label>
</p>
<p>
<label>Password:
<input type="password" name="password" value="test" />
</label>
</p>
<p>
<button type="submit">Log In</button>
</p>
</form>
{% endblock %}

0 comments on commit 694a771

Please sign in to comment.