Skip to content

Commit

Permalink
Merge pull request #42 from TeamCatchMe/fix/character/#41
Browse files Browse the repository at this point in the history
Fix/character/#41
  • Loading branch information
realwhyjay authored Apr 22, 2023
2 parents 0a0d615 + 3f955f2 commit 36827cd
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 68 deletions.
44 changes: 44 additions & 0 deletions src/modules/v1/activity/activity.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { DateUtil } from '@common/libraries/date.util';
import { Injectable } from '@nestjs/common';
import { Activity } from '@prisma/client';
import dayjs from 'dayjs';
import { PrismaService } from 'src/modules/prisma/prisma.service';
import { ActivityDataForLookingDTO } from '../character/dto/characters-get-looking.res.dto';
import { ActivityDto } from './dto/activity.dto';
import { ActivityRepositoryInterface } from './interfaces/activity-repository.interface';

Expand Down Expand Up @@ -61,6 +63,48 @@ export class ActivityRepository implements ActivityRepositoryInterface {
});
}

async findAllForLookingList(
date: Date,
id: number,
limit: number,
): Promise<ActivityDataForLookingDTO[]> {
const activities = await this.prisma.activity.findMany({
select: {
id: true,
content: true,
image: true,
character_id: true,
date: true,
created_at: true,
},
where: {
created_at: {
lte: date,
},
id: { lte: id },
is_delete: false,
Character: { is_public: true },
},
orderBy: { created_at: 'desc' },
take: limit,
});

const result = activities.map((activity) => {
const date = DateUtil.toString(activity.date);
const createdAt = DateUtil.toString(activity.created_at);
const formattedActivity: ActivityDataForLookingDTO = {
id: activity.id,
content: activity.content,
image: activity.image,
character_id: activity.character_id,
date,
createdAt,
};
return formattedActivity;
});
return result;
}

async create(
userId: number,
characterId: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseRepositoryInterface from '@common/interfaces/base-repository.interface';
import { ActivityDataForLookingDTO } from '@modules/v1/character/dto/characters-get-looking.res.dto';
import { ActivityDto } from '../dto/activity.dto';

export const ACTIVITY_REPOSITORY = 'ACTIVITY REPOSITORY';
Expand All @@ -11,6 +12,11 @@ export interface ActivityRepositoryInterface
startDate: Date,
endDate: Date,
): Promise<ActivityDto[]>;
findAllForLookingList(
date: Date,
id: number,
limit: number,
): Promise<ActivityDataForLookingDTO[]>;
findAllByCharacterId(characterId: number): Promise<ActivityDto[]>;
create(
userId: number,
Expand Down
9 changes: 6 additions & 3 deletions src/modules/v1/character/character.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,16 @@ export class CharacterController {
type: InternalServerError,
})
@Get(routesV1.character.looking)
@ApiQuery({ name: 'offset', required: false })
@ApiQuery({ name: 'activityId', required: false })
@ApiQuery({ name: 'date', required: false })
async getCharacterLookingList(
@Token() user: UserDTO,
@Query('offset') offset: number,
@Query('activityId') activityId: number,
@Query('date') date: string,
): Promise<ResponseEntity<CharactersGetLookingResponseDTO[]>> {
const character = await this.characterService.getCharactersForLookingList(
offset,
date,
activityId,
);
return ResponseEntity.OK_WITH_DATA(
rm.READ_CHARACTERS_FROM_LOOKING_SUCCESS,
Expand Down
72 changes: 21 additions & 51 deletions src/modules/v1/character/character.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import dayjs from 'dayjs';
import { PrismaService } from 'src/modules/prisma/prisma.service';
import { CharacterGetDetailResponseDTO } from './dto/character-get-detail.res.dto';
import { CharacterGetFromMainResponseDTO } from './dto/character-get-from-main.res.dto';
import { CharactersGetLookingResponseDTO } from './dto/characters-get-looking.res.dto';
import {
CharactersGetLookingResponseDTO,
FindAllCharactersForLookingDTO,
} from './dto/characters-get-looking.res.dto';
import { CharactersResponseDTO } from './dto/characters.res.dto';
import { CharacterRepositoryInterface } from './interfaces/character-repository.interface';

Expand All @@ -26,6 +29,7 @@ export default class CharacterRepository
return await this.prisma.character.findMany({
where: {
id: { in: ids },
is_delete: false,
},
});
}
Expand All @@ -34,6 +38,7 @@ export default class CharacterRepository
return await this.prisma.character.findMany({
where: {
user_id: userId,
is_delete: false,
},
});
}
Expand All @@ -47,7 +52,7 @@ export default class CharacterRepository
name: string,
): Promise<Character> {
return await this.prisma.character.findFirst({
where: { user_id: userId, name },
where: { user_id: userId, name, is_delete: false },
});
}

Expand All @@ -57,7 +62,7 @@ export default class CharacterRepository
name: string,
): Promise<Character> {
return await this.prisma.character.findFirst({
where: { user_id: userId, name, id: { not: id } },
where: { user_id: userId, name, id: { not: id }, is_delete: false },
});
}

Expand Down Expand Up @@ -99,6 +104,7 @@ export default class CharacterRepository
const characters = await this.prisma.character.findMany({
where: {
user_id,
is_delete: false,
},
include: {
Activity: {
Expand Down Expand Up @@ -158,7 +164,7 @@ export default class CharacterRepository
_count: 'desc',
},
},
where: { user_id: userId },
where: { user_id: userId, is_delete: false },
});

const characters: CharactersResponseDTO[] = [];
Expand Down Expand Up @@ -188,7 +194,7 @@ export default class CharacterRepository
take: 1,
},
},
where: { user_id: userId },
where: { user_id: userId, is_delete: false },
});

