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

Remove listeners #3

Open
wants to merge 3 commits into
base: complete-app
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
20 changes: 2 additions & 18 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AjaxStore, Grid, StringHelper } from './grid.module.js';
const store = new AjaxStore({
createUrl : '/create',
readUrl : '/read',
updateUrl : '/update/',
deleteUrl : '/delete/',
updateUrl : '/update',
deleteUrl : '/delete',
autoLoad : true,
autoCommit : true,

Expand All @@ -14,22 +14,6 @@ const store = new AjaxStore({
create : 'POST',
update : 'PATCH',
delete : 'DELETE'
},
listeners : {
beforeRequest : (event) => {
if (event.action === 'create') {
const newItem = event.body.data[0];
delete newItem.id;
event.body = newItem;
}
if (event.action === 'update') {
const updatedItem = event.body.data[0];
const itemId = updatedItem.id;
delete updatedItem.id;
event.body = updatedItem;
store.updateUrl = `/update/${itemId}/`;
}
}
}
});

Expand Down
50 changes: 27 additions & 23 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,51 +33,55 @@ app.get('/read', async(req, res) => {

app.post('/create', async(req, res) => {
try {
const newPlayer = await Player.create(req.body);

res.send({ success : true, data : [newPlayer] });
const playersData = req.body.data.map(player => {
const { id, ...fields } = player;
return fields;
});
const newPlayers = await Player.bulkCreate(playersData);
res.send({ success : true, data : newPlayers });
}
catch (error) {
console.error(error);
res.send({
success : false,
message : 'Players could not be loaded'
message : 'Players could not be created.'
});
}
});

app.patch('/update/:itemId/', async(req, res) => {
app.patch('/update', async(req, res) => {
try {
const data = req.body;
const itemId = req.params.itemId;
// Perform the update and fetch in a single transaction
const result = await sequelize.transaction(async(t) => {
// Update the player
const [updated] = await Player.update(data, {
where : { id : itemId },
transaction : t
});
const playersData = req.body.data;
const updatedPlayers = [];

if (updated) {
// Fetch the updated player
const updatedPlayer = await Player.findOne({
await sequelize.transaction(async(t) => {
for (const data of playersData) {
const itemId = data.id;
const [updated] = await Player.update(data, {
where : { id : itemId },
transaction : t
});
return { updatedPlayer };
}
else {
throw new Error('Player not found');

if (updated) {
const updatedPlayer = await Player.findOne({
where : { id : itemId },
transaction : t
});
updatedPlayers.push(updatedPlayer);
}
else {
throw new Error(`Player with id ${itemId} could not be found.`);
}
}
});

res.send({ success : true, data : [result.updatedPlayer] });
res.send({ success : true, data : updatedPlayers });
}
catch (error) {
console.error(error);
res.send({
success : false,
message : 'Player could not be updated'
message : 'Error updating player records.'
});
}
});
Expand Down