minor in sorting collection

This commit is contained in:
lesion 2024-08-21 10:46:23 +02:00
parent d773db650f
commit be638ee40a
No known key found for this signature in database
GPG key ID: 352918250B012177
2 changed files with 5 additions and 3 deletions

View file

@ -221,14 +221,14 @@ const collectionController = {
const collectionDetail = { const collectionDetail = {
name: req.body.name, name: req.body.name,
isActor: true, isActor: true,
isTop: true isTop: true,
sortIndex: Sequelize.literal(`(SELECT MAX(${Col('collections.sortIndex')})+1 from collections)`)
} }
// TODO: validation // TODO: validation
log.info(`Create collection: ${req.body.name}`) log.info(`Create collection: ${req.body.name}`)
try { try {
const collection = await Collection.create(collectionDetail) const collection = await Collection.create(collectionDetail)
await collection.update({ sortIndex: collection.id })
res.json(collection) res.json(collection)
} catch (e) { } catch (e) {
log.error(`Create collection failed ${e}`) log.error(`Create collection failed ${e}`)

View file

@ -1,4 +1,5 @@
'use strict'; 'use strict';
const Collection = require('../api/models/collection')
/** @type {import('sequelize-cli').Migration} */ /** @type {import('sequelize-cli').Migration} */
module.exports = { module.exports = {
@ -6,7 +7,8 @@ module.exports = {
const transaction = await queryInterface.sequelize.transaction() const transaction = await queryInterface.sequelize.transaction()
try { try {
await queryInterface.addColumn('collections', 'sortIndex', { type: Sequelize.INTEGER }) await queryInterface.addColumn('collections', 'sortIndex', { type: Sequelize.INTEGER })
await queryInterface.sequelize.query('UPDATE collections set sortIndex=id') const collection = Collection(queryInterface.sequelize, Sequelize.DataTypes)
await collection.update({ sortIndex: Sequelize.col('id')}, { where: {}, transaction })
return transaction.commit() return transaction.commit()
} catch (e) { } catch (e) {
if (transaction) { if (transaction) {