Skip to content

Commit

Permalink
chore: rename ID to Id
Browse files Browse the repository at this point in the history
  • Loading branch information
surmon-china committed May 20, 2024
1 parent 4d57901 commit 274b504
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 202 deletions.
6 changes: 3 additions & 3 deletions src/interfaces/mongoose.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type MongooseModel<T> = Model<MongooseDoc<T>> & PaginateModel<MongooseDoc
// `The id is cast based on the Schema before sending the command.`
// https://mongoosejs.com/docs/api.html#model_Model.findByIdAndDelete
// `id «Object|Number|String» value of _id to query by.`
export type MongooseID = Types.ObjectId | string
export type MongooseObjectID = Types.ObjectId
export type MongooseId = Types.ObjectId | string
export type MongooseObjectId = Types.ObjectId

export type WithID<T> = T & { _id: Types.ObjectId }
export type WithId<T> = T & { _id: Types.ObjectId }
18 changes: 9 additions & 9 deletions src/modules/announcement/announcement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { Injectable } from '@nestjs/common'
import { InjectModel } from '@app/transformers/model.transformer'
import { MongooseModel, MongooseDoc, MongooseID } from '@app/interfaces/mongoose.interface'
import { MongooseModel, MongooseDoc, MongooseId } from '@app/interfaces/mongoose.interface'
import { PaginateResult, PaginateOptions, PaginateQuery } from '@app/utils/paginate'
import { Announcement } from './announcement.model'

Expand All @@ -25,23 +25,23 @@ export class AnnouncementService {
return this.announcementModel.create(announcement)
}

