Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/3 #21

Merged
merged 2 commits into from
Sep 7, 2021
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
3 changes: 1 addition & 2 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
console.log(__dirname);
return process.env.HA;
return `Hello world!`;
}
}
16 changes: 10 additions & 6 deletions src/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class BooksController {
@UseInterceptors(ClassSerializerInterceptor)
@Get('/search')
async search(
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number = 1,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number = 10,
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit = 10,
): Promise<Pagination<BookInfo>> {
return this.booksService.search({
page,
Expand All @@ -95,11 +95,15 @@ export class BooksController {

@Get('info/')
async findInfo(
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number = 1,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number = 10,
@Query('sort') sort: string = 'new',
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page = 1,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit = 10,
@Query('sort') sort = 'new',
) {
return this.booksService.findInfo({ page, limit }, sort);
try {
return this.booksService.findInfo({ page, limit }, sort);
} catch (error) {
return error;
}
}

@Patch(':id')
Expand Down
10 changes: 5 additions & 5 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { UpdateBookDto } from './dto/update-book.dto';
Expand All @@ -7,7 +7,8 @@ import { Book } from './entities/book.entity';
import { paginate, IPaginationOptions } from 'nestjs-typeorm-paginate';
import { getConnection } from 'typeorm';

function setBookDatas(bookData) {
async function setBookDatas(bookData) {
if (bookData == undefined) throw new NotFoundException(bookData);
for (const book of bookData.books) {
if (book.status == 1) book.status = '비치중';
else if (book.status == 2) book.status = '대출중';
Expand Down Expand Up @@ -45,18 +46,17 @@ export class BooksService {
const connection = getConnection();
const bookInfoRepository = connection.getRepository(BookInfo);

const resultData = bookInfoRepository
return await bookInfoRepository
.findOne({
where: { id: bookInfoId },
relations: ['books', 'books.lendings'],
relations: ['books', 'books.lendings', 'books.lendings.returning'],
})
.then((bookData) => {
return setBookDatas(bookData);
})
.then((tBookData) => {
return tBookData;
});
return resultData;
}

async findInfo(options: IPaginationOptions, sort: string) {
Expand Down
14 changes: 9 additions & 5 deletions src/books/entities/book.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ export class Book {

@Expose({ name: 'dueDate' })
getDueDate() {
if (this.id % 2) {
const tDate = new Date();
return tDate.toJSON().substring(2, 10).split('-').join('.');
} else {
return null;
if (this.lendings.length == 0) return '-';
for (const lending of this.lendings) {
if (lending.returning) {
return '-';
} else {
const tDate = new Date(lending['createdAt']);
tDate.setDate(tDate.getDate() + 15);
return tDate.toJSON().substring(2, 10).split('-').join('.');
}
}
}
}
9 changes: 5 additions & 4 deletions src/books/entities/bookInfo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ export class BookInfo {
@Expose({
groups: ['detail'],
})
get donators() {
const donators: string[] = [];
donators() {
const donators = new Set();
for (const book of this.books) {
donators.push(book.donator);
if (book.donator != '') donators.add(book.donator);
}
return donators.join(', ');
if (donators.size == 0) return '-';
return [...donators].join(', ');
}
}
8 changes: 5 additions & 3 deletions src/lendings/entities/lending.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
OneToMany,
OneToOne,
JoinColumn,
} from 'typeorm';

import { User } from '../../users/entities/user.entity';
Expand Down Expand Up @@ -39,6 +40,7 @@ export class Lending {
@ManyToOne(() => Book, (book) => book.lendings)
book: Book;

@OneToMany(() => Returning, (returning) => returning.lending)
returnings: Returning[];
@OneToOne(() => Returning, (returning) => returning.lending)
@JoinColumn()
returning: Returning;
}
5 changes: 3 additions & 2 deletions src/lendings/lendings.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Patch,
Param,
Delete,
Query,
} from '@nestjs/common';
import { LendingsService } from './lendings.service';
import { UpdateLendingDto } from './dto/update-lending.dto';
Expand All @@ -15,8 +16,8 @@ export class LendingsController {
constructor(private readonly lendingsService: LendingsService) {}

@Post()
create() {
return this.lendingsService.create();
create(@Query('bookId') bookId: string, @Query('userId') userId: string) {
return this.lendingsService.create(+bookId, +userId);
}

@Get()
Expand Down
4 changes: 4 additions & 0 deletions src/lendings/lendings.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Module } from '@nestjs/common';
import { LendingsService } from './lendings.service';
import { LendingsController } from './lendings.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Lending } from './entities/lending.entity';

@Module({
controllers: [LendingsController],
providers: [LendingsService],
imports: [TypeOrmModule.forFeature([Lending])],
exports: [TypeOrmModule],
})
export class LendingsModule {}
22 changes: 8 additions & 14 deletions src/lendings/lendings.service.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import { Injectable } from '@nestjs/common';
import { UpdateLendingDto } from './dto/update-lending.dto';
import { getConnection } from 'typeorm';
import { Repository } from 'typeorm';
import { Lending } from './entities/lending.entity';
import { Book } from 'src/books/entities/book.entity';
import { User } from 'src/users/entities/user.entity';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class LendingsService {
async create() {
constructor(
@InjectRepository(Lending)
private readonly lendingsRepository: Repository<Lending>,
) {}

async create(bookId: number, userId: number) {
return 'This action adds a new lending';
}

findAll() {
const connection = getConnection();
connection
.getRepository(Lending)
.find({ relations: ['book'] })
.then((lendingData) => {});
return `This action returns all lendings`;
}

findOne(lendingId: number) {
const connection = getConnection();
connection
.getRepository(Lending)
.findOne({ where: { id: lendingId }, relations: ['book'] })
.then((bookData) => {});
return `This action returns a #${lendingId} lending`;
}

Expand Down
3 changes: 2 additions & 1 deletion src/returns/entities/return.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
OneToOne,
} from 'typeorm';

import { User } from '../../users/entities/user.entity';
Expand All @@ -24,7 +25,7 @@ export class Returning {
@UpdateDateColumn()
updatedAt: Date;

@ManyToOne(() => Lending, (lending) => lending.returnings)
@OneToOne(() => Lending, (lending) => lending.returning)
lending: Lending;

@ManyToOne(() => User, (user) => user.returnings)
Expand Down
19 changes: 11 additions & 8 deletions src/returns/returns.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ export class ReturnsController {
constructor(private readonly returnsService: ReturnsService) {}

@Post()
create(@Body() createReturnDto: CreateReturnDto) {
return this.returnsService.create(createReturnDto);
async create(@Query('bookId') bookId: string) {
return this.returnsService.create(+bookId);
}

@Get()
findAll() {
async findAll() {
return this.returnsService.findAll();
}

@Get()
findOne(@Query('bookId') bookId: string, @Query('cadetId') cadetId: string) {
return this.returnsService.findOne(+bookId, +cadetId);
@Get(':lendingId')
async findOne(@Param('lendingId') lendingId: string) {
return this.returnsService.findOne(+lendingId);
}

@Patch(':id')
update(@Param('id') id: string, @Body() updateReturnDto: UpdateReturnDto) {
async update(
@Param('id') id: string,
@Body() updateReturnDto: UpdateReturnDto,
) {
return this.returnsService.update(+id, updateReturnDto);
}

@Delete(':id')
remove(@Param('id') id: string) {
async remove(@Param('id') id: string) {
return this.returnsService.remove(+id);
}
}
4 changes: 4 additions & 0 deletions src/returns/returns.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Module } from '@nestjs/common';
import { ReturnsService } from './returns.service';
import { ReturnsController } from './returns.controller';
import { Returning } from './entities/return.entity';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
controllers: [ReturnsController],
providers: [ReturnsService],
imports: [TypeOrmModule.forFeature([Returning])],
exports: [TypeOrmModule],
})
export class ReturnsModule {}
25 changes: 13 additions & 12 deletions src/returns/returns.service.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import { Injectable } from '@nestjs/common';
import { Lending } from 'src/lendings/entities/lending.entity';
import { getConnection, getRepository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Connection, getConnection, getRepository, Repository } from 'typeorm';
import { CreateReturnDto } from './dto/create-return.dto';
import { UpdateReturnDto } from './dto/update-return.dto';
import { Returning } from './entities/return.entity';

@Injectable()
export class ReturnsService {
create(createReturnDto: CreateReturnDto) {
constructor(
@InjectRepository(Returning)
private readonly returnsRepository: Repository<Returning>,
) {}

async create(bookId: number) {
return 'This action adds a new return';
}

findAll() {
async findAll() {
return `This action returns all returns`;
}

findOne(bookId: number, cadetId: number) {
const connection = getConnection();
const lendingRepository = connection.getRepository(Lending);
lendingRepository
.createQueryBuilder()
.where('bookId:bookId AND userId:cadetId');
async findOne(lendingId: number) {
return `This action returns one returns`;
}

update(id: number, updateReturnDto: UpdateReturnDto) {
async update(id: number, updateReturnDto: UpdateReturnDto) {
return `This action updates a #${id} return`;
}

remove(id: number) {
async remove(id: number) {
return `This action removes a #${id} return`;
}
}
11 changes: 5 additions & 6 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ import {
// Delete,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
//import { UpdateUserDto } from './dto/update-user.dto';

@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}

@Post()
crate() {
return this.usersService.create();
}

@Get()
findAll() {
return this.usersService.findAll();
}

@Post()
create(@Body() createUserDto: CreateUserDto) {
return this.usersService.create(createUserDto);
}

// @Get(':id')
// findOne(@Param('id') id: string) {
// return this.usersService.findOne(+id);
Expand Down
3 changes: 1 addition & 2 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { ftTypes } from 'src/auth/auth.service';
import { UserRepository } from './user.repository';
import { User } from './entities/user.entity';
Expand Down Expand Up @@ -36,7 +35,7 @@ export class UsersService {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
create(createUserDto: CreateUserDto) {
async create() {
return 'This action adds a new user';
}

Expand Down