Skip to content

Commit

Permalink
Feat/category (#46)
Browse files Browse the repository at this point in the history
* Feat: Add categoryIconMapping entity

* Feat: Add categoryIconCodeMapping logic

- 장소의 카테고리를 기준으로 노출할 icon을 매핑하는 로직 추가
- category_icon_mapping 테이블에 데이터를 저장해 이용함
- 매번 테이블을 조회하기에는 불필요한 데이터로 생각됨. 추후 인메모리캐시 혹은 redis 등 캐싱을 고려해야 할듯

* Fix: Edit category_icon_code type

- 숫자로 저장되도록 수정
  • Loading branch information
sally0226 authored Jul 31, 2024
1 parent 4f1ca83 commit 01ddd67
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const IS_PROD = process.env.NODE_ENV === 'production';
export const SEARCH_KEYWORD_MAX_LENGTH = 10;
export const INVITE_LINK_EXPIRATION_DAYS = 7;
export const INVITE_LINK_PREVIEW_LENGTH = 6;
export const DEFAULT_CATEGORY_ICON_CODE = 100;
28 changes: 28 additions & 0 deletions src/entities/category-icon-mapping.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Entity,
EntityRepositoryType,
PrimaryKey,
Property,
} from '@mikro-orm/core';

import { CategoryIconMappingRepository } from 'src/entities/category-icon-mapping.repository';

@Entity({ repository: () => CategoryIconMappingRepository })
export class CategoryIconMapping {
@PrimaryKey({ autoincrement: true })
id: number;

@Property()
kakaoCategory: string;

@Property()
categoryGroup: string;

@Property()
iconCode: number;

@Property({ defaultRaw: 'CURRENT_TIMESTAMP' })
createdAt: Date = new Date();

[EntityRepositoryType]: CategoryIconMappingRepository;
}
4 changes: 4 additions & 0 deletions src/entities/category-icon-mapping.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ExtendedEntityRepository } from 'src/common/helper/extended-repository.helper';
import { CategoryIconMapping } from 'src/entities/category-icon-mapping.entity';

export class CategoryIconMappingRepository extends ExtendedEntityRepository<CategoryIconMapping> {}
5 changes: 5 additions & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CategoryIconMapping } from 'src/entities/category-icon-mapping.entity';
import { Tag } from 'src/entities/tag.entity';

import { GroupMap } from './group-map.entity';
Expand Down Expand Up @@ -36,6 +37,9 @@ export * from './tag.repository';
export * from './tag-icon.entity';
export * from './tag-icon.repository';

export * from './category-icon-mapping.entity';
export * from './category-icon-mapping.repository';

