mirror of https://github.com/E-Almqvist/hsf
parent
3747c6dfb8
commit
f2ac39db47
@ -1,19 +1,116 @@ |
||||
#!/usr/bin/ruby -w |
||||
require 'sinatra' |
||||
require 'slim' |
||||
require 'sqlite3' |
||||
require 'bcrypt' |
||||
|
||||
#1. Skapa ER + databas som kan hålla användare och todos. Fota ER-diagram, |
||||
# lägg i misc-mapp |
||||
#2. Skapa ett formulär för att registrerara användare. |
||||
# lägg i misc-mapp [x] |
||||
#2. Skapa ett formulär för att registrerara användare. [x] |
||||
#3. Skapa ett formulär för att logga in. Om användaren lyckas logga |
||||
# in: Spara information i session som håller koll på att användaren är inloggad |
||||
#4. Låt inloggad användare skapa todos i ett formulär (på en ny sida ELLER på sidan som visar todos.). |
||||
#5. Låt inloggad användare updatera och ta bort sina formulär. |
||||
# in: Spara information i session som håller koll på att användaren är inloggad [x] |
||||
#4. Låt inloggad användare skapa todos i ett formulär (på en ny sida ELLER på sidan som visar todos.). [x] |
||||
#5. Låt inloggad användare updatera och ta bort sina formulär. [x] |
||||
#6. Lägg till felhantering (meddelande om man skriver in fel user/lösen) |
||||
|
||||
enable :sessions |
||||
|
||||
def create_db_handle |
||||
SQLite3::Database.new("db/todo.db") |
||||
end |
||||
|
||||
|
||||
get "/" do |
||||
slim(:register) |
||||
slim :register |
||||
end |
||||
|
||||
|
||||
get "/showlogin" do |
||||
slim :login |
||||
end |
||||
|
||||
get "/todos" do |
||||
id = session[:id].to_i |
||||
db = create_db_handle |
||||
db.results_as_hash = true |
||||
|
||||
todos = db.execute "SELECT * FROM Todos WHERE user_id = ?", id |
||||
|
||||
slim :"todos/index", locals: {todos: todos} |
||||
end |
||||
|
||||
get "/todos/:tid/edit" do |
||||
tid = params[:tid].to_i |
||||
|
||||
db = create_db_handle |
||||
db.results_as_hash = true |
||||
todo = db.execute("SELECT * FROM Todos WHERE id = ?", tid).first |
||||
|
||||
if( todo["user_id"] == session[:id] ) then |
||||
slim :"todos/edit", locals: {todo: todo} |
||||
else |
||||
"401, access denied!" |
||||
end |
||||
end |
||||
|
||||
post "/todos/:tid/update" do |
||||
tid = params[:tid].to_i |
||||
new_content = params[:content] |
||||
|
||||
db = create_db_handle |
||||
db.execute "UPDATE Todos SET content = ? WHERE id = ?", new_content, tid |
||||
|
||||
redirect "/todos" |
||||
end |
||||
|
||||
post "/todos/:tid/delete" do |
||||
tid = params[:tid].to_i |
||||
|
||||
db = create_db_handle |
||||
db.execute "DELETE FROM Todos WHERE id = ?", tid |
||||
|
||||
redirect "/todos" |
||||
end |
||||
|
||||
post "/todos/new" do |
||||
id = params[:id].to_i |
||||
content = params[:content] |
||||
|
||||
db = create_db_handle |
||||
db.execute "INSERT INTO Todos (user_id, content) VALUES (?, ?)", id, content |
||||
|
||||
redirect "/todos" |
||||
end |
||||
|
||||
post "/users/new" do |
||||
username = params[:username] |
||||
password = params[:password] |
||||
password2 = params[:password_confirm] |
||||
|
||||
if( password == password2 ) then |
||||
pwdigest = BCrypt::Password.create(password) |
||||
db = create_db_handle |
||||
db.execute("INSERT INTO Users (username, pwdigest) VALUES (?, ?)", username, pwdigest) |
||||
|
||||
redirect "/" |
||||
else |
||||
"Passwords does not match..." |
||||
end |
||||
end |
||||
|
||||
post "/users/login" do |
||||
username = params[:username] |
||||
password = params[:password] |
||||
|
||||
db = create_db_handle |
||||
db.results_as_hash = true |
||||
user_info = db.execute("SELECT * FROM Users WHERE username = ?", username).first |
||||
|
||||
dbpass = BCrypt::Password.new(user_info["pwdigest"]) |
||||
if( dbpass == password ) then |
||||
session[:id] = user_info["id"] |
||||
redirect "/todos" |
||||
else |
||||
"WRONG PASSWORD!" |
||||
end |
||||
end |
||||
|
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
@ -0,0 +1 @@ |
||||
<mxfile host="app.diagrams.net" modified="2022-01-19T08:59:02.610Z" agent="5.0 (X11)" etag="PiVFU_WCTCR91cyxY9Ob" version="16.3.0" type="device"><diagram id="veyYhuRtbIGMNOb6CXEn" name="Page-1">7VlNc5swEP01X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a3/913aGv+eu9M/</diagram></mxfile> |
@ -1,12 +1,17 @@ |
||||
nav { |
||||
font-family: Arial, Helvetica, sans-serif; |
||||
text-transform: uppercase; |
||||
text-decoration: none; |
||||
font-size: 1.2em; |
||||
font-weight:800; |
||||
background: lightsalmon; |
||||
height: 10vh; |
||||
display:flex; |
||||
justify-content:space-around; |
||||
align-items:center; |
||||
} |
||||
font-family: Arial, Helvetica, sans-serif; |
||||
text-transform: uppercase; |
||||
text-decoration: none; |
||||
font-size: 1.2em; |
||||
font-weight:800; |
||||
background: #aaf; |
||||
height: 10vh; |
||||
display:flex; |
||||
justify-content:space-around; |
||||
align-items:center; |
||||
} |
||||
|
||||
.inline { |
||||
display: inline-block; |
||||
margin: 8px; |
||||
} |
||||
|
@ -0,0 +1,5 @@ |
||||
label Login |
||||
form action="/users/login" method="post" |
||||
input type="text" name="username" placeholder="Username" |
||||
input type="password" name="password" placeholder="Password" |
||||
input type="submit" value="Login" |
@ -0,0 +1,6 @@ |
||||
label Register |
||||
form action="/users/new" method="post" |
||||
input type="text" name="username" placeholder="Username" |
||||
input type="password" name="password" placeholder="Password" |
||||
input type="password" name="password_confirm" placeholder="Confirm password" |
||||
input type="submit" value="Register" |
@ -0,0 +1,3 @@ |
||||
form action="/todos/#{todo["id"]}/update" method="post" |
||||
input type="text" name="content" placeholder="New TODO text..." |
||||
input type="submit" value="Update TODO" |
@ -0,0 +1,19 @@ |
||||
h2 Create TODO: |
||||
form action="/todos/new" method="post" |
||||
input type="text" name="content" placeholder="I need to do..." |
||||
input type="hidden" name="id" value="#{session[:id]}" |
||||
input type="submit" value="Submit" |
||||
|
||||
h2 Your TODOS: |
||||
ol |
||||
- todos.each do |todo| |
||||
li |
||||
label = todo["content"] |
||||
|
||||
form.inline action="todos/#{todo["id"]}/edit" method="get" |
||||
input type="submit" value="Edit" |
||||
|
||||
form.inline action="todos/#{todo["id"]}/delete" method="post" |
||||
input type="hidden" name="user_id" value="#{session[:id]}" |
||||
input type="hidden" name="todo_id" value="#{todo["id"]}" |
||||
input type="submit" value="Remove" |
Loading…
Reference in new issue