74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
UploadedFile,
|
|
UseInterceptors,
|
|
Body,
|
|
UseGuards,
|
|
Res,
|
|
Req,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { Response } from 'express';
|
|
import { UploadService } from '../upload/upload.service';
|
|
import { ShopService } from './shop.service';
|
|
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
|
|
import { CreateShopDto } from './dto/creat-shop.dto';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { AuthenticatedRequest } from 'src/interfaces/request';
|
|
@Controller('shop')
|
|
export class ShopController {
|
|
constructor(
|
|
private readonly upload: UploadService,
|
|
private readonly shopService: ShopService,
|
|
) {}
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('/add')
|
|
@UseInterceptors(
|
|
FileInterceptor('Logo', {
|
|
limits: {
|
|
fileSize: 2 * 1024 * 1024, // 2MB
|
|
},
|
|
fileFilter: (req, file, cb) => {
|
|
const allowedMimeTypes = ['image/png', 'image/jpeg', 'image/jpg'];
|
|
if (!allowedMimeTypes.includes(file.mimetype)) {
|
|
cb(new BadRequestException('فقط فرمت PNG یا JPG مجاز است'), false);
|
|
} else {
|
|
cb(null, true);
|
|
}
|
|
},
|
|
}),
|
|
)
|
|
|
|
async create(
|
|
@Body() body:any,
|
|
@UploadedFile() file: Express.Multer.File,
|
|
@Req() req: AuthenticatedRequest,
|
|
@Res() res: Response,
|
|
) {
|
|
|
|
const shopData : CreateShopDto = {
|
|
...body,
|
|
Coordinates :JSON.parse(body.Coordinates),
|
|
Schedule :JSON.parse(body.Schedule),
|
|
Property:JSON.parse(body.Property)
|
|
}
|
|
|
|
let fileId: string | null = null;
|
|
|
|
if (file) {
|
|
const result = await this.upload.uploadFileSingle(file);
|
|
if (result.status !== 200) {
|
|
return res.status(400).json({
|
|
message: 'آپلود لوگو با خطا مواجه شد',
|
|
status: 400,
|
|
});
|
|
}
|
|
fileId = result.data._id;
|
|
}
|
|
|
|
const createdShop = await this.shopService.creatshop(shopData, fileId, req.user.userId);
|
|
return res.status(createdShop.status).json(createdShop);
|
|
}
|
|
}
|