Skip to content

Commit 3ddd3db

Browse files
committed
Fixes velopert#67
1 parent 6a9a194 commit 3ddd3db

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

velog-backend/src/database/rawQuery/series.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,25 @@ export const getSeriesPostCountList = async (
2626
throw e;
2727
}
2828
};
29+
30+
export const subtractIndexes = async (
31+
seriesId: string,
32+
from: number,
33+
): Promise<any> => {
34+
const query = `update series_posts
35+
set index = index - 1
36+
where fk_series_id = $seriesId
37+
and index > $from`;
38+
try {
39+
const result = await db.query(query, {
40+
type: Sequelize.QueryTypes.BULKUPDATE,
41+
bind: {
42+
seriesId,
43+
from,
44+
},
45+
});
46+
return result;
47+
} catch (e) {
48+
throw e;
49+
}
50+
};

velog-backend/src/router/posts/post/post.ctrl.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from 'database/models';
2323
import redisClient from 'lib/redisClient';
2424
import UrlSlugHistory from 'database/models/UrlSlugHistory';
25+
import { subtractIndexes } from 'database/rawQuery/series';
2526

2627
export const checkPostExistancy = async (
2728
ctx: Context,
@@ -224,15 +225,16 @@ export const updatePost = async (ctx: Context): Promise<*> => {
224225
if (seriesPost) {
225226
if (seriesPost.series.id !== series_id) {
226227
seriesPost.destroy();
228+
subtractIndexes(seriesPost.series.id, seriesPost.index);
227229
if (!series_id) {
228230
series = null;
229231
} else {
230232
await SeriesPosts.append(series_id, id, ctx.user.id);
231233
}
232234
}
233-
} /* else {
235+
} else if (series_id) {
234236
await SeriesPosts.append(series_id, id, ctx.user.id);
235-
} */
237+
}
236238
} catch (e) {
237239
ctx.throw(500, e);
238240
}
@@ -347,6 +349,15 @@ export const deletePost = async (ctx: Context): Promise<*> => {
347349
db.getQueryInterface().bulkDelete('posts_tags', { fk_post_id: post.id }),
348350
db.getQueryInterface().bulkDelete('post_likes', { fk_post_id: post.id }),
349351
]);
352+
const seriesPost = await SeriesPosts.findOne({
353+
where: {
354+
fk_post_id: post.id,
355+
},
356+
});
357+
if (seriesPost) {
358+
subtractIndexes(seriesPost.fk_series_id, seriesPost.index);
359+
seriesPost.destroy();
360+
}
350361
await post.destroy();
351362
redisClient.remove(`/@${ctx.user.username}/${ctx.post.url_slug}`);
352363
ctx.status = 204;

velog-frontend/src/components/post/PostSeriesInfo/PostSeriesInfo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ class PostSeriesInfo extends Component<Props, State> {
7474
{index}/{length}
7575
</div>
7676
</div>
77-
<h2>{this.props.series.name}</h2>
77+
<h2>
78+
<Link to={`/@${username}/series/${series.url_slug}`}>{this.props.series.name}</Link>
79+
</h2>
7880
{open && (
7981
<ol>
8082
{list.map(item => (

0 commit comments

Comments
 (0)