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

Fransiskus teddy #13

Open
wants to merge 8 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
31 changes: 31 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
const routesIndex = require('./routes/index.js')

// Controller
const Student = require('./controller').Student
// Student.methods(){}

// TEMPLATE ENGINE
app.set('view engine', 'ejs')

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

// PUBLIC FOLDER
app.use(express.static('public'))

// ROUTER INDEX SET
app.use('/', routesIndex)


// STUDENTS CRUD CALL
let argv = process.argv;
// Controller.

// RUN
app.listen(3003, ()=>{
console.log('listening on PORT 3003');
})
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "werkbau",
"password": "pokemon",
"database": "express",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "werkbau",
"password": "pokemon",
"database": "database_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "werkbau",
"password": "pokemon",
"database": "database_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
13 changes: 13 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Student = require('./studentCRUD.js')
const Teacher = require('./teacherCRUD.js')
const Subject = require('./subjectCRUD.js')
// const User = require('./userCRUD.js')




module.exports = {
Student : Student,
Teacher : Teacher,
Subject : Subject
}
165 changes: 165 additions & 0 deletions controller/studentCRUD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
'use strict'
const db = require('../models/index.js');
const View = require('../views/index.js');
const express = require('express');

class Student {
constructor() {

}

static readCommands(argv){
// node app.js <tableName> <command> <options>
let target = argv[2];
let command = argv[3];
let options = argv.slice(4);

if (target.toLowerCase() == "students") {
switch (command.toLowerCase()) {
case 'add':
Student.addStudent(options);
break;
case 'read':
Student.readStudent(options);
break;
case 'update':
Student.updateStudent(options);
break;
case 'delete':
Student.deleteStudent(options);
break;
default: console.log('command invalid');

}
} else {
console.log('target invalid');
}
}

// CRUD
static addStudent(options, res){
db.Student.create({
first_name: options[0],
last_name: options[1],
createdAt: new Date(),
updatedAt: new Date(),
email: options[2]
}).then(newStudent => {
if (res) {
Student.tableResponse(res, newStudent, 'added')
} else {
View.displayAddData(newStudent);
}

}).catch(err=>{
console.log('-----',err.message);
})
}

static readStudent(options){
if (options[0]) {
db.Student.findOne({
where:{
id: options[0]
}
}).then(foundStudent => {
View.displayOneFound(foundStudent);
});
} else {
db.Student.findAll({
// attributes
}).then(foundStudents => {
View.displayManyFound(foundStudents);
});
}
}

static updateStudent(options, res){
db.Student.findOne({
where:{id:options[0]}
}).then(foundStudent => {
if (res) {
let updateData = {
first_name: options[1],
last_name: options[2],
email: options[3]
};
foundStudent.update(updateData).then(()=>{
// Student.tableResponse(res, foundStudent, 'Edited')
View.redirect(res, `/students`);
});

} else {
let updateData = {};
updateData[options[1]] = options[2];
foundStudent.update(updateData).then(()=>{
View.displayUpdate(foundStudent);
});
}
});
}

static deleteStudent(options, res){
db.Student.findOne({
where:{id:options[0]}
}).then(foundStudent => {
if (res) {
return foundStudent.destroy().then(()=>{
View.redirect(res, '/students');
})
} else {
View.displayDestroyed(foundStudent);
return foundStudent.destroy();
}
});
}

// display table
static tableResponse(res, newData, method){
db.Student.findAll({
include:[{
model: db.Subject
}],
attributes: ['id', ['first_name', 'First Name'], ['last_name', 'Last Name'], 'email'],
order:[['first_name', 'ASC']]
}).then(foundStudents => {
View.displayStudentTable(res, foundStudents, 'Students', newData, method);
});
}

static showSubjectAddForm(id, res){


db.Student.findOne({
where:{id:id}
}).then((foundStudent)=>{
res.render('./students_view/formAddSubject.ejs',
{ title:'Edit Student',
h1:'Add Subject to Student',
id: id,
path:'students',
foundStudent:foundStudent
})
})
}

static updateSubject(studentData, StudentId, res){
let Subject = studentData.Subject;
db.Subject.findOne({
where:{
subject_name: Subject
}
}).then(foundSubject=>{
db.StudentSubject.create({
score: null,
StudentId: StudentId,
SubjectId: foundSubject.id
}).then(newConjunct=>{
View.redirect(res, '/students')
})
})
}

}

module.exports = Student;
Loading