Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/backend/src/anthology/dtos/update-anthology.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateAnthologyDto } from './create-anthology.dto';

export class UpdateAnthologyDto extends PartialType(CreateAnthologyDto) {}
110 changes: 110 additions & 0 deletions apps/backend/src/library/library.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('LibraryController', () => {
getAnthologies: jest.fn(),
remove: jest.fn(),
createAnthology: jest.fn(),
updateAnthology: jest.fn(),
};

beforeEach(async () => {
Expand Down Expand Up @@ -208,6 +209,115 @@ describe('LibraryController', () => {
});
});

describe('updateAnthology', () => {
it('should update an anthology successfully', async () => {
const updateDto = {
title: 'Updated Title',
description: 'Updated description',
};

const updatedAnthology = {
...mockAnthology,
title: 'Updated Title',
description: 'Updated description',
};

mockLibraryService.updateAnthology.mockResolvedValue(updatedAnthology);

const result = await controller.updateAnthology(1, updateDto);

expect(result).toEqual(updatedAnthology);
expect(mockLibraryService.updateAnthology).toHaveBeenCalledWith(
1,
updateDto,
);
});

it('should throw NotFoundException when anthology does not exist', async () => {
const updateDto = {
title: 'Updated Title',
};

mockLibraryService.updateAnthology.mockRejectedValue(
new NotFoundException('Anthology not found'),
);

await expect(controller.updateAnthology(999, updateDto)).rejects.toThrow(
NotFoundException,
);
expect(mockLibraryService.updateAnthology).toHaveBeenCalledWith(
999,
updateDto,
);
});

it('should update anthology with partial data', async () => {
const updateDto = {
inventory: 150,
};

const updatedAnthology = {
...mockAnthology,
inventory: 150,
};

mockLibraryService.updateAnthology.mockResolvedValue(updatedAnthology);

const result = await controller.updateAnthology(1, updateDto);

expect(result).toEqual(updatedAnthology);
expect(mockLibraryService.updateAnthology).toHaveBeenCalledWith(
1,
updateDto,
);
});

it('should update anthology status', async () => {
const updateDto = {
status: AnthologyStatus.ARCHIVED,
};

const updatedAnthology = {
...mockAnthology,
status: AnthologyStatus.ARCHIVED,
};

mockLibraryService.updateAnthology.mockResolvedValue(updatedAnthology);

const result = await controller.updateAnthology(1, updateDto);

expect(result).toEqual(updatedAnthology);
expect(mockLibraryService.updateAnthology).toHaveBeenCalledWith(
1,
updateDto,
);
});

it('should update multiple fields at once', async () => {
const updateDto = {
title: 'Brand New Title',
inventory: 200,
status: AnthologyStatus.CAN_BE_SHARED,
shopify_url: 'https://shopify.com/updated',
};

const updatedAnthology = {
...mockAnthology,
...updateDto,
};

mockLibraryService.updateAnthology.mockResolvedValue(updatedAnthology);

const result = await controller.updateAnthology(1, updateDto);

expect(result).toEqual(updatedAnthology);
expect(mockLibraryService.updateAnthology).toHaveBeenCalledWith(
1,
updateDto,
);
});
});

describe('removeLibrary', () => {
it('should remove a library successfully', async () => {
mockLibraryService.remove.mockResolvedValue(mockLibrary);
Expand Down
20 changes: 20 additions & 0 deletions apps/backend/src/library/library.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Param,
ParseIntPipe,
Post,
Put,
Body,
UseGuards,
HttpCode,
Expand All @@ -21,6 +22,7 @@ import {
} from '@nestjs/swagger';
import { Anthology } from '../anthology/anthology.entity';
import { CreateAnthologyDto } from '../anthology/dtos/create-anthology.dto';
import { UpdateAnthologyDto } from '../anthology/dtos/update-anthology.dto';

@ApiTags('Library')
@ApiBearerAuth()
Expand Down Expand Up @@ -62,6 +64,24 @@ export class LibraryController {
return this.libraryService.createAnthology(libraryId, createAnthologyDto);
}

@Put('/anthology/:anthologyId')
@ApiOperation({ summary: 'Update an anthology with the given id' })
@ApiResponse({
status: 200,
description: 'Anthology updated successfully',
type: Anthology,
})
@ApiResponse({
status: 404,
description: 'Anthology not found',
})
async updateAnthology(
@Param('anthologyId', ParseIntPipe) anthologyId: number,
@Body() updateAnthologyDto: UpdateAnthologyDto,
): Promise<Anthology> {
return this.libraryService.updateAnthology(anthologyId, updateAnthologyDto);
}

@Delete('/:id')
removeLibrary(@Param('id') id: string) {
return this.libraryService.remove(parseInt(id));
Expand Down
7 changes: 7 additions & 0 deletions apps/backend/src/library/library.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,11 @@ export class LibraryService {

return anthology;
}

async updateAnthology(
anthologyId: number,
attrs: Partial<Anthology>,
): Promise<Anthology> {
return this.anthologyService.update(anthologyId, attrs);
}
}