export const entities = [
User,
GroupMap,
Expand All @@ -46,4 +50,5 @@ export const entities = [
InviteLink,
TagIcon,
Tag,
CategoryIconMapping,
];
8 changes: 7 additions & 1 deletion src/entities/kakao-place.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ export class KakaoPlace {

@Property({
type: 'string',
comment: '카카오맵 basicInfo.category.cate1name',
comment: '카카오맵 basicInfo.category.catename',
})
category: string;

@Property({
comment: '카카오맵 basicInfo.category.catename을 기준으로 매핑한 icon_code',
nullable: true,
})
categoryIconCode: number;

@Property({
type: 'string',
comment: '카카오맵 basicInfo.address.newaddr.newaddrfull',
Expand Down
89 changes: 88 additions & 1 deletion src/migrations/.snapshot-korrk-stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,83 @@
"namespaces": ["public"],
"name": "public",
"tables": [
{
"columns": {
"id": {
"name": "id",
"type": "serial",
"unsigned": true,
"autoincrement": true,
"primary": true,
"nullable": false,
"mappedType": "integer"
},
"kakao_category": {
"name": "kakao_category",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "string"
},
"category_group": {
"name": "category_group",
"type": "varchar(255)",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "string"
},
"icon_code": {
"name": "icon_code",
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "CURRENT_TIMESTAMP",
"mappedType": "datetime"
}
},
"name": "category_icon_mapping",
"schema": "public",
"indexes": [
{
"keyName": "category_icon_mapping_pkey",
"columnNames": ["id"],
"composite": false,
"constraint": true,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {},
"nativeEnums": {
"user_provider": {
"name": "user_provider",
"schema": "public",
"items": ["KAKAO"]
},
"user_role": {
"name": "user_role",
"schema": "public",
"items": ["USER", "ADMIN"]
}
}
},
{
"columns": {
"id": {
Expand Down Expand Up @@ -31,9 +108,19 @@
"autoincrement": false,
"primary": false,
"nullable": false,
"comment": "카카오맵 basicInfo.category.cate1name",
"comment": "카카오맵 basicInfo.category.catename",
"mappedType": "string"
},
"category_icon_code": {
"name": "category_icon_code",
"type": "int",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"comment": "카카오맵 basicInfo.category.catename을 기준으로 매핑한 icon_code",
"mappedType": "integer"
},
"address": {
"name": "address",
"type": "varchar(255)",
Expand Down
13 changes: 13 additions & 0 deletions src/migrations/Migration20240727070747.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240727070747 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table "category_icon_mapping" ("id" serial primary key, "kakao_category" varchar(255) not null, "category_group" varchar(255) not null, "icon_code" int not null, "created_at" timestamptz not null);',
);
}

async down(): Promise<void> {
this.addSql('drop table if exists "category_icon_mapping" cascade;');
}
}
21 changes: 21 additions & 0 deletions src/migrations/Migration20240727071134.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240727071134 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table "category_icon_mapping" alter column "created_at" type timestamptz using ("created_at"::timestamptz);',
);
this.addSql(
'alter table "category_icon_mapping" alter column "created_at" set default CURRENT_TIMESTAMP;',
);
}

async down(): Promise<void> {
this.addSql(
'alter table "category_icon_mapping" alter column "created_at" drop default;',
);
this.addSql(
'alter table "category_icon_mapping" alter column "created_at" type timestamptz using ("created_at"::timestamptz);',
);
}
}
16 changes: 16 additions & 0 deletions src/migrations/Migration20240729140952.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240729140952 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table "kakao_place" add column "category_icon_code" varchar(255) null;',
);
this.addSql(
'comment on column "kakao_place"."category_icon_code" is \'카카오맵 basicInfo.category.cate1name을 기준으로 매핑한 icon_code\';',
);
}

async down(): Promise<void> {
this.addSql('alter table "kakao_place" drop column "category_icon_code";');
}
}
21 changes: 21 additions & 0 deletions src/migrations/Migration20240729152526.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240729152526 extends Migration {
async up(): Promise<void> {
this.addSql(
'alter table "kakao_place" alter column "category_icon_code" type int using ("category_icon_code"::int);',
);
this.addSql(
'comment on column "kakao_place"."category" is \'카카오맵 basicInfo.category.catename\';',
);
}

async down(): Promise<void> {
this.addSql(
'alter table "kakao_place" alter column "category_icon_code" type varchar(255) using ("category_icon_code"::varchar(255));',
);
this.addSql(
'comment on column "kakao_place"."category" is \'카카오맵 basicInfo.category.cate1name\';',
);
}
}
1 change: 1 addition & 0 deletions src/place/place.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class PlaceController {
@ApiOperation({ summary: '카카오 place id로 장소 등록' })
@ApiParam({ name: 'mapId', description: '지도(GroupMap) id' })
@ApiParam({ name: 'kakaoPlaceId', description: '카카오 place id' })
@UseMapRoleGuard()
@UseAuthGuard([UserRole.USER])
@Post(':mapId/kakao/:kakaoPlaceId')
async registerPlaceByKakaoId(
Expand Down
7 changes: 6 additions & 1 deletion src/search/dtos/searched-place-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class SearchedPlaceResponseDto {
@IsOptional()
likedUserIds?: number[];

@ApiProperty({ description: '기타일 경우 100' })
category_icon_code: number;

constructor(searchedPlace: KakaoPlaceItem | PlaceForMap) {
if ('place' in searchedPlace) {
const { place } = searchedPlace;
Expand All @@ -64,14 +67,16 @@ export class SearchedPlaceResponseDto {
this.createdBy = new CreatedUser(searchedPlace.createdBy);
this.score = kakaoPlace.score;
this.likedUserIds = searchedPlace.likedUserIds;
this.category_icon_code = kakaoPlace.categoryIconCode;
} else {
this.isRegisteredPlace = false;
this.kakaoId = Number(searchedPlace.id);
this.category = searchedPlace.category_group_name;
this.category = searchedPlace.category_name;
this.x = Number(searchedPlace.x);
this.y = Number(searchedPlace.y);
this.placeName = searchedPlace.place_name;
this.address = searchedPlace.road_address_name;
this.category_icon_code = searchedPlace.category_icon_code;
}
}
}
1 change: 1 addition & 0 deletions src/search/kakao-map.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type KakaoPlaceItem = {
road_address_name: string; // "서울 강남구 논현로94길 13"
x: string; // "127.037366122263"
y: string; // "37.5026329250425"
category_icon_code: number; // category_name 기준으로 mapping
};

export type KakaoPlaceDetailRaw = {
Expand Down
4 changes: 2 additions & 2 deletions src/search/search.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { UserModule } from 'src/user/user.module';
import { UtilModule } from 'src/util/util.module';

import { KakaoPlace, PlaceForMap } from '../entities';
import { CategoryIconMapping, KakaoPlace, PlaceForMap } from '../entities';
import { KakaoMapHelper } from './kakao-map.helper';
import { SearchController } from './search.controller';
import { SearchService } from './search.service';
Expand All @@ -15,7 +15,7 @@ import { SearchService } from './search.service';
imports: [
HttpModule,
UtilModule,
MikroOrmModule.forFeature([KakaoPlace, PlaceForMap]),
MikroOrmModule.forFeature([KakaoPlace, PlaceForMap, CategoryIconMapping]),
UserModule,
],
controllers: [SearchController],
Expand Down
Loading

0 comments on commit 01ddd67

Please sign in to comment.