-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from akbarsaputrait/5-api-owner---restaurant-t…
…ables Owner - Restaurant Table
- Loading branch information
Showing
14 changed files
with
177 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Quero } from '@core/decorators/quero.decorator'; | ||
import { Rest } from '@core/decorators/restaurant.decorator'; | ||
import { OwnerAuthGuard } from '@core/guards/auth.guard'; | ||
import { OwnerGuard } from '@core/guards/owner.guard'; | ||
import { PermAct, PermOwner } from '@core/services/role.service'; | ||
import { Location } from '@db/entities/owner/location.entity'; | ||
import { Restaurant } from '@db/entities/owner/restaurant.entity'; | ||
import { Table, TableStatus } from '@db/entities/owner/table.entity'; | ||
import { PlainTransformer } from '@db/transformers/plain.transformer'; | ||
import { TableTransformer } from '@db/transformers/table.transformer'; | ||
import { ValidationException } from '@lib/exceptions/validation.exception'; | ||
import { Validator } from '@lib/helpers/validator.helper'; | ||
import { Permissions } from '@lib/rbac'; | ||
import AppDataSource from '@lib/typeorm/datasource.typeorm'; | ||
import { BadRequestException, Body, Controller, Get, Param, Post, Put, Res, UseGuards } from '@nestjs/common'; | ||
|
||
@Controller() | ||
@UseGuards(OwnerAuthGuard()) | ||
export class TableController { | ||
@Get() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.R}`) | ||
async index(@Rest() rest, @Res() response, @Quero() quero) { | ||
const tables = AppDataSource.createQueryBuilder(Table, 't1'); | ||
tables.where({ restaurant_id: rest.id }); | ||
|
||
if (quero.location_id) { | ||
tables.andWhere({ location_id: quero.location_id }); | ||
} | ||
|
||
const data = await tables.search().sort().getPaged(); | ||
|
||
await response.paginate(data, PlainTransformer); | ||
} | ||
|
||
@Get('/:table_id') | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.R}`) | ||
async show(@Rest() rest, @Res() response, @Param() param) { | ||
const table = await Table.findOneByOrFail({ restaurant_id: rest.id, id: param.table_id }); | ||
await response.item(table, TableTransformer); | ||
} | ||
|
||
@Post() | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Table}@${PermAct.C}`) | ||
async create(@Rest() rest: Restaurant, @Body() body, @Res() response) { | ||
console.log(Object.values(TableStatus).join(',')); | ||
const rules = { | ||
number: 'required|unique|safe_text', | ||
location_id: 'required', | ||
status: `required|in:${Object.values(TableStatus).join(',')}`, | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
const loc = await Location.findOneByOrFail({ id: body.location_id }); | ||
|
||
const table = new Table(); | ||
table.number = body.number; | ||
table.status = body.status; | ||
table.location_id = loc.id; | ||
table.restaurant_id = rest.id; | ||
await table.save(); | ||
|
||
return response.item(table, TableTransformer); | ||
} | ||
|
||
@Put('/:table_id') | ||
@UseGuards(OwnerGuard) | ||
@Permissions(`${PermOwner.Location}@${PermAct.C}`) | ||
async update(@Rest() rest: Restaurant, @Body() body, @Res() response, @Param() param) { | ||
const rules = { | ||
number: 'required|unique|safe_text', | ||
location_id: 'required', | ||
status: `required|in:${Object.values(TableStatus).join(',')}`, | ||
}; | ||
const validation = Validator.init(body, rules); | ||
if (validation.fails()) { | ||
throw new ValidationException(validation); | ||
} | ||
|
||
if (!param.table_id) { | ||
throw new BadRequestException(); | ||
} | ||
|
||
const table = await Table.findOneByOrFail({ id: param.table_id }); | ||
table.number = body.number; | ||
table.status = body.status; | ||
table.location_id = body.location_id; | ||
table.restaurant_id = rest.id; | ||
await table.save(); | ||
|
||
return response.item(table, TableTransformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TableController } from './table.controller'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [TableController], | ||
providers: [], | ||
}) | ||
export class OwnerTableModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const Loc = createParamDecorator((data: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
|
||
return request.current.location || null; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ownerLocation1706111840738 implements MigrationInterface { | ||
name = 'ownerLocation1706111840738'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE \`owner\` ADD \`location_id\` varchar(26) NULL`); | ||
await queryRunner.query( | ||
`ALTER TABLE \`owner\` ADD CONSTRAINT \`FK_71cfe28bf443954332aca78d9e1\` FOREIGN KEY (\`location_id\`) REFERENCES \`location\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION` | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE \`owner\` DROP FOREIGN KEY \`FK_71cfe28bf443954332aca78d9e1\``); | ||
await queryRunner.query(`ALTER TABLE \`owner\` DROP COLUMN \`location_id\``); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { Owner } from '@db/entities/owner/owner.entity'; | ||
import { Location } from '@db/entities/owner/location.entity'; | ||
import { TransformerAbstract } from '@lib/transformer/abstract.transformer'; | ||
|
||
export class LocationTransformer extends TransformerAbstract { | ||
transform(entity: Owner) { | ||
transform(entity: Location) { | ||
return entity.toJSON(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Table } from '@db/entities/owner/table.entity'; | ||
import { TransformerAbstract } from '@lib/transformer/abstract.transformer'; | ||
|
||
export class TableTransformer extends TransformerAbstract { | ||
transform(entity: Table) { | ||
return entity.toJSON(); | ||
} | ||
} |