Skip to content

Commit

Permalink
add: userId
Browse files Browse the repository at this point in the history
  • Loading branch information
SIY1121 committed Feb 20, 2021
1 parent 176a2ef commit 40a65c8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 26 deletions.
18 changes: 10 additions & 8 deletions __tests__/grpc/timetable.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,11 @@ describe('deleteRegisteredCourse', () => {
test('deleteRegisteredCourse', (done) => {
const ids = [v4(), v4()]
mocked(deleteRegisteredCoursesUseCase).mockImplementation(async (i) => {
expect(i).toEqual(deepContaining(ids))
expect(i.ids).toEqual(deepContaining(ids))
expect(i.userId).toEqual(userId)
})

client.deleteRegisteredCourses({ ids }, (err, res) => {
client.deleteRegisteredCourses({ userId, ids }, (err, res) => {
expect(err).toBeNull()
done()
})
Expand All @@ -351,7 +352,7 @@ describe('deleteRegisteredCourse', () => {
throw new NotFoundError('指定された講義は見つかりませんでした')
})

client.deleteRegisteredCourses({ ids }, (err, res) => {
client.deleteRegisteredCourses({ userId, ids }, (err, res) => {
expect(err?.code).toBe(Status.NOT_FOUND)
done()
})
Expand All @@ -361,7 +362,7 @@ describe('deleteRegisteredCourse', () => {
mocked(deleteRegisteredCoursesUseCase).mockImplementation(
throwUnexpectedError
)
client.deleteRegisteredCourses({ ids }, (err, res) => {
client.deleteRegisteredCourses({ userId, ids }, (err, res) => {
expect(err?.code).toBeTruthy()
done()
})
Expand All @@ -372,10 +373,11 @@ describe('deleteTags', () => {
test('deleteTags', (done) => {
const ids = [v4(), v4()]
mocked(deleteTagsUseCase).mockImplementation(async (i) => {
expect(i).toEqual(deepContaining(ids))
expect(i.ids).toEqual(deepContaining(ids))
expect(i.userId).toEqual(userId)
})

client.deleteTags({ ids }, (err, res) => {
client.deleteTags({ userId, ids }, (err, res) => {
expect(err).toBeNull()
done()
})
Expand All @@ -387,7 +389,7 @@ describe('deleteTags', () => {
throw new NotFoundError('指定されたタグは見つかりませんでした')
})

client.deleteTags({ ids }, (err, res) => {
client.deleteTags({ userId, ids }, (err, res) => {
expect(err?.code).toBe(Status.NOT_FOUND)
done()
})
Expand All @@ -397,7 +399,7 @@ describe('deleteTags', () => {
const ids = [v4(), v4()]
mocked(deleteTagsUseCase).mockImplementation(throwUnexpectedError)

client.deleteTags({ ids }, (err, res) => {
client.deleteTags({ userId, ids }, (err, res) => {
expect(err?.code).toBeTruthy()
done()
})
Expand Down
14 changes: 10 additions & 4 deletions __tests__/usecase/deleteRegisteredCourses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,20 @@ beforeAll(async () => {

test('既に存在するデータを正しく削除できる', () => {
return expect(
deleteRegisteredCoursesUseCase(data.map((c) => c.id))
deleteRegisteredCoursesUseCase({ userId, ids: data.map((c) => c.id) })
).resolves.toBeUndefined()
})

test('存在しないデータはエラーになる', () => {
return expect(deleteRegisteredCoursesUseCase([v4()])).rejects.toThrow(
NotFoundError
)
return expect(
deleteRegisteredCoursesUseCase({ userId, ids: [v4()] })
).rejects.toThrow(NotFoundError)
})

test('存在しないデータはエラーになる', () => {
return expect(
deleteRegisteredCoursesUseCase({ userId: v4(), ids: data.map((c) => c.id) })
).rejects.toThrow(NotFoundError)
})

afterAll(disconnectDatabase)
12 changes: 10 additions & 2 deletions __tests__/usecase/deleteTags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ beforeAll(async () => {

test('正しく削除できる', () => {
return expect(
deleteTagsUseCase(data.map((t) => t.id))
deleteTagsUseCase({ userId, ids: data.map((t) => t.id) })
).resolves.toBeUndefined()
})

test('存在しないデータでエラーになる', () => {
return expect(deleteTagsUseCase([v4()])).rejects.toThrow(NotFoundError)
return expect(deleteTagsUseCase({ userId, ids: [v4()] })).rejects.toThrow(
NotFoundError
)
})

test('存在しないデータでエラーになる', () => {
return expect(
deleteTagsUseCase({ userId: v4(), ids: data.map((t) => t.id) })
).rejects.toThrow(NotFoundError)
})

afterAll(disconnectDatabase)
6 changes: 4 additions & 2 deletions protos/TimetableService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ message UpdateTagsResponse {


message DeleteRegisteredCoursesRequest {
repeated string ids = 1;
string userId = 1;
repeated string ids = 2;
}
message DeleteRegisteredCoursesResponse {}


message DeleteTagsRequest {
repeated string ids = 1;
string userId = 1;
repeated string ids = 2;
}
message DeleteTagsResponse {}
4 changes: 2 additions & 2 deletions src/grpc/timetable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ export const timetableService: GrpcServer<TimetableService> = {
},
async deleteRegisteredCourses({ request }, callback) {
try {
await deleteRegisteredCoursesUseCase(request.ids)
await deleteRegisteredCoursesUseCase(request)
callback(null, DeleteRegisteredCoursesResponse.create({}))
} catch (e) {
callback(toGrpcError(e))
}
},
async deleteTags({ request }, callback) {
try {
await deleteTagsUseCase(request.ids)
await deleteTagsUseCase(request)
callback(null, DeleteTagsResponse.create({}))
} catch (e) {
callback(toGrpcError(e))
Expand Down
11 changes: 8 additions & 3 deletions src/usecase/deleteRegisteredCourses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { getConnection } from 'typeorm'
import { RegisteredCourse } from '../database/model/registeredCourse'
import { NotFoundError } from '../error'

type Input = string[]
type Input = {
userId: string
ids: string[]
}

export async function deleteRegisteredCoursesUseCase(ids: Input) {
export async function deleteRegisteredCoursesUseCase({ userId, ids }: Input) {
const repository = getConnection().getRepository(RegisteredCourse)
const target = await repository.findByIds(ids)
const target = await repository.find({
where: ids.map((id) => ({ id, userId })),
})
if (target.length !== ids.length)
throw new NotFoundError(
'指定された講義は見つかりませんでした',
Expand Down
11 changes: 8 additions & 3 deletions src/usecase/deleteTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { getConnection } from 'typeorm'
import { Tag } from '../database/model/tag'
import { NotFoundError } from '../error'

type Input = string[]
type Input = {
userId: string
ids: string[]
}

export async function deleteTagsUseCase(ids: Input) {
export async function deleteTagsUseCase({ userId, ids }: Input) {
const repository = getConnection().getRepository(Tag)
const target = await repository.findByIds(ids)
const target = await repository.find({
where: ids.map((id) => ({ id, userId })),
})
if (target.length !== ids.length)
throw new NotFoundError(
'指定されたタグは見つかりませんでした',
Expand Down
4 changes: 3 additions & 1 deletion src/usecase/updateRegisteredCourses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export async function updateRegisteredCourseUseCase(courses: Input[]) {
})
try {
const repository = getConnection().getRepository(RegisteredCourse)
const target = await repository.findByIds(courses.map((c) => c.id))
const target = await repository.find({
where: courses.map(({ id, userId }) => ({ id, userId })),
})
if (target.length !== courses.length)
throw new NotFoundError(
'指定された講義は見つかりませんでした',
Expand Down
4 changes: 3 additions & 1 deletion src/usecase/updateTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ type Input = {

export async function updateTagsUseCase(tags: Input[]) {
const repository = getConnection().getRepository(Tag)
const target = await repository.findByIds(tags.map((t) => t.id))
const target = await repository.find({
where: tags.map(({ id, userId }) => ({ id, userId })),
})
if (target.length !== tags.length)
throw new NotFoundError(
'指定されたタグが見つかりません',
Expand Down

0 comments on commit 40a65c8

Please sign in to comment.