public update(announcementID: MongooseID, announcement: Announcement): Promise<MongooseDoc<Announcement>> {
public update(announcementId: MongooseId, announcement: Announcement): Promise<MongooseDoc<Announcement>> {
return this.announcementModel
.findByIdAndUpdate(announcementID, announcement, { new: true })
.findByIdAndUpdate(announcementId, announcement, { new: true })
.exec()
.then((result) => result || Promise.reject(`Announcement '${announcementID}' not found`))
.then((result) => result || Promise.reject(`Announcement '${announcementId}' not found`))
}

public delete(announcementID: MongooseID) {
public delete(announcementId: MongooseId) {
return this.announcementModel
.findByIdAndDelete(announcementID, null)
.findByIdAndDelete(announcementId, null)
.exec()
.then((result) => {
return result ?? Promise.reject(`Announcement '${announcementID}' not found`)
return result ?? Promise.reject(`Announcement '${announcementId}' not found`)
})
}

public batchDelete(announcementIDs: MongooseID[]) {
return this.announcementModel.deleteMany({ _id: { $in: announcementIDs } }).exec()
public batchDelete(announcementIds: MongooseId[]) {
return this.announcementModel.deleteMany({ _id: { $in: announcementIds } }).exec()
}
}
16 changes: 8 additions & 8 deletions src/modules/article/article.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SortType } from '@app/constants/biz.constant'
import { TagService } from '@app/modules/tag/tag.service'
import { CategoryService } from '@app/modules/category/category.service'
import { PaginateResult, PaginateQuery, PaginateOptions } from '@app/utils/paginate'
import { ArticlePaginateQueryDTO, ArticleCalendarQueryDTO, ArticleIDsDTO, ArticlesStateDTO } from './article.dto'
import { ArticlePaginateQueryDTO, ArticleCalendarQueryDTO, ArticleIdsDTO, ArticlesStateDTO } from './article.dto'
import { ARTICLE_HOTTEST_SORT_PARAMS } from './article.model'
import { ArticleService } from './article.service'
import { Article } from './article.model'
Expand Down Expand Up @@ -114,12 +114,12 @@ export class ArticleController {
@Get(':id/context')
@Responser.handle('Get context articles')
async getArticleContext(@QueryParams() { params }: QueryParamsResult) {
const articleID = Number(params.id)
const articleId = Number(params.id)
const [prevArticles, nextArticles, relatedArticles] = await Promise.all([
this.articleService.getNearArticles(articleID, 'early', 1),
this.articleService.getNearArticles(articleID, 'later', 1),
this.articleService.getNearArticles(articleId, 'early', 1),
this.articleService.getNearArticles(articleId, 'later', 1),
this.articleService
.getDetailByNumberIDOrSlug({ idOrSlug: articleID, publicOnly: true })
.getDetailByNumberIdOrSlug({ idOrSlug: articleId, publicOnly: true })
.then((article) => this.articleService.getRelatedArticles(article, 20))
])
return {
Expand All @@ -143,8 +143,8 @@ export class ArticleController {
}
// admin user > Object ID | number ID
return Types.ObjectId.isValid(params.id)
? this.articleService.getDetailByObjectID(params.id)
: this.articleService.getDetailByNumberIDOrSlug({ idOrSlug: Number(params.id) })
? this.articleService.getDetailByObjectId(params.id)
: this.articleService.getDetailByNumberIdOrSlug({ idOrSlug: Number(params.id) })
}

@Post()
Expand Down Expand Up @@ -178,7 +178,7 @@ export class ArticleController {
@Delete()
@UseGuards(AdminOnlyGuard)
@Responser.handle('Delete articles')
delArticles(@Body() body: ArticleIDsDTO) {
delArticles(@Body() body: ArticleIdsDTO) {
return this.articleService.batchDelete(body.article_ids)
}
}
4 changes: 2 additions & 2 deletions src/modules/article/article.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ export class ArticleCalendarQueryDTO {
timezone?: string
}

export class ArticleIDsDTO {
export class ArticleIdsDTO {
@ArrayNotEmpty()
@ArrayUnique()
@IsArray()
article_ids: string[]
}

export class ArticlesStateDTO extends ArticleIDsDTO {
export class ArticlesStateDTO extends ArticleIdsDTO {
@IsIn(ARTICLE_PUBLISH_STATES)
@IsInt()
@IsDefined()
Expand Down
58 changes: 29 additions & 29 deletions src/modules/article/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { CategoryService } from '@app/modules/category/category.service'
import { TagService } from '@app/modules/tag/tag.service'
import { PublishState } from '@app/constants/biz.constant'
import { NULL } from '@app/constants/value.constant'
import { MongooseModel, MongooseDoc, MongooseID } from '@app/interfaces/mongoose.interface'
import { MongooseModel, MongooseDoc, MongooseId } from '@app/interfaces/mongoose.interface'
import { PaginateResult, PaginateQuery, PaginateOptions } from '@app/utils/paginate'
import {
Article,
Expand All @@ -38,15 +38,15 @@ export class ArticleService {
) {}

// get near articles
public async getNearArticles(articleID: number, type: 'later' | 'early', count: number): Promise<Article[]> {
public async getNearArticles(articleId: number, type: 'later' | 'early', count: number): Promise<Article[]> {
const typeFieldMap = {
early: { field: '$lt', sort: -1 as SortOrder },
later: { field: '$gt', sort: 1 as SortOrder }
}
const targetType = typeFieldMap[type]
return this.articleModel
.find(
{ ...ARTICLE_LIST_QUERY_GUEST_FILTER, id: { [targetType.field]: articleID } },
{ ...ARTICLE_LIST_QUERY_GUEST_FILTER, id: { [targetType.field]: articleId } },
ARTICLE_LIST_QUERY_PROJECTION
)
.populate(ARTICLE_FULL_QUERY_REF_POPULATE)
Expand Down Expand Up @@ -80,20 +80,20 @@ export class ArticleService {
}

// get articles by ids
public getList(articleIDs: number[]): Promise<Array<Article>> {
return this.articleModel.find({ id: { $in: articleIDs } }).exec()
public getList(articleIds: number[]): Promise<Array<Article>> {
return this.articleModel.find({ id: { $in: articleIds } }).exec()
}

// get article by ObjectID
public getDetailByObjectID(articleID: MongooseID): Promise<MongooseDoc<Article>> {
public getDetailByObjectId(articleId: MongooseId): Promise<MongooseDoc<Article>> {
return this.articleModel
.findById(articleID)
.findById(articleId)
.exec()
.then((result) => result || Promise.reject(`Article '${articleID}' not found`))
.then((result) => result || Promise.reject(`Article '${articleId}' not found`))
}

// get article by number id
public getDetailByNumberIDOrSlug({
public getDetailByNumberIdOrSlug({
idOrSlug,
publicOnly = false,
populate = false
Expand All @@ -118,7 +118,7 @@ export class ArticleService {

// get article detail for guest user
public async getFullDetailForGuest(target: number | string): Promise<Article> {
const article = await this.getDetailByNumberIDOrSlug({
const article = await this.getDetailByNumberIdOrSlug({
idOrSlug: target,
publicOnly: true,
populate: true
Expand All @@ -134,9 +134,9 @@ export class ArticleService {
return article.toObject()
}

public async incrementLikes(articleID: number) {
const article = await this.getDetailByNumberIDOrSlug({
idOrSlug: articleID,
public async incrementLikes(articleId: number) {
const article = await this.getDetailByNumberIdOrSlug({
idOrSlug: articleId,
publicOnly: true
})
article.meta.likes++
Expand All @@ -160,10 +160,10 @@ export class ArticleService {
return article
}

public async update(articleID: MongooseID, newArticle: Article): Promise<MongooseDoc<Article>> {
public async update(articleId: MongooseId, newArticle: Article): Promise<MongooseDoc<Article>> {
if (newArticle.slug) {
const existedArticle = await this.articleModel.findOne({ slug: newArticle.slug }).exec()
if (existedArticle && !existedArticle._id.equals(articleID)) {
if (existedArticle && !existedArticle._id.equals(articleId)) {
throw `Article slug '${newArticle.slug}' is existed`
}
}
Expand All @@ -172,9 +172,9 @@ export class ArticleService {
Reflect.deleteProperty(newArticle, 'created_at')
Reflect.deleteProperty(newArticle, 'updated_at')

const article = await this.articleModel.findByIdAndUpdate(articleID, newArticle, { new: true }).exec()
const article = await this.articleModel.findByIdAndUpdate(articleId, newArticle, { new: true }).exec()
if (!article) {
throw `Article '${articleID}' not found`
throw `Article '${articleId}' not found`
}
this.seoService.update(getArticleUrl(article.id))
this.tagService.updateAllTagsCache()
Expand All @@ -183,10 +183,10 @@ export class ArticleService {
return article
}

public async delete(articleID: MongooseID) {
const article = await this.articleModel.findByIdAndDelete(articleID, null).exec()
public async delete(articleId: MongooseId) {
const article = await this.articleModel.findByIdAndDelete(articleId, null).exec()
if (!article) {
throw `Article '${articleID}' not found`
throw `Article '${articleId}' not found`
}

this.seoService.delete(getArticleUrl(article.id))
Expand All @@ -196,21 +196,21 @@ export class ArticleService {
return article
}

public async batchPatchState(articleIDs: MongooseID[], state: PublishState) {
public async batchPatchState(articleIds: MongooseId[], state: PublishState) {
const actionResult = await this.articleModel
.updateMany({ _id: { $in: articleIDs } }, { $set: { state } }, { multi: true })
.updateMany({ _id: { $in: articleIds } }, { $set: { state } }, { multi: true })
.exec()
this.tagService.updateAllTagsCache()
this.categoryService.updateAllCategoriesCache()
this.archiveService.updateCache()
return actionResult
}

public async batchDelete(articleIDs: MongooseID[]) {
const articles = await this.articleModel.find({ _id: { $in: articleIDs } }).exec()
public async batchDelete(articleIds: MongooseId[]) {
const articles = await this.articleModel.find({ _id: { $in: articleIds } }).exec()
this.seoService.delete(articles.map((article) => getArticleUrl(article.id)))

const actionResult = await this.articleModel.deleteMany({ _id: { $in: articleIDs } }).exec()
const actionResult = await this.articleModel.deleteMany({ _id: { $in: articleIds } }).exec()
this.tagService.updateAllTagsCache()
this.categoryService.updateAllCategoriesCache()
this.archiveService.updateCache()
Expand Down Expand Up @@ -259,14 +259,14 @@ export class ArticleService {
}

// article commentable state
public async isCommentableArticle(articleID: number): Promise<boolean> {
const article = await this.articleModel.findOne({ id: articleID }).exec()
public async isCommentableArticle(articleId: number): Promise<boolean> {
const article = await this.articleModel.findOne({ id: articleId }).exec()
return Boolean(article && !article.disabled_comments)
}

// update article comments count
public async updateMetaComments(articleID: number, commentCount: number) {
const findParams = { id: articleID }
public async updateMetaComments(articleId: number, commentCount: number) {
const findParams = { id: articleId }
const patchParams = { $set: { 'meta.comments': commentCount } }
return this.articleModel.updateOne(findParams, patchParams, { timestamps: false }).exec()
}
Expand Down
38 changes: 19 additions & 19 deletions src/modules/category/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@app/transformers/model.transformer'
import { getCategoryUrl } from '@app/transformers/urlmap.transformer'
import { MongooseModel, MongooseDoc, MongooseID, MongooseObjectID, WithID } from '@app/interfaces/mongoose.interface'
import { MongooseModel, MongooseDoc, MongooseId, MongooseObjectId, WithId } from '@app/interfaces/mongoose.interface'
import { PaginateResult, PaginateQuery, PaginateOptions } from '@app/utils/paginate'
import { CacheService, CacheManualResult } from '@app/processors/cache/cache.service'
import { ArchiveService } from '@app/modules/archive/archive.service'
Expand Down Expand Up @@ -42,8 +42,8 @@ export class CategoryService {
})
}

private async aggregateArticleCount(publicOnly: boolean, categories: Array<WithID<Category>>) {
const counts = await this.articleModel.aggregate<{ _id: MongooseObjectID; count: number }>([
private async aggregateArticleCount(publicOnly: boolean, categories: Array<WithId<Category>>) {
const counts = await this.articleModel.aggregate<{ _id: MongooseObjectId; count: number }>([
{ $match: publicOnly ? ARTICLE_LIST_QUERY_GUEST_FILTER : {} },
{ $unwind: '$categories' },
{ $group: { _id: '$categories', count: { $sum: 1 } } }
Expand Down Expand Up @@ -100,17 +100,17 @@ export class CategoryService {
}

// get categories genealogy
public getGenealogyById(categoryID: MongooseID): Promise<Category[]> {
public getGenealogyById(categoryId: MongooseId): Promise<Category[]> {
const categories: Category[] = []
const findById = (id: MongooseID) => this.categoryModel.findById(id).exec()
const findById = (id: MongooseId) => this.categoryModel.findById(id).exec()

return new Promise((resolve, reject) => {
;(function findCateItem(id) {
findById(id)
.then((category) => {
if (!category) {
if (id === categoryID) {
return reject(`Category '${categoryID}' not found`)
if (id === categoryId) {
return reject(`Category '${categoryId}' not found`)
} else {
return resolve(categories)
}
Expand All @@ -121,20 +121,20 @@ export class CategoryService {
return hasParent ? findCateItem(parentId) : resolve(categories)
})
.catch(reject)
})(categoryID)
})(categoryId)
})
}

// update category
public async update(categoryID: MongooseID, newCategory: Category): Promise<MongooseDoc<Category>> {
public async update(categoryId: MongooseId, newCategory: Category): Promise<MongooseDoc<Category>> {
const existedCategory = await this.categoryModel.findOne({ slug: newCategory.slug }).exec()
if (existedCategory && !existedCategory._id.equals(categoryID)) {
if (existedCategory && !existedCategory._id.equals(categoryId)) {
throw `Category slug '${newCategory.slug}' is existed`
}

const category = await this.categoryModel.findByIdAndUpdate(categoryID, newCategory, { new: true }).exec()
const category = await this.categoryModel.findByIdAndUpdate(categoryId, newCategory, { new: true }).exec()
if (!category) {
throw `Category '${categoryID}' not found`
throw `Category '${categoryId}' not found`
}
this.seoService.push(getCategoryUrl(category.slug))
this.archiveService.updateCache()
Expand All @@ -143,18 +143,18 @@ export class CategoryService {
}

// delete category
public async delete(categoryID: MongooseID) {
const category = await this.categoryModel.findByIdAndDelete(categoryID, null).exec()
public async delete(categoryId: MongooseId) {
const category = await this.categoryModel.findByIdAndDelete(categoryId, null).exec()
if (!category) {
throw `Category '${categoryID}' not found`
throw `Category '${categoryId}' not found`
}

// cache
this.archiveService.updateCache()
this.seoService.delete(getCategoryUrl(category.slug))
this.updateAllCategoriesCache()
// children categories
const categories = await this.categoryModel.find({ pid: categoryID }).exec()
const categories = await this.categoryModel.find({ pid: categoryId }).exec()
// delete when root category -> { pid: target.id }
if (!categories.length) {
return category
Expand All @@ -168,12 +168,12 @@ export class CategoryService {
return category
}

public async batchDelete(categoryIDs: MongooseID[]) {
public async batchDelete(categoryIds: MongooseId[]) {
// SEO remove
const categories = await this.categoryModel.find({ _id: { $in: categoryIDs } }).exec()
const categories = await this.categoryModel.find({ _id: { $in: categoryIds } }).exec()
this.seoService.delete(categories.map((category) => getCategoryUrl(category.slug)))
// DB remove
const actionResult = await this.categoryModel.deleteMany({ _id: { $in: categoryIDs } }).exec()
const actionResult = await this.categoryModel.deleteMany({ _id: { $in: categoryIds } }).exec()
this.archiveService.updateCache()
this.updateAllCategoriesCache()
return actionResult
Expand Down
2 changes: 1 addition & 1 deletion src/modules/comment/comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class CommentController {
@UseGuards(AdminOnlyGuard)
@Responser.handle({ message: 'Get comment detail', error: HttpStatus.NOT_FOUND })
getComment(@QueryParams() { params }: QueryParamsResult): Promise<Comment> {
return this.commentService.getDetailByObjectID(params.id).then((comment) => {
return this.commentService.getDetailByObjectId(params.id).then((comment) => {
return comment ? comment : Promise.reject('Comment not found')
})
}
Expand Down
Loading

0 comments on commit 274b504

Please sign in to comment.