redo yacts controller

This commit is contained in:
Иван 2025-12-14 15:53:47 +03:00
parent b19b7fef1b
commit d7ff6b9517
8 changed files with 112 additions and 74 deletions

11
src/users/user.entity.ts Normal file
View File

@ -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[];
};

View File

@ -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<User | undefined> {
async findOne(
email: string,
includeYachts = false,
): Promise<User | undefined> {
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<User | undefined> {
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<User[]> {
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<User, 'userId'>): Promise<User> {
const newUser: User = {
userId: this.users.length + 1,
...userData,
};
this.users.push(newUser);
this.logger.log(`Created new user: ${newUser.email}`);
return newUser;
}
}

View File

@ -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;
}

View File

@ -1,4 +0,0 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateYachtDto } from './create-yacht.dto';
export class UpdateYachtDto extends PartialType(CreateYachtDto) {}

View File

@ -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 {

View File

@ -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;
}

View File

@ -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 {}

View File

@ -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<Yacht[]> {
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<Yacht | undefined> {
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<Yacht[]> {
return this.yachts.filter((yacht) => yacht.userId === userId);
}
async create(
yachtData: Omit<Yacht, 'yachtId' | 'createdAt' | 'updatedAt'>,
): Promise<Yacht> {
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<Yacht>,
): Promise<Yacht | undefined> {
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<boolean> {
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;
}
}