Skip to content
Merged
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
32 changes: 32 additions & 0 deletions apps/backend/src/library/library.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
Controller,
Delete,
Get,
Param,
ParseIntPipe,
UseGuards,
} from '@nestjs/common';
import { LibraryService } from './library.service';
import { AuthGuard } from '@nestjs/passport';
import { Library } from './library.entity';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('Library')
@ApiBearerAuth()
@Controller('library')
@UseGuards(AuthGuard('jwt'))
export class LibraryController {
constructor(private libraryService: LibraryService) {}

@Get('/:libraryId')
async getLibrary(
@Param('libraryId', ParseIntPipe) libraryId: number,
): Promise<Library> {
return this.libraryService.findOne(libraryId);
}

@Delete('/:id')
removeLibrary(@Param('id') id: string) {
return this.libraryService.remove(parseInt(id));
}
}
11 changes: 11 additions & 0 deletions apps/backend/src/library/library.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Entity, Column } from 'typeorm';
import { Anthology } from '../anthology/anthology.entity';

@Entity()
export class Library {
@Column({ primary: true })
id: number;

@Column()
anthologies: Anthology[];
}
12 changes: 12 additions & 0 deletions apps/backend/src/library/library.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LibraryController } from './library.controller';
import { LibraryService } from './library.service';
import { Library } from './library.entity';

@Module({
imports: [TypeOrmModule.forFeature([Library])],
controllers: [LibraryController],
providers: [LibraryService],
})
export class LibraryModule {}
77 changes: 77 additions & 0 deletions apps/backend/src/library/library.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { Library } from './library.entity';
import { Anthology } from '../anthology/anthology.entity';

@Injectable()
export class LibraryService {
constructor(@InjectRepository(Library) private repo: Repository<Library>) {}

async create(anthologies: Anthology[]) {
const libraryId = (await this.repo.count()) + 1;
const library = this.repo.create({
id: libraryId,
anthologies: anthologies,
});

return this.repo.save(library);
}

findOne(id: number) {
if (!id) {
return null;
}

return this.repo.findOneBy({ id });
}

findAll() {
return this.repo.find();
}

async update(id: number, attrs: Partial<Library>) {
const library = await this.findOne(id);

if (!library) {
throw new NotFoundException('Library not found');
}

Object.assign(library, attrs);

return this.repo.save(library);
}

async remove(id: number) {
const library = await this.findOne(id);

if (!library) {
throw new NotFoundException('Library not found');
}

return this.repo.remove(library);
}

async addAnthology(id: number, anthology: Anthology | Anthology[]) {
const library = await this.findOne(id);

if (!library) {
throw new NotFoundException('Library not found');
}

library.anthologies.concat(anthology);
return this.repo.save(library);
}

async removeAnthology(id: number, anthology: Anthology) {
const library = await this.findOne(id);

if (!library) {
throw new NotFoundException('Library not found');
}

library.anthologies.filter((a) => a !== anthology);
return this.repo.save(library);
}
}