Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sesi 1 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
//your code here
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./db/movie.db');

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

const movies = require('./models/movies')
const prod = require('./models/prodHouses')

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.set('view engine', 'ejs');

app.get('/movies', function(req,res) {
let query = `SELECT M.*, P.name_prodHouse
FROM Movies AS M
LEFT JOIN ProductionHouses AS P
ON P.id = M.prodHouseId`

db.all(query, function(err,rowsMovie) {
if(!err) {
res.render('movie', {dataMovie: rowsMovie});
} else {
res.send(err)
}
})
})

app.get('/movies/edit/:id', function(req,res) {
let query = `SELECT * FROM Movies WHERE id = ${req.params.id}`;
let queryProdHouse = `SELECT * FROM ProductionHouses`

db.all(query, function(errMovies,movie) {
if(!errMovies) {
if(movie.length > 0) {
db.all(queryProdHouse, function(errProdHouse,rowsProdHouse) {
if(!errProdHouse) {
res.render('editMovie', {movie: movie[0], dataProdHouse: rowsProdHouse})
} else {
res.send(errProdHouse)
}
})
}
} else {
res.send(errMovies)
}
})
})

app.post('/movies/edit/:id', function(req,res) {
movies.update(req.body, req.params.id, function(err)) {
res.redirect('/movies');
} else {
res.send(err)
}
})
})

app.get('/prodHouses', function(req,res){
prod.findAll(function(err,rowsProdHouse) {
if(!err) {
res.render('prodHouse', {dataProdHouse: rowsProdHouse});
} else {
res.send(err)
}
})
})

app.listen(3000, function() {
console.log(`Are you looking for me? 3000`);
})
Binary file added db/movie.db
Binary file not shown.
40 changes: 40 additions & 0 deletions models/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./db/movie.db');

class Movies {

findAllWithProd(cb){
let query = `SELECT M.*, P.name_prodHouse
FROM Movies AS M
LEFT JOIN ProductionHouses AS P
ON P.id = M.prodHouseId`

db.all(query, function(err,cb()){

}
}

findID (){

}


update(body,params,cb){
let query = `UPDATE Movies
SET name = '${body.movie}',
released_year = '${body.released_year}',
genre = '${body.genre}',
prodHouseId = ${body.prodHouseId}
WHERE id = ${params}`
db.run(query, function(err){
if(!err) {
cb()
} else {

}
}
}

}

module.exports = Movies
16 changes: 16 additions & 0 deletions models/prodHouses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./db/movie.db');

class ProdHouses {

findAll (cb){
let query = `SELECT * FROM ProductionHouses`

db.all(query, cb()){

}
}

}

module.exports = ProdHouses
202 changes: 202 additions & 0 deletions people.csv

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sesi 3 (20 menit)
- Pisahkan codingan kamu yang ada pada file app.js menjadi model dan routes (mvc)
- Gunakan callback atau promise pada model
Empty file added router/movies.js
Empty file.
37 changes: 37 additions & 0 deletions views/editMovie.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="/movies/edit/<%= movie.id %>" method="post">
<input type="text" name="movie" value="<%= movie.name %>" size="39"><br>

<input type="text" name="released_year" value="<%= movie.released_year %>"><br>

<select name="genre">
<% let genre = ['action', 'comedy', 'drama', 'horor']
genre.forEach(g => {
if (movie.genre === g) {
%>
<option selected value="<%= g %>"> <%= g %></option>
<% }else{ %>
<option value="<%= g %>"> <%= g %></option>
<% }})%>
</select><br>

<select name="prodHouseId">
<% dataProdHouse.forEach(prodHouse => {
if (prodHouse.id === movie.prodHouseId) {
%>
<option selected value="<%= prodHouse.id %>"> <%= prodHouse.name_prodHouse %></option>
<% }else{ %>
<option value="<%= prodHouse.id %>"> <%= prodHouse.name_prodHouse %></option>
<% }})%>
</select><br>

<button type="submit">Save</button>
</form>
</body>
</html>
34 changes: 34 additions & 0 deletions views/movie.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Halaman Movie</h1>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>Movie</th>
<th>Released Year</th>
<th>genre</th>
<th>Production House</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% dataMovie.forEach(movie => { %>
<tr>
<td><%= movie.id %></td>
<td><%= movie.name %></td>
<td><%= movie.released_year %></td>
<td><%= movie.genre %></td>
<td><%= movie.name_prodHouse %></td>
<td><a href="/movies/edit/<%= movie.id %>">Edit</a></td>
</tr>
<% }) %>
</tbody>
</table>
</body>
</html>
10 changes: 10 additions & 0 deletions views/movieedit.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Movie Edit</title>
</head>
<body>

</body>
</html>
20 changes: 20 additions & 0 deletions views/movies.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Movies</title>
</head>
<body>
<h1>Halaman Movies</h1>
<table>
<% for(let i = 0; i < dataMovies.length; i++){%>
<tr>
<td><%=dataMovies[i].name%></td>
<td><%=dataMovies[i].released_year%></td>
<td><%=dataMovies[i].genre%></td>
<td><%=dataMovies[i].name_prodHouse%></td>
</tr>
<%}%>
</table>
</body>
</html>
28 changes: 28 additions & 0 deletions views/prodHouse.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>Halaman Prod. House</h1>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>Production House</th>
<th>Origin City</th>
</tr>
</thead>
<tbody>
<% dataProdHouse.forEach(prodHouse => { %>
<tr>
<td><%= prodHouse.id %></td>
<td><%= prodHouse.name_prodHouse %></td>
<td><%= prodHouse.origin_city%></td>
</tr>
<% }) %>
</tbody>
</table>
</body>
</html>
19 changes: 19 additions & 0 deletions views/prodHouses.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<% console.log(dataHouses) %>
<html>
<head>
<meta charset="utf-8">
<title>Prod Houses</title>
</head>
<body>
<h1>Halaman Prod. House</h1>
<table>
<% for(let i = 0; i < dataHouses.length; i++){%>
<tr>
<td><%=dataHouses[i].name_prodHouse%></td>
<td><%=dataHouses[i].origin_city%></td>
</tr>
<%}%>
</table>
</body>
</html>