diff --git a/src/users/user.entity.ts b/src/users/user.entity.ts new file mode 100644 index 0000000..fee0c6c --- /dev/null +++ b/src/users/user.entity.ts @@ -0,0 +1,11 @@ +import { Yacht } from '../yacht/yacht.entity'; + +export type User = { + userId: number; + firstName: string; + lastName: string; + phone: string; + email: string; + password: string; + yachts?: Yacht[]; +}; diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 3b06e1a..7343b39 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -1,39 +1,81 @@ import { Injectable, Logger } from '@nestjs/common'; - -export type User = { - userId: number; - firstName: string; - lastName: string; - phone: string; - email: string; - password: string; -}; +import { YachtService } from '../yacht/yacht.service'; +import { User } from './user.entity'; @Injectable() export class UsersService { private readonly logger = new Logger(UsersService.name); - private readonly users = [ + constructor(private readonly yachtsService: YachtService) {} + + private readonly users: User[] = [ { userId: 1, firstName: 'Ivan', lastName: 'Andreev', phone: '+79009009090', - email: 'emai@email.com', + email: 'email@email.com', password: 'admin', + yachts: [], }, { userId: 2, firstName: 'Sergey', lastName: 'Bolshakov', phone: '+79009009090', - email: 'emai1@email.com', + email: 'email1@email.com', password: 'admin', + yachts: [], }, ]; - async findOne(email: string): Promise { + async findOne( + email: string, + includeYachts = false, + ): Promise { this.logger.log({ email }); - return this.users.find((user) => user.email === email); + const user = this.users.find((user) => user.email === email); + + if (user && includeYachts) { + user.yachts = await this.yachtsService.findByUserId(user.userId); + } + + return user; + } + + async findById( + userId: number, + includeYachts = false, + ): Promise { + const user = this.users.find((user) => user.userId === userId); + + if (user && includeYachts) { + user.yachts = await this.yachtsService.findByUserId(user.userId); + } + + return user; + } + + async findAll(includeYachts = false): Promise { + const users = [...this.users]; + + if (includeYachts) { + for (const user of users) { + user.yachts = await this.yachtsService.findByUserId(user.userId); + } + } + + return users; + } + + async create(userData: Omit): Promise { + const newUser: User = { + userId: this.users.length + 1, + ...userData, + }; + + this.users.push(newUser); + this.logger.log(`Created new user: ${newUser.email}`); + return newUser; } } diff --git a/src/yacht/dto/create-yacht.dto.ts b/src/yacht/dto/create-yacht.dto.ts deleted file mode 100644 index 25ac48b..0000000 --- a/src/yacht/dto/create-yacht.dto.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { IsString, IsNumber, IsBoolean, IsUrl, Min } from 'class-validator'; - -export class CreateYachtDto { - @IsString() - name: string; - - @IsNumber() - @Min(1) - length: number; - - @IsNumber() - @Min(0) - speed: number; - - @IsNumber() - @Min(0) - minCost: number; - - @IsBoolean() - hasQuickRent: boolean; - - @IsUrl() - pictureUrl: string; -} diff --git a/src/yacht/dto/update-yacht.dto.ts b/src/yacht/dto/update-yacht.dto.ts deleted file mode 100644 index 702ca91..0000000 --- a/src/yacht/dto/update-yacht.dto.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { PartialType } from '@nestjs/mapped-types'; -import { CreateYachtDto } from './create-yacht.dto'; - -export class UpdateYachtDto extends PartialType(CreateYachtDto) {} diff --git a/src/yacht/yacht.controller.ts b/src/yacht/yacht.controller.ts index 6f5ee9c..3d99473 100644 --- a/src/yacht/yacht.controller.ts +++ b/src/yacht/yacht.controller.ts @@ -11,7 +11,7 @@ import { import { YachtService } from './yacht.service'; import { CreateYachtDto } from './dto/create-yacht.dto'; import { UpdateYachtDto } from './dto/update-yacht.dto'; -import { Yacht } from './entities/yacht.entity'; +import { Yacht } from './yacht.entity'; @Controller('yacht') // Routes will be /yacht export class YachtController { diff --git a/src/yacht/entities/yacht.entity.ts b/src/yacht/yacht.entity.ts similarity index 73% rename from src/yacht/entities/yacht.entity.ts rename to src/yacht/yacht.entity.ts index cc91567..0dc527d 100644 --- a/src/yacht/entities/yacht.entity.ts +++ b/src/yacht/yacht.entity.ts @@ -1,9 +1,12 @@ export class Yacht { id?: number; + userId: number; name: string; length: number; speed: number; minCost: number; hasQuickRent: boolean; pictureUrl: string; + createdAt: Date; + updatedAt: Date; } diff --git a/src/yacht/yacht.module.ts b/src/yacht/yacht.module.ts index f796615..a597ccc 100644 --- a/src/yacht/yacht.module.ts +++ b/src/yacht/yacht.module.ts @@ -1,9 +1,8 @@ import { Module } from '@nestjs/common'; import { YachtService } from './yacht.service'; -import { YachtController } from './yacht.controller'; @Module({ - controllers: [YachtController], providers: [YachtService], + exports: [YachtService], }) -export class YachtModule {} +export class YachtsModule {} diff --git a/src/yacht/yacht.service.ts b/src/yacht/yacht.service.ts index 164d0da..a64b0aa 100644 --- a/src/yacht/yacht.service.ts +++ b/src/yacht/yacht.service.ts @@ -1,53 +1,64 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; -import { Yacht } from './entities/yacht.entity'; -import { CreateYachtDto } from './dto/create-yacht.dto'; -import { UpdateYachtDto } from './dto/update-yacht.dto'; +import { Injectable } from '@nestjs/common'; +import { Yacht } from './yacht.entity'; @Injectable() export class YachtService { private yachts: Yacht[] = []; - private idCounter = 1; - create(createYachtDto: CreateYachtDto): Yacht { - const yacht: Yacht = { - id: this.idCounter++, - ...createYachtDto, - }; - this.yachts.push(yacht); - return yacht; - } - - getAll(): Yacht[] { + async findAll(): Promise { return this.yachts; } - getById(id: number): Yacht { - const yacht = this.yachts.find((y) => y.id === id); - if (!yacht) { - throw new NotFoundException(`Yacht with ID ${id} not found`); - } - return yacht; + async findById(yachtId: number): Promise { + return this.yachts.find((yacht) => yacht.id === yachtId); } - update(id: number, updateYachtDto: UpdateYachtDto): Yacht { - const index = this.yachts.findIndex((y) => y.id === id); + async findByUserId(userId: number): Promise { + return this.yachts.filter((yacht) => yacht.userId === userId); + } + + async create( + yachtData: Omit, + ): Promise { + const newYacht: Yacht = { + id: this.yachts.length + 1, + ...yachtData, + createdAt: new Date(), + updatedAt: new Date(), + }; + + this.yachts.push(newYacht); + return newYacht; + } + + async update( + yachtId: number, + updateData: Partial, + ): Promise { + const index = this.yachts.findIndex((yacht) => yacht.id === yachtId); + if (index === -1) { - throw new NotFoundException(`Yacht with ID ${id} not found`); + return undefined; } this.yachts[index] = { ...this.yachts[index], - ...updateYachtDto, + ...updateData, + updatedAt: new Date(), }; return this.yachts[index]; } - remove(id: number): void { - const index = this.yachts.findIndex((y) => y.id === id); + async delete(yachtId: number): Promise { + const index = this.yachts.findIndex((yacht) => yacht.id === yachtId); + if (index === -1) { - throw new NotFoundException(`Yacht with ID ${id} not found`); + return false; } + this.yachts.splice(index, 1); + + return true; } }