728x90
schedule 파트 기본 CRUD를 완성하고 인섬니아로 확인을 하고 싶지만 서버 연결이 되지 않고, 다른 파트가 완성이 되지 않아 확인할 수 없어서 리더님께 코드를 하나하나 설명하면서 코드에 대한 이해도를 높였음
수정한 부분들
1. 스케쥴 등록 부분의 코드의 순서 오류
- 스케쥴레파지토리 부분에 세이브를 하는 코드가 메인 로직의 맨 꼭대기에 있었음.
2. 스케쥴 전체 조회
- 조회를 할 때 전체적인 스케쥴이 나와야하는데 1개의 스케쥴만 보임.
그룹을 찾는 find의 조건에 groupId와 scheduleId를 넣어줬음 => 1개만 찾아옴..
groupId와 scheduleId를 함께 넣어준 이유: 해당하는 그룹의 스케쥴들을 찾겠다.
수정한 이유
1. 그룹을 1개 찾아주고 스케쥴이 소속된 그룹이 있는지 확인을 하고, 생성을 해줬어야했는데 맨 꼭대기에 올라와있었음.
2. schedulesRepository는 모든 스케쥴이 담겨있는 레포지토리인데 groupId만 입력하면 해당 그룹 아이디에 포함되는 모든 스케쥴들이 나오는데 scheduleId 까지 입력해버리면 a그룹의 a스케쥴을 찾겠다는 의미가 되어버리는 것임...
import { HttpStatus, Injectable } from '@nestjs/common';
import { ScheduleDto } from './dto/schedule.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Schedules } from './entities/schedule.entity';
import { Repository } from 'typeorm';
@Injectable()
export class SchedulesService {
constructor(
@InjectRepository(Schedules)
private ScheduleRepository: Repository<Schedules>,
) {}
/** 전체적으로 다시 수정해야 하거나 생각해봐야 하는 것: group안에 있는 스케쥴임....**/
// 스케쥴 등록
async createSchedule(
createScheduleDto: ScheduleDto,
groupId: number,
userId: string,
) {
const { title, content, category, scheduleDate } = createScheduleDto;
// 매개변수로 받은 groupId로 그룹을 찾는다.
const group = await this.ScheduleRepository.find({
where: { groupId },
});
if (!group) {
throw {
status: HttpStatus.NOT_FOUND,
};
}
await this.ScheduleRepository.save({
groupId,
userId,
title,
content,
category,
scheduleDate,
});
return {
status: HttpStatus.CREATED,
};
}
// 스케쥴 전체 조회
/** 클라이언트가 url에 접근하면 자동적으로 싹 보여줌... 그럼 파라미터는 없어도 되는 거 아닌가?**/
async getAllSchedule(groupId: number) {
const allSchedule = await this.ScheduleRepository.find({
where: { groupId },
});
if (!allSchedule) {
throw {
status: HttpStatus.NOT_FOUND,
};
}
return allSchedule;
}
// 스케쥴 상세 조회
async getOneSchedule(groupId: number, scheduleId: number) {
const schedule = this.ScheduleRepository.findOne({
where: { groupId, scheduleId },
});
if (!schedule) {
throw {
status: HttpStatus.NOT_FOUND,
};
}
return schedule;
}
// 스케쥴 수정
async changeSchedule(changeScheduleDto: ScheduleDto, scheduleId: number) {
// 교체하고자 하는 스케쥴 1개를 찾아준다.
const schedule = await this.ScheduleRepository.findOne({
where: { scheduleId },
});
if (!schedule) {
throw {
status: HttpStatus.NOT_FOUND,
};
}
await this.ScheduleRepository.update(scheduleId, changeScheduleDto);
return {
status: HttpStatus.OK,
};
}
// 스케쥴 삭제
async deleteSchedule(scheduleId: number) {
const schedule = await this.ScheduleRepository.findOne({
where: { scheduleId },
});
if (!schedule) {
throw {
status: HttpStatus.NOT_FOUND,
};
}
await this.ScheduleRepository.delete({ scheduleId });
return {
status: HttpStatus.OK,
};
}
}
테스트 코드도 시작했지만 자료들을 살펴보고 공부를 해봐도 너무 이해가 되지 않는 부분들이 많았음...
// SchedulesService를 테스트할 거임.(테스트 대상을 모셔옴)
import { Test, TestingModule } from '@nestjs/testing';
import { SchedulesService } from './schedules.service';
// describe는 문자열(이름), 콜백함수(수행될 코드)를 파라미터로 받음
describe('schedulesService', () => {
// 테스트 스위트 전체에서 사용할 ScheduleService를 선언함
let schedulesService: SchedulesService;
beforeEach(async () => {
// 테스트 모듈을 만들어준다.
const module: TestingModule = await Test.createTestingModule({
// 실제로 주입될 의존성을 나타낸다.
providers: [SchedulesService],
}).compile(); // compile 함수를 사용해서 모듈 생성을 완료함(비동기로 처리된다.)
});
});
아무튼 큰일임... 자료를 봐도 이게 맞나 싶고 코드를 어떻게 써야할 지 도통 감이 안옴...