From b3a1cd86f5cd768d20af426d088af1b6b08e079d Mon Sep 17 00:00:00 2001 From: minkj1992 Date: Fri, 22 Jan 2021 20:34:43 +0900 Subject: [PATCH] =?UTF-8?q?Update:=20spot=20=EC=A0=80=EC=9E=A5=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - cache에 place없을 경우 검색해서 가져옴 - spot의 location 추가 ($.geonear) --- src/place/kakaoMapSearch/search.service.ts | 20 +++++++++++-- src/spot/entities/spot.entity.ts | 12 +++++--- src/spot/spot.resolver.ts | 18 ++++++++--- src/spot/spot.service.ts | 35 ++++++++++++++++++---- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/place/kakaoMapSearch/search.service.ts b/src/place/kakaoMapSearch/search.service.ts index 81d7950..3249c90 100644 --- a/src/place/kakaoMapSearch/search.service.ts +++ b/src/place/kakaoMapSearch/search.service.ts @@ -8,9 +8,11 @@ import { } from "@nestjs/common"; import { Cache } from "cache-manager"; +import { CreateSpotInput } from "src/spot/dto/create-spot.input"; import { ConfigService } from "../../config/config.service"; import { KeywordSearchDto } from "./search.dto"; import { Place } from "../place.entity"; +import { SortType } from "src/place/kakaoMapSearch/search.dto"; @Injectable() export class SearchService { @@ -20,9 +22,7 @@ export class SearchService { ) {} // https://developers.kakao.com/docs/latest/ko/local/dev-guide#search-by-keyword - async searchByKeyworld( - keywordSearchDto: KeywordSearchDto - ): Promise> { + async searchByKeyword(keywordSearchDto: KeywordSearchDto): Promise { const baseUrl = this.configService.get("KAKAO_DEV_HOST"); return Axios.get(baseUrl, { headers: { @@ -63,4 +63,18 @@ export class SearchService { async getPlaceFromCacheById(id): Promise { return this.cacheManager.get(id); } + + async getIdenticalPlace( + createSpotInput: CreateSpotInput + ): Promise { + const places: Place[] = await this.searchByKeyword({ + query: createSpotInput.place_name, + x: createSpotInput.x, + y: createSpotInput.y, + radius: 1, + sort: SortType.distance, + }); + console.log(places); + return places.length >= 1 ? places[0] : null; + } } diff --git a/src/spot/entities/spot.entity.ts b/src/spot/entities/spot.entity.ts index 91a9063..ba3ed9a 100644 --- a/src/spot/entities/spot.entity.ts +++ b/src/spot/entities/spot.entity.ts @@ -5,10 +5,6 @@ import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose"; @ObjectType() @Schema({ timestamps: true }) // graphql 은 timestamp 삽입 어떻게 할까? export class Spot { - @Prop({ type: mongoose.Schema.Types.ObjectId, ref: "Spot" }) - @Field(() => ID, { description: "mongodb default id" }) - _id: Spot; - @Prop({ required: true, unique: true }) @Field(() => String, { description: "kakao place id" }) id: string; @@ -69,6 +65,14 @@ export class Spot { }, }) location: string; + + @Field((type) => Float, { nullable: true }) + @Prop() + x?: number; + + @Field((type) => Float, { nullable: true }) + @Prop() + y?: number; } export type SpotDocument = Spot & mongoose.Document; diff --git a/src/spot/spot.resolver.ts b/src/spot/spot.resolver.ts index 09b9199..24cea12 100644 --- a/src/spot/spot.resolver.ts +++ b/src/spot/spot.resolver.ts @@ -1,4 +1,4 @@ -import { Resolver, Query, Mutation, Args, Int } from "@nestjs/graphql"; +import { Resolver, Query, Mutation, Args, Int, Float } from "@nestjs/graphql"; import { SpotService } from "src/spot/spot.service"; import { Spot } from "src/spot/entities/spot.entity"; import { CreateSpotInput } from "src/spot/dto/create-spot.input"; @@ -10,13 +10,15 @@ export class SpotResolver { constructor(private readonly spotService: SpotService) {} @Mutation(() => Spot) - async createSpot(@Args("createSpotInput") createSpotInput: CreateSpotInput) { + async createSpot( + @Args("createSpotInput") createSpotInput: CreateSpotInput + ): Promise { const spot = await this.spotService.findOne(createSpotInput.id); if (spot === null) { - return this.spotService.create(createSpotInput); + return await this.spotService.create(createSpotInput); } else { - return this.spotService.update(spot, createSpotInput.emoji); + return await this.spotService.update(spot, createSpotInput.emoji); } } @@ -25,6 +27,14 @@ export class SpotResolver { return await this.spotService.findAll(); } + // @Query(() => [Spot]) + // async getSpots( + // @Args("x", { type: () => Float }) x: number, + // @Args("y", { type: () => Float }) y: number + // ) { + // return await this.spotService.getSpot(x, y); + // } + // @Query(() => Spot, { name: "spot" }) // async findOne(@Args("id", { type: () => Int }) id: number) { // return this.spotService.findOne(id); diff --git a/src/spot/spot.service.ts b/src/spot/spot.service.ts index 236d0ff..4459603 100644 --- a/src/spot/spot.service.ts +++ b/src/spot/spot.service.ts @@ -2,10 +2,12 @@ import { Injectable } from "@nestjs/common"; import { InjectModel } from "@nestjs/mongoose"; import { Model, Types } from "mongoose"; import { SearchService } from "src/place/kakaoMapSearch/search.service"; +import { SortType } from "src/place/kakaoMapSearch/search.dto"; import { CreateSpotInput } from "src/spot/dto/create-spot.input"; import { UpdateSpotInput } from "src/spot/dto/update-spot.input"; import { Spot, SpotDocument } from "src/spot/entities/spot.entity"; +import { Place } from "src/place/place.entity"; @Injectable() export class SpotService { @@ -14,21 +16,42 @@ export class SpotService { private readonly searchService: SearchService ) {} - async create(createSpotInput: CreateSpotInput) { - const place = await this.searchService.getPlaceFromCacheById( + async create(createSpotInput: CreateSpotInput): Promise { + let place: + | Place + | undefined = await this.searchService.getPlaceFromCacheById( createSpotInput.id ); - // place.emoji = createSpotInput.emoji; - // TODO: cache miss .... + if (place === undefined) { + const placeResult = await this.searchService.getIdenticalPlace( + createSpotInput + ); - const createdSpot = new this.spotModel(place); + if (placeResult === undefined) { + // TODO: custom place 만들기 + // pass + } else { + place = placeResult; + } + } + + const location = { type: "Point", coordinates: [place.x, place.y] }; + const createSpotDto = { + id: createSpotInput.id, + emojis: [createSpotInput.emoji], + location, + ...place, + }; + const createdSpot = new this.spotModel(createSpotDto); + console.log(createdSpot); + // TODO: save error handling return createdSpot.save(); } async update(spot: any, emoji: string): Promise { spot.emojis.push(emoji); - return await spot.save(); + return spot.save(); // const update = { $push: { emojis: emoji } }; // return await this.spotModel.findOneAndUpdate(filter, update); }