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

Agung pangestu #26

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
24 changes: 24 additions & 0 deletions movies/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const bodyParser = require('body-parser')

const app = express();

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

// parse application/json
app.use(bodyParser.json())


app.set('views', './views') // specify the views directory
app.set('view engine', 'ejs') // register the template engine


const movie = require('./routers/movies');
const cast = require('./routers/cast')

app.use('/movie',movie)
app.use('/casts',cast)



app.listen(3000, () => console.log('Example app listening on port 3000!'))
9 changes: 9 additions & 0 deletions movies/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"development": {
"username": "agungpangestu",
"password": null,
"database": "live-code",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
36 changes: 36 additions & 0 deletions movies/migrations/20171109031558-create-cast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Casts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
first_name: {
type: Sequelize.STRING
},
last_name: {
type: Sequelize.STRING
},
birth_year: {
type: Sequelize.STRING
},
gender: {
type: Sequelize.STRING
},
createdAt: {
allowNull: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Casts');
}
};
33 changes: 33 additions & 0 deletions movies/migrations/20171109031757-create-movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Movies', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
released_year: {
type: Sequelize.STRING
},
genre: {
type: Sequelize.STRING
},
createdAt: {
allowNull: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Movies');
}
};
33 changes: 33 additions & 0 deletions movies/migrations/20171109031948-create-cast-movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('CastMovies', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
CastId: {
type: Sequelize.INTEGER
},
MovieId: {
type: Sequelize.INTEGER
},
role: {
type: Sequelize.STRING
},
createdAt: {
allowNull: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: true,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('CastMovies');
}
};
33 changes: 33 additions & 0 deletions movies/models/cast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Cast = sequelize.define('Cast', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
birth_year: DataTypes.STRING,
gender: DataTypes.STRING
},
{
getterMethods: {
fullName() {
return this.first_name + ' ' + this.last_name
}
},

setterMethods: {
fullName(value) {
const names = value.split(' ');

this.setDataValue('first_name', names.slice(0, -1).join(' '));
this.setDataValue('last_name', names.slice(-1).join(' '));
},
}
})

// Cast.associate = function (models) {
// Cast.hasMany(models.Movie)
// Cast.belongsToMany(models.CastMovie,{through : models.CastMovie})
// }


return Cast;
};
16 changes: 16 additions & 0 deletions movies/models/castmovie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var CastMovie = sequelize.define('CastMovie', {
CastId: DataTypes.INTEGER,
MovieId: DataTypes.INTEGER,
role:DataTypes.STRING

});

// CastMovie.associate = function (models) {
// CastMovie.belongsTo(models.Movie)
// CastMovie.belongsTo(models.Cast)
// };

return CastMovie;
};
36 changes: 36 additions & 0 deletions movies/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var basename = path.basename(__filename);
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
var db = {};

if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
14 changes: 14 additions & 0 deletions movies/models/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
var Movie = sequelize.define('Movie', {
name: DataTypes.STRING,
released_year: DataTypes.STRING,
genre: DataTypes.STRING
});

// Movie.associate = function (models) {
// Movie.hasMany(models.Cast)
// Movie.belongsToMany(models.CastMovie,{through : models.CastMovie})
// }
return Movie;
};
19 changes: 19 additions & 0 deletions movies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "movies",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"ejs": "^2.5.7",
"express": "^4.16.2",
"pg": "^7.4.0",
"sequelize": "^4.22.5"
}
}
52 changes: 52 additions & 0 deletions movies/routers/cast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require('express');
const Models = require('../models');


const router = express.Router();


router.get('/',(req,res)=>{
Models.Cast.findAll().then(data=>{
// res.send(data)
res.render('casts',{data})
})
})

router.get('/add',(req,res)=>{
res.render('addcast')
})

router.post('/add',(req,res)=>{
Models.Cast.create({first_name: req.body.first_name,last_name :req.body.last_name,birth_year:req.body.birth_year,gender :req.body.gender}).then(()=>{
res.redirect('/casts')

})
})

router.get('/edit/:id',(req,res)=>{
Models.Cast.findById(req.params.id).then(data=>{
// res.send(data)
res.render('editcast',{data})

})
})
router.get('/delete/:id',(req,res)=>{
Models.Cast.destroy({where:{id :req.params.id}}).then(data=>{
res.redirect('/casts')
})
})



module.exports = router;



// Buatlah CRUD untuk model Cast dengan (20)
// Untuk menampilkan list, pada route GET “/casts”. Tampilkan Fullname, Birth Year, Gender. Untuk menampilkan fullname buatlah instance method pada model Cast. (-3 jika tidak menggunakan instance method) (5)
// Untuk menambah data, route “/casts/add” (4)
// Gunakan dropdown untuk memasukkan nilai gender
// Untuk update data, route “/casts/edit/:id” (9)
// Data pada dropdown harus ter-populate untuk memasukkan nilai gender (-5 bila pada form edit memunculkan gender yang tidak sesuai dengan data sebelumnya)
// Untuk delete data, route “/casts/delete/:id” (2)
//
16 changes: 16 additions & 0 deletions movies/routers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const bodyParser = require('body-parser')
const Models = require('../models');


const router = express.Router();


router.get('/',(req,res)=>{
Models.Movie.findAll().then(data=>{
// res.send(d)
res.render('movies',{data})
})
})

module.exports = router;
53 changes: 53 additions & 0 deletions movies/seeders/20171109032805-my-movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('Movies', [{
name: 'Cintaku Kepentok Sudoku',
released_year: '2016',
genre : "Komedi"
},
{
name: 'Ratapan di Depan Mac Mini',
released_year: '2014',
genre : "Drama"
},
{
name: 'Boggle 2, The Haunting Anchor',
released_year: '2011',
genre : "Horor"
},
{
name: 'Taubat Instruktur Karena Tugas Tak Dibuat',
released_year: '2000',
genre : "Religi"
},
{
name: 'Heroku wa Heromu da',
released_year: '2008',
genre : "Animasi"
}]
, {});
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
},

down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Movies', null, {});
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.

Example:
return queryInterface.bulkDelete('Person', null, {});
*/
}
};
Loading