-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60239e6
commit 8eb0422
Showing
8 changed files
with
336 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package models | ||
|
||
import slick.jdbc.PostgresProfile.api._ | ||
import scala.concurrent.ExecutionContext | ||
import models.Tables._ | ||
import scala.concurrent.Future | ||
import org.mindrot.jbcrypt.BCrypt | ||
|
||
class TaskListDatabaseModel(db: Database)(implicit ec: ExecutionContext) { | ||
def validateUser(username: String, password: String): Future[Option[Int]] = { | ||
val matches = db.run(Users.filter(userRow => userRow.username === username).result) | ||
matches.map(userRows => userRows.headOption.flatMap { | ||
userRow => if (BCrypt.checkpw(password, userRow.password)) Some(userRow.id) else None | ||
}) | ||
} | ||
|
||
def createUser(username: String, password: String): Future[Option[Int]] = { | ||
val matches = db.run(Users.filter(userRow => userRow.username === username).result) | ||
matches.flatMap { userRows => | ||
if (userRows.isEmpty) { | ||
db.run(Users += UsersRow(-1, username, BCrypt.hashpw(password, BCrypt.gensalt()))) | ||
.flatMap { addCount => | ||
if (addCount > 0) db.run(Users.filter(userRow => userRow.username === username).result) | ||
.map(_.headOption.map(_.id)) | ||
else Future.successful(None) | ||
} | ||
} else Future.successful(None) | ||
} | ||
} | ||
|
||
def getTasks(username: String): Future[Seq[TaskItem]] = { | ||
db.run( | ||
(for { | ||
user <- Users if user.username === username | ||
item <- Items if item.userId === user.id | ||
} yield { | ||
item | ||
}).result | ||
).map(items => items.map(item => TaskItem(item.itemId, item.text))) | ||
} | ||
|
||
def addTask(userid: Int, task: String): Future[Int] = { | ||
db.run(Items += ItemsRow(-1, userid, task)) | ||
} | ||
|
||
def removeTask(itemId: Int): Future[Boolean] = { | ||
db.run( Items.filter(_.itemId === itemId).delete ).map(count => count > 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package models | ||
|
||
case class UserData(username: String, password: String) | ||
case class UserData(username: String, password: String) | ||
case class TaskItem(id: Int, text: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@()(implicit request: RequestHeader, flash: Flash) | ||
|
||
@main("Task List 5"){ | ||
<input type="hidden" id="csrfToken" value="@helper.CSRF.getToken.value"> | ||
<input type="hidden" id="validateRoute" value="@routes.TaskList5.validate()"> | ||
<input type="hidden" id="tasksRoute" value="@routes.TaskList5.taskList()"> | ||
<input type="hidden" id="createRoute" value="@routes.TaskList5.createUser()"> | ||
<input type="hidden" id="deleteRoute" value="@routes.TaskList5.delete()"> | ||
<input type="hidden" id="addRoute" value="@routes.TaskList5.addTask()"> | ||
<input type="hidden" id="logoutRoute" value="@routes.TaskList5.logout()"> | ||
|
||
<div id="react-root"></div> | ||
<script src="@routes.Assets.versioned("javascript/version5.js")"></script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
console.log("Running version 5."); | ||
|
||
const ce = React.createElement | ||
const csrfToken = document.getElementById("csrfToken").value; | ||
const validateRoute = document.getElementById("validateRoute").value; | ||
const tasksRoute = document.getElementById("tasksRoute").value; | ||
const createRoute = document.getElementById("createRoute").value; | ||
const deleteRoute = document.getElementById("deleteRoute").value; | ||
const addRoute = document.getElementById("addRoute").value; | ||
const logoutRoute = document.getElementById("logoutRoute").value; | ||
|
||
class Version5MainComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { loggedIn: false }; | ||
} | ||
|
||
render() { | ||
if (this.state.loggedIn) { | ||
return ce(TaskListComponent, { doLogout: () => this.setState( { loggedIn: false})}); | ||
} else { | ||
return ce(LoginComponent, { doLogin: () => this.setState( { loggedIn: true }) }); | ||
} | ||
} | ||
} | ||
|
||
class LoginComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
loginName: "", | ||
loginPass: "", | ||
createName: "", | ||
createPass: "", | ||
loginMessage: "", | ||
createMessage: "" | ||
}; | ||
} | ||
|
||
render() { | ||
return ce('div', null, | ||
ce('h2', null, 'Login:'), | ||
ce('br'), | ||
'Username: ', | ||
ce('input', {type: "text", id: "loginName", value: this.state.loginName, onChange: e => this.changerHandler(e)}), | ||
ce('br'), | ||
'Password: ', | ||
ce('input', {type: "password", id: "loginPass", value: this.state.loginPass, onChange: e => this.changerHandler(e)}), | ||
ce('br'), | ||
ce('button', {onClick: e => this.login(e)}, 'Login'), | ||
ce('span', {id: "login-message"}, this.state.loginMessage), | ||
ce('h2', null, 'Create User:'), | ||
ce('br'), | ||
'Username: ', | ||
ce('input', {type: "text", id: "createName", value: this.state.createName, onChange: e => this.changerHandler(e)}), | ||
ce('br'), | ||
'Password: ', | ||
ce('input', {type: "password", id: "createPass", value: this.state.createPass, onChange: e => this.changerHandler(e)}), | ||
ce('br'), | ||
ce('button', {onClick: e => this.createUser(e)}, 'Create User'), | ||
ce('span', {id: "create-message"}, this.state.createMessage) | ||
); | ||
} | ||
|
||
changerHandler(e) { | ||
this.setState({ [e.target['id']]: e.target.value }); | ||
} | ||
|
||
login(e) { | ||
const username = this.state.loginName; | ||
const password = this.state.loginPass; | ||
fetch(validateRoute, { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json', 'Csrf-Token': csrfToken }, | ||
body: JSON.stringify({ username, password }) | ||
}).then(res => res.json()).then(data => { | ||
if(data) { | ||
this.props.doLogin(); | ||
} else { | ||
this.setState({ loginMessage: "Login Failed" }); | ||
} | ||
}); | ||
} | ||
|
||
createUser() { | ||
const username = this.state.createName; | ||
const password = this.state.createPass; | ||
fetch(createRoute, { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json', 'Csrf-Token': csrfToken }, | ||
body: JSON.stringify({ username, password }) | ||
}).then(res => res.json()).then(data => { | ||
if(data) { | ||
this.props.doLogin(); | ||
} else { | ||
this.setState({ createMessage: "User Creation Failed"}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
class TaskListComponent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { tasks: [], newTask: "", taskMessage: "" }; | ||
} | ||
|
||
componentDidMount() { | ||
this.loadTasks(); | ||
} | ||
|
||
render() { | ||
return ce('div', null, | ||
'Task List', | ||
ce('br'), | ||
ce('ul', null, | ||
this.state.tasks.map(task => ce('li', { key: task.id, onClick: e => this.handleDeleteClick(task.id) }, task.text)) | ||
), | ||
ce('br'), | ||
ce('div', null, | ||
ce('input', {type: 'text', value: this.state.newTask, onChange: e => this.handleChange(e) }), | ||
ce('button', {onClick: e => this.handleAddClick(e)}, 'Add Task'), | ||
this.state.taskMessage | ||
), | ||
ce('br'), | ||
ce('button', { onClick: e => this.props.doLogout() }, 'Log out') | ||
); | ||
} | ||
|
||
loadTasks() { | ||
fetch(tasksRoute).then(res => res.json()).then(tasks => this.setState({ tasks })); | ||
} | ||
|
||
handleChange(e) { | ||
this.setState({newTask: e.target.value}) | ||
} | ||
|
||
handleAddClick(e) { | ||
fetch(addRoute, { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json', 'Csrf-Token': csrfToken }, | ||
body: JSON.stringify(this.state.newTask) | ||
}).then(res => res.json()).then(data => { | ||
if(data) { | ||
this.loadTasks(); | ||
this.setState({ taskMessage: "", newTask: "" }); | ||
} else { | ||
this.setState({ taskMessage: "Failed to add." }); | ||
} | ||
}); | ||
} | ||
|
||
handleDeleteClick(i) { | ||
fetch(deleteRoute, { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json', 'Csrf-Token': csrfToken }, | ||
body: JSON.stringify(i) | ||
}).then(res => res.json()).then(data => { | ||
if(data) { | ||
this.loadTasks(); | ||
this.setState({ taskMessage: "" }); | ||
} else { | ||
this.setState({ taskMessage: "Failed to delete."}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
ReactDOM.render( | ||
ce(Version5MainComponent, null, null), | ||
document.getElementById('react-root') | ||
); |
Oops, something went wrong.