Skip to content

Commit

Permalink
Gotov backend za kreiranje zadace
Browse files Browse the repository at this point in the history
  • Loading branch information
Peco231 committed Apr 20, 2019
1 parent fc25dec commit 440b6d7
Show file tree
Hide file tree
Showing 17 changed files with 1,009 additions and 19 deletions.
12 changes: 6 additions & 6 deletions dbComponents/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ db.Sequelize = Sequelize;
db.sequelize = sequelize;

//import modela
db.zadaca = sequelize.import(__dirname + '/zadaca.js');
db.zadatak = sequelize.import(__dirname + '/zadatak.js');
db.mimeTip = sequelize.import(__dirname + '/mimeTip.js');
db.Zadaca = sequelize.import(__dirname + '/zadaca.js');
db.Zadatak = sequelize.import(__dirname + '/zadatak.js');
db.MimeTip = sequelize.import(__dirname + '/mimeTip.js');

//relacije
db.zadaca.hasMany(db.zadatak, {as: 'zadaci' , foreignKey: 'idZadaca' });
db.zadatak.hasMany(db.mimeTip, {as: 'mimeTipovi' , foreignKey: 'idZadatak' });
db.Zadaca.hasMany(db.Zadatak, {as: 'zadaci' , foreignKey: 'idZadaca' });
db.Zadatak.hasMany(db.MimeTip, {as: 'mimeTipovi' , foreignKey: 'idZadatak' });

module.exports=db;
module.exports = db;
11 changes: 8 additions & 3 deletions dbComponents/mimeTip.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const Sequelize = require("sequelize");

module.exports = function(sequelize){
const Zadatak = sequelize.define("zadatak",{
idZadatka : Sequelize.INTEGER,
const MimeTip = sequelize.define("mimeTip",{
idMimeTip : {type: Sequelize.INTEGER, primaryKey : true, autoIncrement : true},
idZadatak : Sequelize.INTEGER,
mimeTip : Sequelize.STRING
}, {
timestamps: false,
freezeTableName: true,
tableName: 'MimeTip'
})
return Zadatak;
return MimeTip;
};
14 changes: 10 additions & 4 deletions dbComponents/zadaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ const Sequelize = require("sequelize");

module.exports = function(sequelize){
const Zadaca = sequelize.define("zadaca",{
idZadaca : {type: Sequelize.INTEGER, primaryKey : true, autoIncrement : true},
idPredmet : Sequelize.INTEGER,
naziv : Sequelize.STRING,
brojZadataka : Sequelize.INTEGER,
rokZaPredaju : Sequelize.DATE,
maxBrojBodova : Sequelize.INTEGER,
ukupnoOstvareniBodovi : Sequelize.INTEGER,
postavkaZadace : Sequelize.BLOB
ukupnoBodova : Sequelize.INTEGER,
ukupniOstvareniBodovi : Sequelize.INTEGER,
postavka : Sequelize.BLOB,
}, {
timestamps: false,
freezeTableName: true,
tableName: 'Zadaca'
})
return Zadaca;
};
};
11 changes: 9 additions & 2 deletions dbComponents/zadatak.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const Sequelize = require("sequelize");

module.exports = function(sequelize){
const Zadatak = sequelize.define("zadatak",{
idZadatak : {type: Sequelize.INTEGER, primaryKey : true, autoIncrement : true},
idZadaca : Sequelize.INTEGER,
nazivZadataka : Sequelize.STRING,
redniBrojZadatkaUZadaci : Sequelize.INTEGER,
maxBrojBodova : Sequelize.INTEGER,
brojOstvarenihBodova : Sequelize.INTEGER,
profesorovKomentar : Sequelize.STRING,
Expand All @@ -12,6 +13,12 @@ module.exports = function(sequelize){
sadrzajFile : Sequelize.BLOB,
velicinaFile : Sequelize.INTEGER,
mimeTipUpdateZadatka : Sequelize.STRING
}, {
timestamps: false,
freezeTableName: true,
tableName: 'Zadatak'
})
return Zadatak;
};
};

//DROP TABLE mimeTips, zadataks, zadaca
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ const morgan = require('morgan');
const bodyParser = require('body-parser');
const db = require('./dbComponents/db.js');
const app = express();
const cors = require('cors');

app.use('*', cors()); // enable cors

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('dev'))

db.sequelize.sync({force:false}).then(() => {
db.sequelize.sync({force:true}).then(() => {
console.log("Usao u bazu!");
}).catch((e) => {
console.log("greska");
Expand All @@ -17,13 +20,49 @@ db.sequelize.sync({force:false}).then(() => {


app.post('/addZadaca', function(req, res) {

var bodyReq = req.body;


db.Zadaca.findOrCreate({where:{
idPredmet : bodyReq.idPredmeta,
naziv : bodyReq.naziv,
brojZadataka : bodyReq.brojZadataka,
rokZaPredaju : bodyReq.datum + " " + bodyReq.vrijeme + ":59",
ukupnoBodova : bodyReq.ukupnoBodova,
ukupniOstvareniBodovi : 0,
postavka: bodyReq.postavka,
}}).then(function(dodanaZadaca){
console.log(dodanaZadaca);
var idDodaneZadace = dodanaZadaca[0].idZadaca;
for(let i = 0; i < dodanaZadaca[0].brojZadataka; i++) {
db.Zadatak.findOrCreate({where :{
idZadaca : idDodaneZadace,
redniBrojZadatkaUZadaci : i,
maxBrojBodova : bodyReq.listaBodova[i],
brojOstvarenihBodova : 0,
profesorovKomentar : "",
datumPredaje : null,
statusZadatka : "neposlan",
sadrzajFile : null,
velicinaFile : null,
mimeTipUpdateZadatka : null
}}).then(function(dodaniZadatak){
console.log(dodaniZadatak);
var idDodanogZadatka = dodaniZadatak[0].idZadatak;
var mimeTipovi = [".pdf", ".zip", ".m", ".doc", ".txt"];
for(let j = 0; j < 5; j++) {
if(bodyReq.listaTipova[i][j] === true) {
db.MimeTip.findOrCreate({where: {
idZadatak : idDodanogZadatka,
mimeTip : mimeTipovi[j]
}})
}
}
})
}
});



res.end("test1");
});

app.listen(6001);
33 changes: 33 additions & 0 deletions node_modules/cors/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions node_modules/cors/HISTORY.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions node_modules/cors/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 440b6d7

Please sign in to comment.