const charactersOrderedByRecent = charactersWithActivities.sort(
Expand Down Expand Up @@ -218,7 +224,7 @@ export default class CharacterRepository
orderBy: {
created_at: 'asc',
},
where: { user_id: userId },
where: { user_id: userId, is_delete: false },
});
return characters;
}
Expand All @@ -238,6 +244,7 @@ export default class CharacterRepository
},
where: {
id: characterId,
is_delete: false,
},
});

Expand Down Expand Up @@ -276,62 +283,25 @@ export default class CharacterRepository
}

async getCharactersForLookingList(
offset: number,
limit: number,
): Promise<CharactersGetLookingResponseDTO[]> {
characterIds: number[],
): Promise<FindAllCharactersForLookingDTO[]> {
const characters = await this.prisma.character.findMany({
skip: offset,
take: limit,
select: {
id: true,
name: true,
type: true,
level: true,
User: { select: { id: true, nickname: true } },
Activity: {
select: {
id: true,
content: true,
image: true,
date: true,
},
orderBy: {
date: 'desc',
},
take: 1,
},
where: {
id: {
in: characterIds,
},
is_delete: false,
},
});

const sortedCharacters = characters.sort((a, b) => {
const aRecentActivity = a.Activity[0];
const bRecentActivity = b.Activity[0];

if (!aRecentActivity || !bRecentActivity) {
return 0;
}

const aDate = new Date(aRecentActivity.date);
const bDate = new Date(bRecentActivity.date);

return bDate.getTime() - aDate.getTime();
});

const result = sortedCharacters
.filter((character) => character.Activity.length > 0)
.map((character) => {
const formattedActivity = {
...character.Activity[0],
date: dayjs(character.Activity[0].date).format('YYYYMMDDHHmmss'),
};

return {
...character,
Activity: formattedActivity,
};
});

return result;
return characters;
}

async delete(characterId: number): Promise<void> {
Expand Down
57 changes: 49 additions & 8 deletions src/modules/v1/character/character.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BLOCK_REPOSITORY,
} from '../block/interface/block-repository.interface';
import { DailyCharacterDataForCalenderResponseDTO } from './dto/character-get-calender.res.dto';
import { CharactersGetLookingResponseDTO } from './dto/characters-get-looking.res.dto';
import { CharactersResponseDTO } from './dto/characters.res.dto';
import {
CharacterRepositoryInterface,
Expand Down Expand Up @@ -140,16 +141,56 @@ export class CharacterService {
return character;
}

async getCharactersForLookingList(offset: number) {
let limit = 10;
if (!offset) {
offset = 0;
limit = 100;
async getCharactersForLookingList(date: string, activityId: number) {
const limit = 10;
let offsetDate = new Date();
let offsetId = 99999999;

if (date) {
offsetDate = dayjs(date).toDate();
console.info(offsetDate);
}
if (activityId) {
offsetId = activityId;
}
const character =
await this.characterRepository.getCharactersForLookingList(offset, limit);

return character;
const activities = await this.activityRepository.findAllForLookingList(
offsetDate,
offsetId,
limit,
);
const characterIds = activities.map((activity) => activity.character_id);

const characters =
await this.characterRepository.getCharactersForLookingList(characterIds);

const result: CharactersGetLookingResponseDTO[] = activities
.map(({ character_id, ...activity }) => {
const character = characters.find((c) => c.id === character_id);
if (character) {
return {
id: character.id,
name: character.name,
type: character.type,
level: character.level,
User: {
id: character.User.id,
nickname: character.User.nickname,
},
Activity: {
id: activity.id,
content: activity.content,
image: activity.image,
date: activity.date,
createdAt: activity.createdAt,
},
};
}
return null;
})
.filter((item) => item !== null);

return result;
}

async deleteCharacter(userId: number, characterId: number): Promise<void> {
Expand Down
38 changes: 36 additions & 2 deletions src/modules/v1/character/dto/characters-get-looking.res.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,49 @@ class UserDataForLookingResponseDTO extends PickType(UserDTO, [
'nickname',
]) {}

export class ActivityDataForLookingDTO extends PickType(ActivityDto, [
'id',
'content',
'image',
'character_id',
]) {
@ApiProperty({
description: '활동의 날짜(YYYYMMDDHHmmss)',
})
date: string;

@ApiProperty({
description: '활동의 생성 날짜(YYYYMMDDHHmmss)',
})
createdAt: string;
}
class ActivityDataForLookingResponseDTO extends PickType(ActivityDto, [
'id',
'content',
'image',
]) {
@ApiProperty({
description: '활동의 날짜(YYYYMMDDHHmmss',
description: '활동의 날짜(YYYYMMDDHHmmss)',
})
date: string;

@ApiProperty({
description: '활동의 생성 날짜(YYYYMMDDHHmmss)',
})
createdAt: string;
}

export class FindAllCharactersForLookingDTO extends PickType(CharacterDTO, [
'id',
'name',
'type',
'level',
]) {
@ApiProperty({
description: '캐츄의 유져 정보',
type: UserDataForLookingResponseDTO,
})
User: UserDataForLookingResponseDTO;
}

export class CharactersGetLookingResponseDTO extends PickType(CharacterDTO, [
Expand All @@ -33,7 +67,7 @@ export class CharactersGetLookingResponseDTO extends PickType(CharacterDTO, [

@ApiProperty({
description: '캐츄의 가장 최근 활동 정보',
type: [ActivityDataForLookingResponseDTO],
type: ActivityDataForLookingResponseDTO,
})
Activity: ActivityDataForLookingResponseDTO;
}
Loading

0 comments on commit 36827cd

Please sign in to comment.