-
Notifications
You must be signed in to change notification settings - Fork 0
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
폴더 목록 및 기본폴더 생성 #40
폴더 목록 및 기본폴더 생성 #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
import { FunctionType } from '../types/type'; | ||
|
||
export * from './parser.util'; | ||
export * from './math.util'; | ||
|
||
export const pipe = (...functions: FunctionType[]) => { | ||
return (initial: unknown) => | ||
functions.reduce((result, func) => { | ||
return func(result); | ||
}, initial); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const sum = <T = number | object>( | ||
_list: T[], | ||
selector?: T extends object ? (item: T) => number : never, | ||
): number => { | ||
if (_list.length === 0) return 0; | ||
|
||
const list = (selector ? _list.map(selector) : _list) as number[]; | ||
return list.reduce((sum, cur) => cur + sum, 0); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { FolderType } from '@src/infrastructure/database/types/folder-type.enum'; | ||
|
||
export class FolderDomain { | ||
userId: string; | ||
|
||
name: string; | ||
|
||
type: FolderType; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export enum FolderType { | ||
CUSTOM = 'custom', | ||
DEFAULT = 'default', | ||
|
||
/** 실제 DB에 들어가지는 않고 클라이언트에 내려주기 위해 있는 값 타입 */ | ||
ALL = 'all', | ||
FAVORITE = 'favorite', | ||
} | ||
Comment on lines
3
to
8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그럼 DEFAULT 인 폴더는 "나중에 읽을 링크" 만 있는건강 궁금쓰 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { FolderDomain } from '@src/domains/folder'; | ||
import { FolderDocument } from '@src/infrastructure'; | ||
|
||
export interface FolderWithCount extends FolderDocument { | ||
postCount: number; | ||
export interface FolderListServiceDto { | ||
/** */ | ||
defaultFolders: any[]; | ||
customFolders: any[]; | ||
} | ||
Comment on lines
+4
to
8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 파일 이름은 안바뀌나욤? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 바꿈 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateFolderDto, UpdateFolderDto } from './dto/mutate-folder.dto'; | ||
import { HydratedDocument, Schema as MongooseSchema } from 'mongoose'; | ||
import { FolderWithCount } from './dto/folder-with-count.dto'; | ||
import { Schema as MongooseSchema } from 'mongoose'; | ||
import { FolderRepository } from './folders.repository'; | ||
import { PostsRepository } from '../posts/posts.repository'; | ||
import { FolderType } from '@src/infrastructure/database/types/folder-type.enum'; | ||
import { Type } from 'class-transformer'; | ||
import { sum } from '@src/common'; | ||
import { FolderListServiceDto } from './dto/folder-with-count.dto'; | ||
import { Folder, FolderDocument } from '@src/infrastructure'; | ||
|
||
@Injectable() | ||
export class FoldersService { | ||
|
@@ -24,21 +25,51 @@ export class FoldersService { | |
await this.folderRepository.createMany(folders); | ||
} | ||
|
||
async findAll(userId: string): Promise<FolderWithCount[]> { | ||
async findAll(userId: string): Promise<FolderListServiceDto> { | ||
const folders = await this.folderRepository.findByUserId(userId); | ||
const folderIds = folders.map((folder) => folder._id); | ||
|
||
const posts = await this.postRepository.getPostCountByFolderIds(folderIds); | ||
const groupedFolders = | ||
await this.postRepository.getPostCountByFolderIds(folderIds); | ||
|
||
const foldersWithCounts = folders.map((folder) => { | ||
const post = posts.find((post) => post._id.equals(folder._id)); | ||
return { | ||
...folder.toJSON(), | ||
postCount: post?.count ?? 0, | ||
} satisfies FolderWithCount; | ||
}); | ||
const allPostCount = sum(groupedFolders, (folder) => folder.count); | ||
const favoritePostCount = | ||
await this.postRepository.findFavoritePostCount(userId); | ||
|
||
const defaultFolder = folders.find( | ||
(folder) => folder.type === FolderType.DEFAULT, | ||
); | ||
const customFolders = folders | ||
.filter((folder) => folder.type === FolderType.CUSTOM) | ||
.map((folder) => { | ||
const post = groupedFolders.find((folder) => | ||
folder._id.equals(folder._id), | ||
); | ||
return { | ||
...folder.toJSON(), | ||
postCount: post?.count ?? 0, | ||
}; | ||
}); | ||
|
||
const all = { | ||
id: null, | ||
name: '모든 링크', | ||
type: FolderType.ALL, | ||
userId: new MongooseSchema.Types.ObjectId(userId), | ||
postCount: allPostCount, | ||
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MongooseSchema 별칭 쓴 이유 궁금쓰 |
||
}; | ||
const favorite = { | ||
id: null, | ||
name: '즐겨찾기', | ||
type: FolderType.FAVORITE, | ||
userId: new MongooseSchema.Types.ObjectId(userId), | ||
postCount: favoritePostCount, | ||
}; | ||
|
||
return foldersWithCounts; | ||
const defaultFolders = [all, favorite, defaultFolder].filter( | ||
(folder) => !!folder, | ||
); | ||
return { defaultFolders, customFolders }; | ||
} | ||
|
||
async findOne(userId: string, folderId: string) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ export class FolderSummaryResponse { | |
createdAt: Date; | ||
|
||
constructor(data: FolderDocument) { | ||
this.id = data._id.toString(); | ||
this.id = data._id ? data._id.toString() : data.id; | ||
Comment on lines
18
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _id가 없으면 data.id 주는거 맞징 |
||
this.name = data.name; | ||
this.type = data.type; | ||
this.createdAt = data.createdAt; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
도메인으로 따로 빼는 기준 궁금쓰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
없앰 ㅎㅎ