Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minlength #67

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
8 changes: 6 additions & 2 deletions apps/admin-api/src/api/article/article.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ApiBody, ApiTags } from '@nestjs/swagger';
import { CurrentUser } from '@repo/api';
import { ApiAuth } from '@repo/api/decorators/http.decorators';
import { ArticleService } from './article.service';
import { ArticleFeedReqDto } from './dto/article-feed.dto';
import { ArticleListReqDto, ArticleListResDto } from './dto/article-list.dto';
import { ArticleResDto } from './dto/article.dto';
import { CreateArticleReqDto } from './dto/create-article.dto';
Expand All @@ -34,8 +35,11 @@ export class ArticleController {
}

@Get('feed')
async feed() {
return await this.articleService.feed();
async feed(
@CurrentUser('id') userId: number,
@Query() reqDto: ArticleFeedReqDto,
): Promise<ArticleListResDto> {
return await this.articleService.feed(userId, reqDto);
}

@Get(':slug')
Expand Down
33 changes: 31 additions & 2 deletions apps/admin-api/src/api/article/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { paginate } from '@repo/api/utils/offset-pagination';
import { ArticleEntity, TagEntity } from '@repo/database-typeorm';
import slugify from 'slugify';
import { In, Repository } from 'typeorm';
import { ArticleFeedReqDto } from './dto/article-feed.dto';
import { ArticleListReqDto, ArticleListResDto } from './dto/article-list.dto';
import { ArticleDto, ArticleResDto } from './dto/article.dto';
import { CreateArticleReqDto } from './dto/create-article.dto';
Expand Down Expand Up @@ -62,8 +63,35 @@ export class ArticleService {
};
}

async feed() {
throw new Error('Method not implemented.');
async feed(
userId: number,
reqDto: ArticleFeedReqDto,
): Promise<ArticleListResDto> {
const qb = this.articleRepository
.createQueryBuilder('article')
.leftJoinAndSelect('article.author', 'author');

qb.where('1 = 1');

qb.orderBy('article.createdAt', 'DESC');

const [articles, metaDto] = await paginate<ArticleEntity>(qb, reqDto, {
skipCount: false,
takeAll: false,
});

const articleDtos = articles.map((article) => {
const articleDto = article.toDto(ArticleDto);
delete articleDto.body;
articleDto.tagList = article.tags.map((tag) => tag.name);
return articleDto;
});

return {
articles: articleDtos,
articlesCount: metaDto.totalRecords,
pagination: metaDto,
};
}

async get(_slug: string) {
Expand Down Expand Up @@ -98,6 +126,7 @@ export class ArticleService {

return {
article: {
slug: savedArticle.slug,
title: savedArticle.title,
description: savedArticle.description,
body: savedArticle.body,
Expand Down
18 changes: 18 additions & 0 deletions apps/admin-api/src/api/article/dto/article-feed.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NumberFieldOptional } from '@repo/api';
import { PageOptionsDto } from '@repo/api/dto/offset-pagination/page-options.dto';

export class ArticleFeedReqDto extends PageOptionsDto {
@NumberFieldOptional({
minimum: 1,
default: 20,
int: true,
})
override readonly limit: number = 20;

@NumberFieldOptional({
minimum: 0,
default: 0,
int: true,
})
override readonly offset: number = 0;
}
6 changes: 3 additions & 3 deletions apps/admin-api/src/api/article/dto/article-list.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { PageOptionsDto } from '@repo/api/dto/offset-pagination/page-options.dto
import { ArticleDto } from './article.dto';

export class ArticleListReqDto extends PageOptionsDto {
@StringFieldOptional()
@StringFieldOptional({ minLength: 0 })
readonly tag?: string;

@StringFieldOptional()
@StringFieldOptional({ minLength: 0 })
readonly author?: string;

@StringFieldOptional()
@StringFieldOptional({ minLength: 0 })
readonly favorited?: string;

@NumberFieldOptional({
Expand Down
5 changes: 2 additions & 3 deletions apps/admin-api/src/api/article/dto/article.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {
DateFieldOptional,
NumberFieldOptional,
StringField,
StringFieldOptional,
} from '@repo/api';

export class ArticleDto {
@StringFieldOptional({ expose: true })
slug?: string;
@StringField({ expose: true })
slug: string;

@StringField({ expose: true })
title: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/admin-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function bootstrap() {
app.enableCors({
origin: corsOrigin,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Content-Type, Accept',
allowedHeaders: 'Content-Type, Accept, Authorization',
credentials: true,
});
logger.log(`CORS Origin: ${corsOrigin.toString()}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/decorators/field.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function StringField(
);
}

const minLength = options.minLength || 1;
const minLength = options.minLength ?? 1;

decorators.push(MinLength(minLength, { each: options.each }));

Expand Down