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

[#23] 모집글 생성 api 추가 #34

Open
wants to merge 1 commit into
base: feature/22-region-district-feed-entity
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
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TypeOrmModule } from "@nestjs/typeorm";
import { EmailOptions } from "./common/config/email-config";
import { AccessTokenGuard } from "./common/guards/bearer-token.guard";
import { TypeOrmModuleOptions } from "./common/typeorm";
import { ArticlesModule } from "./modules/articles.module";
import { AuthModule } from "./modules/auth.module";
import { MembersModule } from "./modules/members.module";

Expand All @@ -20,6 +21,7 @@ import { MembersModule } from "./modules/members.module";
MembersModule,
AuthModule,
MailerModule.forRootAsync(EmailOptions),
ArticlesModule,
],
controllers: [],
providers: [
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/articles.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Body, Controller, Post } from "@nestjs/common";

import { CurrentMemberDecorator } from "@APP/common/decorators/current-member.decorator";
import { CreateArticleDto } from "@APP/dtos/create-article.dto";
import { ArticlesService } from "@APP/services/articles.service";

@Controller("articles")
export class ArticlesController {
constructor(private readonly articlesService: ArticlesService) {}

@Post()
async postArticle(
@Body() body: CreateArticleDto,
@CurrentMemberDecorator("id") currentMemberId: number,
) {
return this.articlesService.createArticle(currentMemberId, body);
}
}
13 changes: 13 additions & 0 deletions src/dtos/create-article.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { PickType } from "@nestjs/swagger";

import { ArticleEntity } from "@APP/entities/article.entity";

export class CreateArticleDto extends PickType(ArticleEntity, [
"title",
"content",
"startTime",
"endTime",
"categoryId",
"districtId",
"regionId",
]) {}
20 changes: 20 additions & 0 deletions src/entities/article.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Type } from "class-transformer";
import { IsDate, IsNotEmpty, IsNumber, IsString } from "class-validator";
import {
Column,
CreateDateColumn,
Expand All @@ -20,39 +22,57 @@ export class ArticleEntity {
@PrimaryGeneratedColumn({ type: "int" })
id!: number;

@IsNotEmpty()
@IsString()
@Column({ type: "varchar", length: 100 })
title!: string;

@IsNotEmpty()
@IsString()
@Column({ type: "text" })
content!: string;

@IsNotEmpty()
@Type(() => Date)
@IsDate()
@Column({ type: "datetime" })
startTime!: Date;

@IsNotEmpty()
@Type(() => Date)
@IsDate()
@Column({ type: "datetime" })
endTime!: Date;

@IsNotEmpty()
@IsNumber()
@Column({ type: "int" })
memberId!: number;

@ManyToOne(() => MemberEntity, (member) => member.articles)
@JoinColumn({ name: "memberId", referencedColumnName: "id" })
member!: MemberEntity;

@IsNotEmpty()
@IsNumber()
@Column({ type: "int" })
categoryId!: number;

@ManyToOne(() => CategoryEntity, (category) => category.articles)
@JoinColumn({ name: "categoryId", referencedColumnName: "id" })
category!: CategoryEntity;

@IsNotEmpty()
@IsNumber()
@Column({ type: "int" })
regionId!: number;

@ManyToOne(() => RegionEntity, (region) => region.articles)
@JoinColumn({ name: "regionId", referencedColumnName: "id" })
region!: RegionEntity;

@IsNotEmpty()
@IsNumber()
@Column({ type: "int" })
districtId!: number;

Expand Down
36 changes: 36 additions & 0 deletions src/modules/articles.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";

import { ArticlesController } from "@APP/controllers/articles.controller";
import { ArticleLikeEntity } from "@APP/entities/article-like.entity";
import { ArticleEntity } from "@APP/entities/article.entity";
import { CategoryEntity } from "@APP/entities/category.entity";
import { DistrictEntity } from "@APP/entities/district.entity";
import { RegionEntity } from "@APP/entities/region.entity";
import { ArticlesRepository } from "@APP/repositories/articles.repository";
import { CategoriesRepository } from "@APP/repositories/categories.repository";
import { DistrictsRepository } from "@APP/repositories/districts.repository";
import { RegionsRepository } from "@APP/repositories/regions.repository";
import { ArticlesService } from "@APP/services/articles.service";

@Module({
imports: [
TypeOrmModule.forFeature([
ArticleEntity,
ArticleLikeEntity,
CategoryEntity,
DistrictEntity,
RegionEntity,
]),
],
controllers: [ArticlesController],
providers: [
ArticlesService,
ArticlesRepository,
CategoriesRepository,
DistrictsRepository,
RegionsRepository,
],
exports: [],
})
export class ArticlesModule {}
59 changes: 59 additions & 0 deletions src/services/articles.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Injectable, NotFoundException } from "@nestjs/common";

import { CreateArticleDto } from "@APP/dtos/create-article.dto";
import { ArticlesRepository } from "@APP/repositories/articles.repository";
import { CategoriesRepository } from "@APP/repositories/categories.repository";
import { DistrictsRepository } from "@APP/repositories/districts.repository";
import { RegionsRepository } from "@APP/repositories/regions.repository";

@Injectable()
export class ArticlesService {
constructor(
private readonly articlesRepository: ArticlesRepository,
private readonly categoriesRepository: CategoriesRepository,
private readonly districtsRepository: DistrictsRepository,
private readonly regionsRepository: RegionsRepository,
) {}

async createArticle(currentMemberId: number, body: CreateArticleDto) {
const [category, district, region] = await Promise.all([
this.categoriesRepository.exists({
where: {
id: body.categoryId,
},
}),
this.districtsRepository.exists({
where: {
id: body.districtId,
},
}),
this.regionsRepository.exists({
where: {
id: body.regionId,
},
}),
]);

if (!category) {
throw new NotFoundException("카테고리를 찾을 수 없습니다.");
}

if (!district || !region) {
throw new NotFoundException("지역을 찾을 수 없습니다.");
}

return this.articlesRepository.save(
this.createArticleEntity(currentMemberId, body),
);
}

private createArticleEntity(
currentMemberId: number,
body: CreateArticleDto,
) {
return this.articlesRepository.create({
memberId: currentMemberId,
...body,
});
}
}