Skip to content

Commit

Permalink
refactor: 나도 뭐 한지 몰라용... 이것저것들 수정했어요 (#82)
Browse files Browse the repository at this point in the history
* fix(db): mongodb의 username 외 2개의 필드를 optional로 수정

* refactor: 몰라요... 이것저것

* feat: 검색 추가

* fix: 테스트 코드 버그 수정
  • Loading branch information
son-daehyeon committed Sep 17, 2024
1 parent 0ff5531 commit 23ce8e2
Show file tree
Hide file tree
Showing 37 changed files with 465 additions and 152 deletions.
12 changes: 6 additions & 6 deletions src/common/config/mongo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class MongoConfig implements MongooseOptionsFactory {
createMongooseOptions(): MongooseModuleOptions {
const host = this.configService.getOrThrow<string>('mongo.host');
const port = this.configService.getOrThrow<number>('mongo.port');
const username = this.configService.getOrThrow<string>('mongo.username');
const password = this.configService.getOrThrow<string>('mongo.password');
const authSource = this.configService.getOrThrow<string>('mongo.authSource');
const username = this.configService.get<string>('mongo.username');
const password = this.configService.get<string>('mongo.password');
const authSource = this.configService.get<string>('mongo.authSource');
const database = this.configService.getOrThrow<string>('mongo.database');

return {
Expand All @@ -22,9 +22,9 @@ export class MongoConfig implements MongooseOptionsFactory {
private buildConnectionString(
host: string,
port: number,
username: string,
password: string,
authSource: string,
username: string | undefined,
password: string | undefined,
authSource: string | undefined,
database: string,
): string {
return 'mongodb://${credential}${host}/${database}${otherOptions}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ export class PurgeUnusedImageJob {

private async job() {
const usedImages = [
...(await this.socialRepository.findAll())
.flatMap((member) => member.contents)
.map((content) => content.image)
.map((image) => this.activityService.extractKeyFromUrl(image)),
...(await this.projectRepository.findAll())
.map((project) => project.content)
.flatMap((content) => this.toImagesFromHtml(content))
.map((image) => this.activityService.extractKeyFromUrl(image)),
...(await this.projectRepository.findAll())
.map((project) => project.image)
.map((image) => this.activityService.extractKeyFromUrl(image)),
...(await this.socialRepository.findAll())
.flatMap((social) => social.contents)
.map((content) => content.image)
.map((image) => this.activityService.extractKeyFromUrl(image)),
];

const savedImages = await this.activityService.getKeys();
Expand Down
17 changes: 14 additions & 3 deletions src/domain/activity/project/controller/project.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';

import {
Expand All @@ -7,6 +7,7 @@ import {
GetProjectsPageResponseDto,
GetProjectsRequestDto,
GetProjectsResponseDto,
SearchProjectsRequestDto,
} from '@wink/activity/dto';
import { ProjectNotFoundException } from '@wink/activity/exception';
import { ProjectService } from '@wink/activity/service';
Expand All @@ -23,7 +24,7 @@ export class ProjectController {
@ApiProperty({ type: GetProjectRequestDto })
@ApiCustomResponse(GetProjectResponseDto)
@ApiCustomErrorResponse([ProjectNotFoundException])
async getProject(@Body() request: GetProjectRequestDto): Promise<GetProjectResponseDto> {
async getProject(@Query() request: GetProjectRequestDto): Promise<GetProjectResponseDto> {
return this.projectService.getProject(request);
}

Expand All @@ -34,11 +35,21 @@ export class ProjectController {
return this.projectService.getProjectsPage();
}

@Get('/search')
@ApiOperation({ summary: '프로젝트 검색' })
@ApiProperty({ type: SearchProjectsRequestDto })
@ApiCustomResponse(GetProjectsResponseDto)
async searchProjects(
@Query() request: SearchProjectsRequestDto,
): Promise<GetProjectsResponseDto> {
return this.projectService.searchProjects(request);
}

@Get()
@ApiOperation({ summary: '프로젝트 목록' })
@ApiProperty({ type: GetProjectsRequestDto })
@ApiCustomResponse(GetProjectsResponseDto)
async getProjects(@Body() request: GetProjectsRequestDto): Promise<GetProjectsResponseDto> {
async getProjects(@Query() request: GetProjectsRequestDto): Promise<GetProjectsResponseDto> {
return this.projectService.getProjects(request);
}
}
1 change: 1 addition & 0 deletions src/domain/activity/project/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './request/create-project.request.dto';
export * from './request/update-project.request.dto';
export * from './request/get-project.request.dto';
export * from './request/get-projects.request.dto';
export * from './request/search-projects.request.dto';
export * from './request/delete-project.request.dto';

// Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { ApiProperty } from '@nestjs/swagger';

import { CommonValidation, TypeValidation } from '@wink/validation';

import { Type } from 'class-transformer';

export class GetProjectsRequestDto {
@ApiProperty({
description: '페이지',
example: 1,
})
@Type(() => Number)
@CommonValidation.IsNotEmpty()
@TypeValidation.IsNumber()
page!: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';

import { CommonValidation, TypeValidation } from '@wink/validation';

export class SearchProjectsRequestDto {
@ApiProperty({
description: '검색어',
example: '김',
})
@CommonValidation.IsNotEmpty()
@TypeValidation.IsString()
query!: string;
}
15 changes: 13 additions & 2 deletions src/domain/activity/project/repository/project.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class ProjectRepository {
}

// Read
async count(): Promise<number> {
return this.projectModel.countDocuments().exec();
}

async findAll(): Promise<Project[]> {
return this.projectModel.find().sort({ createdAt: -1 }).exec();
}
Expand All @@ -23,15 +27,22 @@ export class ProjectRepository {
return this.projectModel
.find()
.sort({ createdAt: -1 })
.skip((page - 1) * 10)
.limit(10)
.skip((page - 1) * 15)
.limit(15)
.exec();
}

async findById(id: string): Promise<Project | null> {
return this.projectModel.findById(id).exec();
}

async findAllByContainsTitle(title: string): Promise<Project[]> {
return this.projectModel
.find({ title: { $regex: title, $options: 'i' } })
.sort({ createdAt: -1 })
.exec();
}

// Delete
async deleteById(id: string): Promise<void> {
await this.projectModel.deleteOne({ _id: id }).exec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ProjectAdminService {
member: Member,
{ title, content, tags, image }: CreateProjectRequestDto,
): Promise<CreateProjectResponseDto> {
if (!(await this.projectRepository.existsByTitle(title))) {
if (await this.projectRepository.existsByTitle(title)) {
throw new AlreadyExistsProjectException();
}

Expand Down
12 changes: 9 additions & 3 deletions src/domain/activity/project/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GetProjectsPageResponseDto,
GetProjectsRequestDto,
GetProjectsResponseDto,
SearchProjectsRequestDto,
} from '@wink/activity/dto';
import { ProjectNotFoundException } from '@wink/activity/exception';
import { ProjectRepository } from '@wink/activity/repository';
Expand All @@ -25,10 +26,15 @@ export class ProjectService {
}

async getProjectsPage(): Promise<GetProjectsPageResponseDto> {
const projects = await this.projectRepository.findAll();
const page = Math.ceil(projects.length / 10);
const count = await this.projectRepository.count();

return { page };
return { page: Math.ceil(count / 15) };
}

async searchProjects({ query }: SearchProjectsRequestDto): Promise<GetProjectsResponseDto> {
const projects = await this.projectRepository.findAllByContainsTitle(query);

return { projects };
}

async getProjects({ page }: GetProjectsRequestDto): Promise<GetProjectsResponseDto> {
Expand Down
26 changes: 22 additions & 4 deletions src/domain/activity/social/controller/social.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Body, Controller, Get } from '@nestjs/common';
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';

import {
GetSocialRequestDto,
GetSocialResponseDto,
GetSocialsPageResponseDto,
GetSocialsRequestDto,
GetSocialsResponseDto,
SearchSocialsRequestDto,
} from '@wink/activity/dto';
import { SocialNotFoundException } from '@wink/activity/exception';
import { SocialService } from '@wink/activity/service';
Expand All @@ -21,14 +24,29 @@ export class SocialController {
@ApiProperty({ type: GetSocialRequestDto })
@ApiCustomResponse(GetSocialResponseDto)
@ApiCustomErrorResponse([SocialNotFoundException])
async getProject(@Body() request: GetSocialRequestDto): Promise<GetSocialResponseDto> {
async getSocial(@Query() request: GetSocialRequestDto): Promise<GetSocialResponseDto> {
return this.socialService.getSocial(request);
}

@Get('/max')
@ApiOperation({ summary: '친목 활동 최대 페이지' })
@ApiCustomResponse(GetSocialsPageResponseDto)
async getSocialsPage(): Promise<GetSocialsPageResponseDto> {
return this.socialService.getSocialsPage();
}

@Get('/search')
@ApiOperation({ summary: '친목 활동 검색' })
@ApiProperty({ type: SearchSocialsRequestDto })
@ApiCustomResponse(GetSocialsResponseDto)
async searchProjects(@Query() request: SearchSocialsRequestDto): Promise<GetSocialsResponseDto> {
return this.socialService.searchSocials(request);
}

@Get()
@ApiOperation({ summary: '친목 활동 목록' })
@ApiCustomResponse(GetSocialsResponseDto)
async getSocials(): Promise<GetSocialsResponseDto> {
return this.socialService.getSocials();
async getSocials(@Query() request: GetSocialsRequestDto): Promise<GetSocialsResponseDto> {
return this.socialService.getSocials(request);
}
}
3 changes: 3 additions & 0 deletions src/domain/activity/social/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
export * from './request/create-social.request.dto';
export * from './request/delete-social.request.dto';
export * from './request/get-social.request.dto';
export * from './request/get-socials.request.dto';
export * from './request/search-socials.request.dto';
export * from './request/update-social.request.dto';

// Response
export * from './response/create-social.response.dto';
export * from './response/get-social.response.dto';
export * from './response/get-socials.response.dto';
export * from './response/get-socials-page.response.dto';
16 changes: 16 additions & 0 deletions src/domain/activity/social/dto/request/get-socials.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApiProperty } from '@nestjs/swagger';

import { CommonValidation, TypeValidation } from '@wink/validation';

import { Type } from 'class-transformer';

export class GetSocialsRequestDto {
@ApiProperty({
description: '페이지',
example: 1,
})
@Type(() => Number)
@CommonValidation.IsNotEmpty()
@TypeValidation.IsNumber()
page!: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ApiProperty } from '@nestjs/swagger';

import { CommonValidation, TypeValidation } from '@wink/validation';

export class SearchSocialsRequestDto {
@ApiProperty({
description: '검색어',
example: '김',
})
@CommonValidation.IsNotEmpty()
@TypeValidation.IsString()
query!: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';

export class GetSocialsPageResponseDto {
@ApiProperty({
description: '최대 페이지',
example: 1,
})
page!: number;
}
22 changes: 21 additions & 1 deletion src/domain/activity/social/repository/social.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,28 @@ export class SocialRepository {
}

// Read
async count(): Promise<number> {
return this.socialModel.countDocuments().exec();
}

async findAll(): Promise<Social[]> {
return this.socialModel.find().sort({ createdAt: -1 }).limit(6).exec();
return this.socialModel.find().sort({ createdAt: -1 }).exec();
}

async findAllPage(page: number): Promise<Social[]> {
return this.socialModel
.find()
.sort({ createdAt: -1 })
.skip((page - 1) * 10)
.limit(10)
.exec();
}

async findAllByContainsTitle(title: string): Promise<Social[]> {
return this.socialModel
.find({ title: { $regex: title, $options: 'i' } })
.sort({ createdAt: -1 })
.exec();
}

async findById(id: string): Promise<Social | null> {
Expand Down
2 changes: 1 addition & 1 deletion src/domain/activity/social/service/social.admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SocialAdminService {
member: Member,
{ title, contents }: CreateSocialRequestDto,
): Promise<CreateSocialResponseDto> {
if (!(await this.socialRepository.existsByTitle(title))) {
if (await this.socialRepository.existsByTitle(title)) {
throw new AlreadyExistsSocialException();
}

Expand Down
19 changes: 17 additions & 2 deletions src/domain/activity/social/service/social.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { Injectable } from '@nestjs/common';
import {
GetSocialRequestDto,
GetSocialResponseDto,
GetSocialsPageResponseDto,
GetSocialsRequestDto,
GetSocialsResponseDto,
SearchSocialsRequestDto,
} from '@wink/activity/dto';
import { SocialNotFoundException } from '@wink/activity/exception';
import { SocialRepository } from '@wink/activity/repository';
Expand All @@ -22,9 +25,21 @@ export class SocialService {
return { social };
}

async getSocials(): Promise<GetSocialsResponseDto> {
const socials = await this.socialRepository.findAll();
async getSocials({ page }: GetSocialsRequestDto): Promise<GetSocialsResponseDto> {
const socials = await this.socialRepository.findAllPage(page);

return { socials };
}

async searchSocials({ query }: SearchSocialsRequestDto): Promise<GetSocialsResponseDto> {
const socials = await this.socialRepository.findAllByContainsTitle(query);

return { socials };
}

async getSocialsPage(): Promise<GetSocialsPageResponseDto> {
const count = await this.socialRepository.count();

return { page: Math.ceil(count / 10) };
}
}
Loading

0 comments on commit 23ce8e2

Please sign in to comment.