redo yacts controller
This commit is contained in:
parent
b19b7fef1b
commit
d7ff6b9517
|
|
@ -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[];
|
||||||
|
};
|
||||||
|
|
@ -1,39 +1,81 @@
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import { YachtService } from '../yacht/yacht.service';
|
||||||
export type User = {
|
import { User } from './user.entity';
|
||||||
userId: number;
|
|
||||||
firstName: string;
|
|
||||||
lastName: string;
|
|
||||||
phone: string;
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
private readonly logger = new Logger(UsersService.name);
|
private readonly logger = new Logger(UsersService.name);
|
||||||
|
|
||||||
private readonly users = [
|
constructor(private readonly yachtsService: YachtService) {}
|
||||||
|
|
||||||
|
private readonly users: User[] = [
|
||||||
{
|
{
|
||||||
userId: 1,
|
userId: 1,
|
||||||
firstName: 'Ivan',
|
firstName: 'Ivan',
|
||||||
lastName: 'Andreev',
|
lastName: 'Andreev',
|
||||||
phone: '+79009009090',
|
phone: '+79009009090',
|
||||||
email: 'emai@email.com',
|
email: 'email@email.com',
|
||||||
password: 'admin',
|
password: 'admin',
|
||||||
|
yachts: [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
userId: 2,
|
userId: 2,
|
||||||
firstName: 'Sergey',
|
firstName: 'Sergey',
|
||||||
lastName: 'Bolshakov',
|
lastName: 'Bolshakov',
|
||||||
phone: '+79009009090',
|
phone: '+79009009090',
|
||||||
email: 'emai1@email.com',
|
email: 'email1@email.com',
|
||||||
password: 'admin',
|
password: 'admin',
|
||||||
|
yachts: [],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
async findOne(email: string): Promise<User | undefined> {
|
async findOne(
|
||||||
|
email: string,
|
||||||
|
includeYachts = false,
|
||||||
|
): Promise<User | undefined> {
|
||||||
this.logger.log({ email });
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
import { PartialType } from '@nestjs/mapped-types';
|
|
||||||
import { CreateYachtDto } from './create-yacht.dto';
|
|
||||||
|
|
||||||
export class UpdateYachtDto extends PartialType(CreateYachtDto) {}
|
|
||||||
|
|
@ -11,7 +11,7 @@ import {
|
||||||
import { YachtService } from './yacht.service';
|
import { YachtService } from './yacht.service';
|
||||||
import { CreateYachtDto } from './dto/create-yacht.dto';
|
import { CreateYachtDto } from './dto/create-yacht.dto';
|
||||||
import { UpdateYachtDto } from './dto/update-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
|
@Controller('yacht') // Routes will be /yacht
|
||||||
export class YachtController {
|
export class YachtController {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
export class Yacht {
|
export class Yacht {
|
||||||
id?: number;
|
id?: number;
|
||||||
|
userId: number;
|
||||||
name: string;
|
name: string;
|
||||||
length: number;
|
length: number;
|
||||||
speed: number;
|
speed: number;
|
||||||
minCost: number;
|
minCost: number;
|
||||||
hasQuickRent: boolean;
|
hasQuickRent: boolean;
|
||||||
pictureUrl: string;
|
pictureUrl: string;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { YachtService } from './yacht.service';
|
import { YachtService } from './yacht.service';
|
||||||
import { YachtController } from './yacht.controller';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [YachtController],
|
|
||||||
providers: [YachtService],
|
providers: [YachtService],
|
||||||
|
exports: [YachtService],
|
||||||
})
|
})
|
||||||
export class YachtModule {}
|
export class YachtsModule {}
|
||||||
|
|
|
||||||
|
|
@ -1,53 +1,64 @@
|
||||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Yacht } from './entities/yacht.entity';
|
import { Yacht } from './yacht.entity';
|
||||||
import { CreateYachtDto } from './dto/create-yacht.dto';
|
|
||||||
import { UpdateYachtDto } from './dto/update-yacht.dto';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class YachtService {
|
export class YachtService {
|
||||||
private yachts: Yacht[] = [];
|
private yachts: Yacht[] = [];
|
||||||
private idCounter = 1;
|
|
||||||
|
|
||||||
create(createYachtDto: CreateYachtDto): Yacht {
|
async findAll(): Promise<Yacht[]> {
|
||||||
const yacht: Yacht = {
|
|
||||||
id: this.idCounter++,
|
|
||||||
...createYachtDto,
|
|
||||||
};
|
|
||||||
this.yachts.push(yacht);
|
|
||||||
return yacht;
|
|
||||||
}
|
|
||||||
|
|
||||||
getAll(): Yacht[] {
|
|
||||||
return this.yachts;
|
return this.yachts;
|
||||||
}
|
}
|
||||||
|
|
||||||
getById(id: number): Yacht {
|
async findById(yachtId: number): Promise<Yacht | undefined> {
|
||||||
const yacht = this.yachts.find((y) => y.id === id);
|
return this.yachts.find((yacht) => yacht.id === yachtId);
|
||||||
if (!yacht) {
|
|
||||||
throw new NotFoundException(`Yacht with ID ${id} not found`);
|
|
||||||
}
|
|
||||||
return yacht;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(id: number, updateYachtDto: UpdateYachtDto): Yacht {
|
async findByUserId(userId: number): Promise<Yacht[]> {
|
||||||
const index = this.yachts.findIndex((y) => y.id === id);
|
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) {
|
if (index === -1) {
|
||||||
throw new NotFoundException(`Yacht with ID ${id} not found`);
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.yachts[index] = {
|
this.yachts[index] = {
|
||||||
...this.yachts[index],
|
...this.yachts[index],
|
||||||
...updateYachtDto,
|
...updateData,
|
||||||
|
updatedAt: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.yachts[index];
|
return this.yachts[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(id: number): void {
|
async delete(yachtId: number): Promise<boolean> {
|
||||||
const index = this.yachts.findIndex((y) => y.id === id);
|
const index = this.yachts.findIndex((yacht) => yacht.id === yachtId);
|
||||||
|
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
throw new NotFoundException(`Yacht with ID ${id} not found`);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.yachts.splice(index, 1);
|
this.yachts.splice(index, 1);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue