Skip to content
This repository has been archived by the owner on Mar 29, 2019. It is now read-only.

单条记录排序 #118

Open
wants to merge 1 commit into
base: develop
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
12 changes: 6 additions & 6 deletions app/constants/codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ const ECodes = {
code: 'BULK_COPY_PARAMS_ERROR',
message: 'SourceColumnConfigs and sourceCellValues2dArray must be array.',
},
ORIGINAL_POSITIONS_MISSING: {
code: 'ORIGINAL_POSITIONS_MISSING',
message: 'OriginalPositions can not be empty.',
UNSUPPORTED_POSITION_TYPE: {
code: 'UNSUPPORTED_POSITION_TYPE',
message: 'Unsupported position type.',
},
ILLEGAL_TARGET_POSITION: {
code: 'ILLEGAL_TARGET_POSITION',
message: 'Illegal targetPosition.',
ILLEGAL_POSITION: {
code: 'ILLEGAL_POSITION',
message: 'Illegal position.',
},
USER_NOT_FOUND: {
code: 'USER_NOT_FOUND',
Expand Down
39 changes: 9 additions & 30 deletions app/controllers/positions.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
const posQueries = require('../queries/positions');
const { POSITION_TYPE } = require('../constants/app');
const { error, Status, ECodes } = require('../util/error');

module.exports = {
changePosition({ originalPositions, targetPosition, parentId, type }) {
targetPosition = parseInt(targetPosition);
originalPositions = Array.from(originalPositions, i => parseInt(i));
originalPositions.sort();
const originLen = originalPositions.length;
const minPosition = Math.min(originalPositions[0], targetPosition);
const maxPosition = Math.max(
originalPositions[originLen - 1],
targetPosition,
);
const temp = originalPositions.reduce((sum, originalPosition) => {
if (originalPosition < targetPosition) sum++;
return sum;
}, 0);
let movedNum = 0;
let sql = `update positions set position = case`;
for (let i = minPosition; i <= maxPosition; i++) {
if (originalPositions.indexOf(i) === -1) {
if (i >= targetPosition) {
sql += ` when position = ${i} then ${i + originLen - movedNum}`;
} else {
sql += ` when position = ${i} then ${i - movedNum}`;
}
} else {
sql += ` when position = ${i} then ${targetPosition - temp + movedNum}`;
movedNum++;
}
}
sql += ` else position end where "parentId" = ? and "type" = ?`;
return posQueries.query(sql, { replacements: [parentId, type] });
changePosition(params) {
if (
params.type === POSITION_TYPE.FIELD &&
(params.targetPosition === 1 || params.originalPosition === 1)
)
error(Status.Forbidden, ECodes.ILLEGAL_POSITION);
return posQueries.updateOne(params);
},

async create({ siblingId, parentId, type }) {
Expand Down
16 changes: 16 additions & 0 deletions app/queries/positions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
sequelize,
} = require('../models');
const { POSITION_TYPE } = require('../constants/app');
const { error, Status, ECodes } = require('../util/error');

const POSITION_MODELS = {
[POSITION_TYPE.BASE]: BasePositions,
Expand Down Expand Up @@ -42,4 +43,19 @@ module.exports = {
const PosModel = POSITION_MODELS[type];
return PosModel.bulkCreate(records);
},

updateOne(params) {
const tableName =
params.type.slice(0, 1).toUpperCase() +
params.type.slice(1) +
'Positions';
const sql = `update "${tableName}" set "position" = case when "position"=${
params.originalPosition
} then ${params.targetPosition} when "position"=${
params.targetPosition
} then ${params.originalPosition} else "position" end where "parentId"='${
params.parentId
}'`;
return sequelize.query(sql);
},
};
16 changes: 8 additions & 8 deletions app/resolvers/positions.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const { checkKeyExists } = require('../util/helper');
const posController = require('../controllers/positions');
const socketIo = require('../../lib/socketIo');
const { error, Status, ECodes } = require('../util/error');
const { sequelize } = require('../models/index');
const { POSITION_TYPE } = require('../constants/app');

const changePosition = async ctx => {
const params = ctx.request.body;
checkKeyExists(
params,
'originalPositions',
'originalPosition',
'targetPosition',
'parentId',
'type',
);
if (params.originalPositions.length === 0)
error(null, ECodes.ORIGINAL_POSITIONS_MISSING);
if (!(params.targetPosition > 1)) error(null, ECodes.ILLEGAL_TARGET_POSITION);
if (!Array.isArray(params.originalPositions))
params.originalPositions = [params.originalPositions];
await posController.changePosition(params);
params.originalPosition = parseInt(params.originalPosition);
params.targetPosition = parseInt(params.targetPosition);
if (!(params.originalPosition > 0) || !(params.targetPosition > 0))
error(Status.Forbidden, ECodes.ILLEGAL_POSITION);
await sequelize.transaction(() => posController.changePosition(params));
ctx.body = { message: 'success' };
socketIo.sync({
op: 'changePosition',
Expand Down
4 changes: 2 additions & 2 deletions app/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ router.delete('/api/array-field-item', fldValResolver.deleteArrayValue);
// TODO fix bulk copy
// router.post('/api/bulk-copy-field-value', resolveBulkCopyFieldValue);

// //Position
// router.put('/api/change-position', posResolver.changePosition);
//Position
router.put('/api/change-position', posResolver.changePosition);

//FieldTypes
router.get('/api/fieldTypes', fldTypesResolver.getAll);
Expand Down