Skip to content

Commit

Permalink
Update: spot 저장하기 수정
Browse files Browse the repository at this point in the history
- cache에 place없을 경우 검색해서 가져옴
- spot의 location 추가 ($.geonear)
  • Loading branch information
minkj1992 committed Jan 22, 2021
1 parent f8bdfa1 commit b3a1cd8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
20 changes: 17 additions & 3 deletions src/place/kakaoMapSearch/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<HttpException | AxiosResponse<object>> {
async searchByKeyword(keywordSearchDto: KeywordSearchDto): Promise<Place[]> {
const baseUrl = this.configService.get("KAKAO_DEV_HOST");
return Axios.get(baseUrl, {
headers: {
Expand Down Expand Up @@ -63,4 +63,18 @@ export class SearchService {
async getPlaceFromCacheById(id): Promise<Place> {
return this.cacheManager.get(id);
}

async getIdenticalPlace(
createSpotInput: CreateSpotInput
): Promise<Place | null> {
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;
}
}
12 changes: 8 additions & 4 deletions src/spot/entities/spot.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 14 additions & 4 deletions src/spot/spot.resolver.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<Spot> {
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);
}
}

Expand All @@ -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);
Expand Down
35 changes: 29 additions & 6 deletions src/spot/spot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Spot> {
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> {
spot.emojis.push(emoji);
return await spot.save();
return spot.save();
// const update = { $push: { emojis: emoji } };
// return await this.spotModel.findOneAndUpdate(filter, update);
}
Expand Down

0 comments on commit b3a1cd8

Please sign in to comment.