-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Roger13579/dev
Dev
- Loading branch information
Showing
16 changed files
with
1,172 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { IUser } from '../models/user'; | ||
import { | ||
MovieGenre, | ||
ProductSortBy, | ||
ProductType, | ||
RecommendWeightRange, | ||
TGetProductsReq, | ||
} from '../types/product.type'; | ||
import { AccountType } from '../types/user.type'; | ||
import { throwError } from '../utils/errorHandler'; | ||
import { CustomResponseType } from '../types/customResponseType'; | ||
import { | ||
checkDateOrder, | ||
parseBoolean, | ||
parseDate, | ||
parsePositiveInteger, | ||
parseValidEnums, | ||
} from '../utils/common'; | ||
|
||
export class ProductFilterDTO { | ||
public readonly title?: string; | ||
public readonly types?: ProductType[]; | ||
public readonly genres?: MovieGenre[]; | ||
public readonly vendors?: string[]; | ||
public readonly theaters?: string[]; | ||
public readonly isPublic?: boolean; | ||
public readonly isLaunched?: boolean; | ||
public readonly startAtFrom?: Date; | ||
public readonly startAtTo?: Date; | ||
public readonly recommendWeights?: number[]; | ||
public readonly sellStartAtFrom?: Date; | ||
public readonly sellStartAtTo?: Date; | ||
public readonly priceMax?: number; | ||
public readonly priceMin?: number; | ||
public readonly tags?: string[]; | ||
public readonly page?: number; | ||
public readonly limit?: number; | ||
public readonly sortBy?: string; | ||
public readonly accountType: AccountType = AccountType.member; | ||
|
||
get getFilter() { | ||
return this; | ||
} | ||
|
||
constructor(req: TGetProductsReq) { | ||
const { | ||
title, | ||
types, | ||
genres, | ||
vendors, | ||
theaters, | ||
isLaunched, | ||
isPublic, | ||
startAtFrom, | ||
startAtTo, | ||
sellStartAtFrom, | ||
recommendWeights, | ||
sellStartAtTo, | ||
priceMax, | ||
priceMin, | ||
tags, | ||
page, | ||
limit, | ||
sortBy, | ||
} = req.query; | ||
|
||
if ((req.user as IUser)?.accountType === AccountType.admin) { | ||
this.accountType = AccountType.admin; | ||
} | ||
|
||
// number | ||
this.limit = parsePositiveInteger('limit', limit); | ||
this.priceMax = parsePositiveInteger('priceMax', priceMax); | ||
this.priceMin = parsePositiveInteger('priceMin', priceMin); | ||
this.page = parsePositiveInteger('page', page); | ||
|
||
// string | ||
this.title = title; | ||
|
||
// validate | ||
this.types = parseValidEnums('type', ProductType, types); | ||
this.genres = parseValidEnums('genre', MovieGenre, genres); | ||
|
||
const validRecommendWeights: number[] = []; | ||
recommendWeights?.split(',').forEach((weight) => { | ||
const weightNum = parsePositiveInteger('recommendWeight', weight); | ||
if ( | ||
weightNum && | ||
Object.values(RecommendWeightRange).indexOf(weightNum) > -1 | ||
) { | ||
validRecommendWeights.push(weightNum); | ||
} | ||
}); | ||
|
||
if (validRecommendWeights.length > 0) { | ||
this.recommendWeights = validRecommendWeights; | ||
} | ||
|
||
if (sortBy) { | ||
const isValidSortBy = | ||
Object.keys(ProductSortBy).indexOf(sortBy.trim().replace('-', '')) >= 0; | ||
if (isValidSortBy) { | ||
this.sortBy = sortBy as ProductSortBy; | ||
} else { | ||
throwError( | ||
CustomResponseType.INVALID_PRODUCT_FILTER_MESSAGE + | ||
`: sortBy 不得為 ${sortBy}`, | ||
CustomResponseType.INVALID_PRODUCT_FILTER, | ||
); | ||
} | ||
} | ||
|
||
// array | ||
this.vendors = vendors?.split(','); | ||
this.theaters = theaters?.split(','); | ||
this.tags = tags?.split(','); | ||
|
||
// boolean | ||
this.isLaunched = parseBoolean('isLaunched', isLaunched); | ||
this.isPublic = parseBoolean('isPublic', isPublic); | ||
|
||
// time | ||
this.startAtTo = parseDate('startAtTo', startAtTo); | ||
this.startAtFrom = parseDate('startAtFrom', startAtFrom); | ||
this.sellStartAtFrom = parseDate('sellStartAtFrom', sellStartAtFrom); | ||
this.sellStartAtTo = parseDate('sellStartAtTo', sellStartAtTo); | ||
// 確認時間順序 | ||
checkDateOrder( | ||
{ prop: 'sellStartAtFrom', value: this.sellStartAtFrom }, | ||
{ prop: 'sellStartAtTo', value: this.sellStartAtTo }, | ||
{ prop: 'startAtFrom', value: this.startAtFrom }, | ||
{ prop: 'startAtTo', value: this.startAtTo }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,84 @@ | ||
import { NewProductDto } from '../dto/newProductDto'; | ||
import { ProductFilterDTO } from '../dto/productFilterDto'; | ||
import ProductModel, { IProduct } from '../models/product'; | ||
import { AccountType } from '../types/user.type'; | ||
|
||
export class ProductRepository { | ||
private createProductFilter(productFilterDto: ProductFilterDTO) { | ||
const { | ||
title, | ||
types, | ||
genres, | ||
vendors, | ||
theaters, | ||
isLaunched, | ||
isPublic, | ||
startAtFrom, | ||
startAtTo, | ||
sellStartAtFrom, | ||
recommendWeights, | ||
sellStartAtTo, | ||
priceMax, | ||
priceMin, | ||
tags, | ||
} = productFilterDto.getFilter; | ||
const titleRegex = title ? new RegExp(title) : undefined; | ||
return { | ||
...(titleRegex && { title: { $regex: titleRegex } }), | ||
...(types && { type: { $in: types } }), | ||
...(genres && { genre: { $in: genres } }), | ||
...(vendors && { vendor: { $in: vendors } }), | ||
...(theaters && { theater: { $in: theaters } }), | ||
...(recommendWeights && { recommendWeight: { $in: recommendWeights } }), | ||
...(isLaunched !== undefined && { isLaunched }), | ||
...(isPublic !== undefined && { isPublic }), | ||
...((startAtFrom || startAtTo) && { | ||
startAt: { | ||
...(startAtFrom && { $lte: startAtFrom }), | ||
...(startAtTo && { $gte: startAtTo }), | ||
}, | ||
}), | ||
...((sellStartAtFrom || sellStartAtTo) && { | ||
sellStartAt: { | ||
...(sellStartAtFrom && { $lte: sellStartAtFrom }), | ||
...(sellStartAtTo && { $gte: sellStartAtTo }), | ||
}, | ||
}), | ||
...((priceMax || priceMin) && { | ||
price: { | ||
...(priceMin && { $lte: priceMin }), | ||
...(priceMax && { $gte: priceMax }), | ||
}, | ||
}), | ||
...(tags && { tags: { $in: tags } }), | ||
}; | ||
} | ||
|
||
public async createProducts( | ||
newProductsDto: NewProductDto, | ||
): Promise<IProduct[] | void> { | ||
return ProductModel.insertMany(newProductsDto.getNewProducts); | ||
} | ||
|
||
public async findProducts(): Promise<IProduct[]> { | ||
return ProductModel.find({}); | ||
public async findProducts( | ||
productFilterDto: ProductFilterDTO, | ||
): Promise<IProduct[]> { | ||
const { page, limit, sortBy, accountType } = productFilterDto.getFilter; | ||
const filter = this.createProductFilter(productFilterDto); | ||
const selection = | ||
accountType === AccountType.admin ? '' : '-recommendWeight -isPublic'; | ||
const options = { | ||
...(page && limit && { skip: (page - 1) * limit }), | ||
...(limit && { limit }), | ||
sort: sortBy || '-createdAt', | ||
}; | ||
return ProductModel.find(filter, selection, options); | ||
} | ||
|
||
public async countProducts( | ||
productFilterDto: ProductFilterDTO, | ||
): Promise<number> { | ||
const filter = this.createProductFilter(productFilterDto); | ||
return ProductModel.countDocuments(filter); | ||
} | ||
} |
Oops, something went wrong.