This repository was archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: data accessor에서 setParameters 제거 (#118)
* school menu 서비스 리팩토링 * school info 서비스 리팩토링 * 테스트 타이핑 오류 수정 * 타이핑 오류 수정 * 타이핑 오류 수정 * testTimeout 수정
- Loading branch information
Showing
16 changed files
with
189 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface Crawler<T> { | ||
get(): Promise<T> | ||
export interface Crawler<T, Q> { | ||
get(identifier: Readonly<Q>): Promise<T> | ||
shouldSave(): boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
export interface DataAccessor<T> { | ||
setParameters(...any): DataAccessor<T>; | ||
close(): any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node' | ||
testEnvironment: 'node', | ||
testTimeout: 10000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 10 additions & 10 deletions
20
functions/package-school-info/src/service/SchoolInfoService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 10 additions & 25 deletions
35
functions/package-school-menu/src/data/MenuDataAccessor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,40 @@ | ||
import { DataAccessor } from '@school-api/common' | ||
import { SchoolMenu } from '../type/SchoolMenu' | ||
import { firestore } from 'firebase-admin' | ||
import { SchoolMenuIdentifier } from '../type/parameter' | ||
|
||
const collectionName = 'schoolmenu' | ||
|
||
export class MenuDataAccessor implements DataAccessor<SchoolMenu[]> { | ||
private db: firestore.Firestore; | ||
private ref: firestore.CollectionReference; | ||
|
||
private schoolCode: string; | ||
private menuYear: number; | ||
private menuMonth: number; | ||
|
||
constructor (db: firestore.Firestore) { | ||
this.db = db | ||
this.ref = this.db.collection(collectionName) | ||
} | ||
|
||
setParameters (schoolCode: string, menuYear: number, menuMonth: number): MenuDataAccessor { | ||
this.schoolCode = schoolCode | ||
this.menuYear = menuYear | ||
this.menuMonth = menuMonth | ||
|
||
return this | ||
} | ||
|
||
async get (): Promise<SchoolMenu[]> { | ||
async get (identifier: SchoolMenuIdentifier): Promise<SchoolMenu[]> { | ||
const snapshots = await this.ref | ||
.where('schoolCode', '==', this.schoolCode) | ||
.where('menuYear', '==', this.menuYear) | ||
.where('menuMonth', '==', this.menuMonth) | ||
.where('schoolCode', '==', identifier.schoolCode) | ||
.where('menuYear', '==', identifier.menuYear) | ||
.where('menuMonth', '==', identifier.menuMonth) | ||
.get() | ||
|
||
if (snapshots.docs.length == 0) { | ||
if (snapshots.docs.length === 0) { | ||
return null | ||
} | ||
|
||
return snapshots.docs[0].data().menu as SchoolMenu[] | ||
} | ||
|
||
async put (menu: SchoolMenu[]) { | ||
async put (identifier: SchoolMenuIdentifier, menu: SchoolMenu[]) { | ||
return await this.ref.doc().set({ | ||
menu, | ||
version: 2, | ||
schoolCode: this.schoolCode, | ||
menuYear: this.menuYear, | ||
menuMonth: this.menuMonth | ||
schoolCode: identifier.schoolCode, | ||
menuYear: identifier.menuYear, | ||
menuMonth: identifier.menuMonth | ||
}) | ||
} | ||
|
||
close () { | ||
this.db.terminate() | ||
} | ||
} |
Oops, something went wrong.