프로그래밍(Web)/Javascript(TS,Node)

[바미] Typescript-restful-starter - Plan 부분 실행.

Bami 2020. 12. 24. 18:35
728x90
반응형

수정 사항.


여기 Plan부분 시행.

  1. name외에도 age, phonenumber와 같은 데이터를 추가 할 수 있도록 수정.
  2. 기존 데이터에서 1번에서 추가한 컬럼 값들을 넣지 않은 데이터들이 Update될 수 있도록 수정.
  3. Create로 생성된 데이터 중 입력한 이름을 가진 데이터, 입력한 나이를 가진 데이터, 입력한 전화번호를 가진 데이터, 등을
    검색하여 조회 할 수 있도록 수정.

코드


Sample.controller.ts


    // select -> routes/Sample.route.ts 참조.
    public async find5(): Promise<Response> {
        const { age } = this.req.params as unknown as { age: number };
        const sample = await this.sampleService.findByAge(age);
        if (sample) {
            return this.res.status(200).send(sample);
        } else {
            return this.res.status(404).send({ text: "not found" });
        }
    }

    // select -> routes/Sample.route.ts 참조.
    public async find6(): Promise<Response> {
        const { phone } = this.req.params as unknown as { phone: string };
        const sample = await this.sampleService.findByPhone(phone);
        if (sample) {
            return this.res.status(200).send(sample);
        } else {
            return this.res.status(404).send({ text: "not found" });
        }
    }

Sample.route.ts


export class SampleRouter extends Router {
    constructor() {
        super(SampleController);
        this.router
            .get("/", this.handler(SampleController.prototype.all))
            .get("/find/text/:text", this.handler(SampleController.prototype.find))
            .get("/find/id/:id", this.handler(SampleController.prototype.find2))
            .get("/find/email/:email", this.handler(SampleController.prototype.find3))
            .get("/find/name/:name", this.handler(SampleController.prototype.find4))
            .get("/find/age/:age", this.handler(SampleController.prototype.find5)) // 추가
            .get("/find/phone/:phone", this.handler(SampleController.prototype.find6)) // 추가
            .post("/", [ Validator(createSample) ], this.handler(SampleController.prototype.create))
            .put("/", [ Validator(updateSample) ],  this.handler(SampleController.prototype.update))
            .delete("/", [ Validator(deleteSample) ], this.handler(SampleController.prototype.delete));
    }
}

Sample.repository.ts


import { EntityRepository, Repository } from "typeorm";
import { Sample } from "../models";

@EntityRepository(Sample)
export class SampleRepository extends Repository<Sample> {

    public bulkCreate(Samples: Sample[]): Promise<any> {
        return this.manager.createQueryBuilder().insert().into(Sample).values(Samples).execute();
    }

    public async removeById(id: number): Promise<Sample> {
        const itemToRemove: Sample = await this.findOne({id});
        return this.manager.remove(itemToRemove);
    }

    public findByText(text: string): Promise<Sample[]> {
        return this.manager.find(Sample, {where: {text}});
    }

    public findByEmail(email: string): Promise<Sample[]> {
        return this.manager.find(Sample, {where: {email}});
    }

    public findByName(name: string): Promise<Sample[]> {
        return this.manager.find(Sample, {where: {name}});
    }

    public findByAge(age: number): Promise<Sample[]> { // 추가
        return this.manager.find(Sample, {where: {age}});
    }

    public findByPhone(phone: string): Promise<Sample[]> { // 추가
        return this.manager.find(Sample, {where: {phone}});
    }

    public findOneById(id: number): Promise<Sample> {
        return this.manager.findOne(Sample, {where: {id}});
    }

}

Sample.service.ts


import { getCustomRepository } from "typeorm";
import { Sample } from "../models";
import { SampleRepository } from "../repository";

export class SampleService {

    public findByText(text: string): Promise<Sample[]> {
        return getCustomRepository(SampleRepository).findByText(text);
    }

    public findByEmail(email: string): Promise<Sample[]> {
        return getCustomRepository(SampleRepository).findByEmail(email);
    }

    public findByName(name: string): Promise<Sample[]> {
        return getCustomRepository(SampleRepository).findByName(name);
    }

    public findByAge(age: number): Promise<Sample[]> { // 추가
        return getCustomRepository(SampleRepository).findByAge(age);
    }

    public findByPhone(phone: string): Promise<Sample[]> { // 추가
        return getCustomRepository(SampleRepository).findByPhone(phone);
    }

    public bulkCreate(Samples: Sample[]): Promise<Sample[]> {
        return getCustomRepository(SampleRepository).bulkCreate(Samples);
    }

    public findOneById(id: number): Promise<Sample> {
        return getCustomRepository(SampleRepository).findOneById(id);
    }

    public find(): Promise<Sample[]> {
        return getCustomRepository(SampleRepository).find();
    }

    public remove(sample: Sample): Promise<Sample> {
        return getCustomRepository(SampleRepository).remove(sample);
    }

    public removeById(id: number): Promise<Sample> {
        return getCustomRepository(SampleRepository).removeById(id);
    }

    public save(sample: Sample): Promise<Sample> {
        return getCustomRepository(SampleRepository).save(sample);
    }

}

Sample.schemas.ts


  import { number, object, string } from "joi";

  /* 구조 정의 */ 
  export const createSample = object().keys({
      text: string().required(),
      email: string().required(),
      name: string().required(),
      age: number().required(), // 추가
      phone: string().required(), // 추가
  });

  export const updateSample = object().keys({
      id: number().required(),
      text: string().required(),
      email: string().required(),
      name: string().required(), // 추가
      age: number().required(), // 추가
      phone: string().required(), // 추가
  });

  export const deleteSample = object().keys({
      id: number().required(),
  });

실행화면


Insert(Post)

Tester

Web

Select(Get)


name 검색

age 검색

phone 검색

Update(Put)


Tester

Web

728x90
반응형