Skip to content

Commit

Permalink
refactor: 코스 id를 통해 owner id를 조회하는 로직을 만들어서 필요한 부분만 조회할 수 있도록 변경 #83
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 9, 2024
1 parent 5200c83 commit 7a5316f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions backend/src/course/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export class CourseService {
return await CourseDetailResponse.from(map);
}

async getCourseOwnerId(courseId: number) {
const course = await this.courseRepository.findById(courseId);
if (!course) throw new CourseNotFoundException(courseId);

return course.user.id;
}

async createCourse(userId: number, createMapForm: CreateCourseRequest) {
const user = { id: userId } as User;
const map = createMapForm.toEntity(user);
Expand Down
6 changes: 3 additions & 3 deletions backend/src/course/guards/CoursePermissionGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export class CoursePermissionGuard implements CanActivate {
const courseId = Number(request.params.id);
const userId = Number(request.user.userId);

const course = await this.courseService.getCourseById(courseId);
if (course.user.id !== userId) {
const course = await this.courseService.getCourseOwnerId(courseId);
if (course !== userId) {
throw new CoursePermissionException(courseId);
}
return course.user.id === userId;
return true;
}
}

0 comments on commit 7a5316f

Please sign in to comment.