stuff
This commit is contained in:
Regular → Executable
+13
-21
@@ -4,13 +4,12 @@ import User from '#models/user'
|
||||
import logger from '@adonisjs/core/services/logger'
|
||||
|
||||
export default class AuthController {
|
||||
|
||||
/**
|
||||
* Registers a user using the provided email, password, and full name.
|
||||
*
|
||||
* @bodyParam *email* - string | The email of the user.
|
||||
* @bodyParam *password* - string | The password of the user.
|
||||
* @bodyParam *fullName* - string | The full name of the user.
|
||||
*
|
||||
* @bodyParam *email* - string | The email of the user.
|
||||
* @bodyParam *password* - string | The password of the user.
|
||||
* @bodyParam *fullName* - string | The full name of the user.
|
||||
* @returns The user access token.
|
||||
*/
|
||||
async register({ request }: HttpContext) {
|
||||
@@ -19,16 +18,15 @@ export default class AuthController {
|
||||
const user = await User.create(data)
|
||||
logger.info('User registered: %s, %s, %s', user.fullName, user.email, request.ip())
|
||||
return User.accessTokens.create(user)
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Registration failed: %s',error)
|
||||
throw(error)
|
||||
} catch (error) {
|
||||
logger.error('Registration failed: %s', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log in a user using the provided email and password.
|
||||
*
|
||||
*
|
||||
* @bodyParam *email* - string | The email of the user.
|
||||
* @bodyParam *password* - string | The password of the user.
|
||||
* @returns The access token of the user.
|
||||
@@ -39,17 +37,12 @@ export default class AuthController {
|
||||
const user = await User.verifyCredentials(email, password)
|
||||
logger.info('User logged in: %s', email)
|
||||
return User.accessTokens.create(user)
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Login failed: %s',error)
|
||||
} catch (error) {
|
||||
logger.error('Login failed: %s', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deletes the user's access token and logs them out. If the user is not
|
||||
* authenticated, returns an unauthorized message.
|
||||
@@ -60,10 +53,9 @@ export default class AuthController {
|
||||
try {
|
||||
await User.accessTokens.delete(user, user.currentAccessToken.identifier)
|
||||
return { message: 'User logged out' }
|
||||
}
|
||||
catch(error){
|
||||
logger.error('Logout failed: %s',error)
|
||||
throw(error)
|
||||
} catch (error) {
|
||||
logger.error('Logout failed: %s', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Regular → Executable
+114
-102
@@ -1,120 +1,132 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import RestaurantMenuItem from '#models/restaurant_menu_item'
|
||||
import RestaurantMenu from '#models/restaurant_menu'
|
||||
import logger from '@adonisjs/core/services/logger'
|
||||
import { menuItemValidator } from '#validators/menu'
|
||||
|
||||
export default class RestaurantMenuItemsController {
|
||||
/**
|
||||
* Retrieve all restaurant menu items.
|
||||
*
|
||||
* @returns An array of all restaurant menu items.
|
||||
*/
|
||||
async getAll() {
|
||||
const items = await RestaurantMenuItem.query().preload('category').preload('menus')
|
||||
return items
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all restaurant menu items.
|
||||
*
|
||||
* @returns An array of all restaurant menu items.
|
||||
*/
|
||||
async getAll() {
|
||||
const items = await RestaurantMenuItem.all()
|
||||
return items
|
||||
/**
|
||||
* Retrieve a single restaurant menu item.
|
||||
*
|
||||
* @param request - The HTTP context containing the request data.
|
||||
* @returns The restaurant menu item with the specified id.
|
||||
*/
|
||||
|
||||
async getOne({ request }: HttpContext) {
|
||||
try {
|
||||
const id = request.body().id
|
||||
const item = await RestaurantMenuItem.query()
|
||||
.where('id', id)
|
||||
.preload('category')
|
||||
.preload('menus')
|
||||
.first()
|
||||
return item
|
||||
} catch (error) {
|
||||
logger.error('Menu item retrieval failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a single restaurant menu item.
|
||||
*
|
||||
* @param request - The HTTP context containing the request data.
|
||||
* @returns The restaurant menu item with the specified id.
|
||||
*/
|
||||
// The following methods require authentication. Log user activity.
|
||||
|
||||
async getOne({request}: HttpContext) {
|
||||
try {
|
||||
const id = request.body().id
|
||||
const item = await RestaurantMenuItem.find(id)
|
||||
return item
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu item retrieval failed: %s',error)
|
||||
}
|
||||
/**
|
||||
* Create a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *name* - string | The name of the menu item.
|
||||
* @bodyParam *priceSmall* - number | The price of the small serving of the menu item.
|
||||
* @bodyParam *priceLarge* - number | The price of the large serving of the menu item.
|
||||
* @bodyParam *description* - string | The description of the menu item.
|
||||
* @bodyParam *menu_id* - number | The menu id of the menu item.
|
||||
* @returns The created menu item.
|
||||
*/
|
||||
async create({ request, auth }: HttpContext) {
|
||||
const data = await request.validateUsing(menuItemValidator)
|
||||
try {
|
||||
const item = await RestaurantMenuItem.create(data)
|
||||
logger.info('Menu item created: %s by %s', item.name, auth.getUserOrFail().email)
|
||||
return item
|
||||
} catch (error) {
|
||||
logger.error('Menu item creation failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
// The following methods require authentication. Log user activity.
|
||||
|
||||
/**
|
||||
* Create a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *name* - string | The name of the menu item.
|
||||
* @bodyParam *priceSmall* - number | The price of the small serving of the menu item.
|
||||
* @bodyParam *priceLarge* - number | The price of the large serving of the menu item.
|
||||
* @bodyParam *description* - string | The description of the menu item.
|
||||
* @bodyParam *menu_id* - number | The menu id of the menu item.
|
||||
* @returns The created menu item.
|
||||
*/
|
||||
async create({request, auth}: HttpContext) {
|
||||
const data = await request.validateUsing(menuItemValidator)
|
||||
try {
|
||||
const item = await RestaurantMenuItem.create(data)
|
||||
logger.info('Menu item created: %s by %s', item.name, auth.getUserOrFail().email)
|
||||
return item
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Menu item creation failed: %s',error)
|
||||
}
|
||||
/**
|
||||
* Update a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *id* - number | The id of the menu item to update.
|
||||
* @bodyParam *name* - string | The new name of the menu item.
|
||||
* @bodyParam *priceSmall* - number | The new price of the small size of the menu item.
|
||||
* @bodyParam *priceLarge* - number | The new price of the large size of the menu item.
|
||||
* @bodyParam *description* - string | The new description of the menu item.
|
||||
* @bodyParam *menu_id* - number | The new menu id of the menu item.
|
||||
*/
|
||||
async update({ request, auth }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(data.id)
|
||||
item?.merge(data)
|
||||
await item?.save()
|
||||
logger.info('Menu item updated: %s by %s', item?.name, auth.getUserOrFail().email)
|
||||
} catch (error) {
|
||||
logger.error('Menu item update failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *id* - number | The id of the menu item to update.
|
||||
* @bodyParam *name* - string | The new name of the menu item.
|
||||
* @bodyParam *priceSmall* - number | The new price of the small size of the menu item.
|
||||
* @bodyParam *priceLarge* - number | The new price of the large size of the menu item.
|
||||
* @bodyParam *description* - string | The new description of the menu item.
|
||||
* @bodyParam *menu_id* - number | The new menu id of the menu item.
|
||||
*/
|
||||
async update({request, auth}: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(data.id)
|
||||
item?.merge(data)
|
||||
await item?.save()
|
||||
logger.info('Menu item updated: %s by %s', item?.name, auth.getUserOrFail().email)
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Menu item update failed: %s',error)
|
||||
}
|
||||
/**
|
||||
* Delete a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *id* - number | The id of the menu item to delete.
|
||||
*/
|
||||
async delete({ request, auth, response }: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(id)
|
||||
if (!item) {
|
||||
return response.notFound({ message: 'Menu item not found' })
|
||||
}
|
||||
// Detach from all menus first
|
||||
await item.related('menus').detach()
|
||||
await item.delete()
|
||||
logger.info('Menu item deleted: %s by %s', item.name, auth.getUserOrFail().email)
|
||||
return response.ok({ message: 'Menu item deleted' })
|
||||
} catch (error) {
|
||||
logger.error('Menu item deletion failed: %s', error)
|
||||
return response.internalServerError({ message: 'Menu item deletion failed' })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a restaurant menu item.
|
||||
*
|
||||
* @bodyParam *id* - number | The id of the menu item to delete.
|
||||
*/
|
||||
async delete({request, auth}: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(id)
|
||||
await item?.delete()
|
||||
logger.info('Menu item deleted: %s by %s', item?.name, auth.getUserOrFail().email)
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Menu item deletion failed: %s',error)
|
||||
}
|
||||
}
|
||||
// This method connects the menu item to a menu.
|
||||
|
||||
// This method connects the menu item to a menu.
|
||||
|
||||
/**
|
||||
* Connect a restaurant menu item to a restaurant menu.
|
||||
*
|
||||
* @bodyParam *item_id* - number | The id of the menu item to connect.
|
||||
* @bodyParam *menu_id* - number | The id of the menu to connect the menu item to.
|
||||
*/
|
||||
async connectToMenu({request}: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(data.item_id)
|
||||
const menu = await data.menu_id
|
||||
item?.merge({menu_id: menu})
|
||||
await item?.save()
|
||||
}
|
||||
catch(error) {
|
||||
logger.error('Menu item connection failed: %s',error)
|
||||
}
|
||||
/**
|
||||
* Connect a restaurant menu item to a restaurant menu.
|
||||
*
|
||||
* @bodyParam *item_id* - number | The id of the menu item to connect.
|
||||
* @bodyParam *menu_id* - number | The id of the menu to connect the menu item to.
|
||||
*/
|
||||
async connectToMenu({ request, response }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const item = await RestaurantMenuItem.find(data.item_id)
|
||||
const menu = await RestaurantMenu.find(data.menu_id)
|
||||
|
||||
if (!item || !menu) {
|
||||
return response.notFound({ message: 'Menu or Item not found' })
|
||||
}
|
||||
|
||||
await item.related('menus').attach([menu.id])
|
||||
return response.ok({ message: 'Menu item connected to menu' })
|
||||
} catch (error) {
|
||||
logger.error('Menu item connection failed: %s', error)
|
||||
return response.internalServerError({ message: 'Menu item connection failed' })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Regular → Executable
+87
-78
@@ -1,90 +1,99 @@
|
||||
// import type { HttpContext } from '@adonisjs/core/http'
|
||||
|
||||
import RestaurantMenu from "#models/restaurant_menu"
|
||||
import { HttpContext } from "@adonisjs/core/http"
|
||||
import logger from "@adonisjs/core/services/logger"
|
||||
import RestaurantMenu from '#models/restaurant_menu'
|
||||
import { HttpContext } from '@adonisjs/core/http'
|
||||
import logger from '@adonisjs/core/services/logger'
|
||||
|
||||
export default class RestaurantMenusController {
|
||||
|
||||
/**
|
||||
* Retrieve all restaurant menus.
|
||||
*
|
||||
* @returns An array of all restaurant menus.
|
||||
*/
|
||||
async getAll() {
|
||||
const menus = await RestaurantMenu.all()
|
||||
return menus
|
||||
}
|
||||
/**
|
||||
* Retrieve a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number The id of the menu to retrieve.
|
||||
* @returns The retrieved menu.
|
||||
*/
|
||||
async getOne({ request }: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const menu = await RestaurantMenu.find(id)
|
||||
return menu
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu retrieval failed: %s', error)
|
||||
}
|
||||
/**
|
||||
* Retrieve all restaurant menus.
|
||||
*
|
||||
* @returns An array of all restaurant menus.
|
||||
*/
|
||||
async getAll() {
|
||||
const menus = await RestaurantMenu.query().preload('items', (itemsQuery) => {
|
||||
itemsQuery.preload('category')
|
||||
})
|
||||
return menus
|
||||
}
|
||||
/**
|
||||
* Retrieve a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number The id of the menu to retrieve.
|
||||
* @returns The retrieved menu.
|
||||
*/
|
||||
async getOne({ request }: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const menu = await RestaurantMenu.query()
|
||||
.where('id', id)
|
||||
.preload('items', (itemsQuery) => {
|
||||
itemsQuery.preload('category')
|
||||
})
|
||||
.first()
|
||||
return menu
|
||||
} catch (error) {
|
||||
logger.error('Menu retrieval failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a restaurant menu.
|
||||
*
|
||||
* @bodyParam name string The name of the menu.
|
||||
* @bodyParam description string The description of the menu.
|
||||
* @returns The created menu.
|
||||
*/
|
||||
async create({ request, auth }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const menu = await RestaurantMenu.create(data)
|
||||
logger.info('Menu created: %s by %s', menu.name, auth.getUserOrFail().email)
|
||||
return menu
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu creation failed: %s', error)
|
||||
}
|
||||
/**
|
||||
* Create a restaurant menu.
|
||||
*
|
||||
* @bodyParam name string The name of the menu.
|
||||
* @bodyParam description string The description of the menu.
|
||||
* @returns The created menu.
|
||||
*/
|
||||
async create({ request, auth }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const menu = await RestaurantMenu.create(data)
|
||||
logger.info('Menu created: %s by %s', menu.name, auth.getUserOrFail().email)
|
||||
return menu
|
||||
} catch (error) {
|
||||
logger.error('Menu creation failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number The id of the menu to update.
|
||||
* @bodyParam name string The new name of the menu.
|
||||
*/
|
||||
async update({ request, auth }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const menu = await RestaurantMenu.find(data.id)
|
||||
menu?.merge(data)
|
||||
await menu?.save()
|
||||
logger.info('Menu updated: %s by %s', menu?.name, auth.getUserOrFail().email)
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu update failed: %s', error)
|
||||
}
|
||||
/**
|
||||
* Update a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number. The id of the menu to update.
|
||||
* @bodyParam name string. The new name of the menu.
|
||||
*/
|
||||
async update({ request, auth }: HttpContext) {
|
||||
const data = request.body()
|
||||
try {
|
||||
const menu = await RestaurantMenu.find(data.id)
|
||||
menu?.merge(data)
|
||||
await menu?.save()
|
||||
logger.info('Menu updated: %s by %s', menu?.name, auth.getUserOrFail().email)
|
||||
} catch (error) {
|
||||
logger.error('Menu update failed: %s', error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number. The id of the menu to delete.
|
||||
*/
|
||||
async delete({ request, auth, response }: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const menu = await RestaurantMenu.find(id)
|
||||
if (!menu) {
|
||||
return response.notFound({ message: 'Menu not found' })
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a restaurant menu.
|
||||
*
|
||||
* @bodyParam id number The id of the menu to delete.
|
||||
*/
|
||||
async delete({ request, auth }: HttpContext) {
|
||||
const id = request.body().id
|
||||
try {
|
||||
const menu = await RestaurantMenu.find(id)
|
||||
await menu?.delete()
|
||||
logger.info('Menu deleted: %s by %s', menu?.name, auth.getUserOrFail().email)
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu deletion failed: %s', error)
|
||||
}
|
||||
// Detach all menu items before deleting the menu
|
||||
await menu.related('items').detach()
|
||||
await menu.delete()
|
||||
logger.info('Menu deleted: %s by %s', menu.name, auth.getUserOrFail().email)
|
||||
return response.ok({ message: 'Menu deleted' })
|
||||
} catch (error) {
|
||||
logger.error('Menu deletion failed: %s', error)
|
||||
return response.internalServerError({ message: 'Menu deletion failed' })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
+13
-5
@@ -1,6 +1,6 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, column, hasMany } from '@adonisjs/lucid/orm'
|
||||
import type { HasMany } from '@adonisjs/lucid/types/relations'
|
||||
import { BaseModel, column, manyToMany } from '@adonisjs/lucid/orm'
|
||||
import type { ManyToMany } from '@adonisjs/lucid/types/relations'
|
||||
import RestaurantMenuItem from './restaurant_menu_item.js'
|
||||
|
||||
export default class RestaurantMenu extends BaseModel {
|
||||
@@ -16,6 +16,14 @@ export default class RestaurantMenu extends BaseModel {
|
||||
@column()
|
||||
declare name: string
|
||||
|
||||
@hasMany(() => RestaurantMenuItem)
|
||||
declare items: HasMany<typeof RestaurantMenuItem>
|
||||
}
|
||||
@column({ columnName: 'name_hu' })
|
||||
declare nameHu: string | null
|
||||
|
||||
@column({ columnName: 'name_en' })
|
||||
declare nameEn: string | null
|
||||
|
||||
@manyToMany(() => RestaurantMenuItem, {
|
||||
pivotTable: 'menu_item_menus',
|
||||
})
|
||||
declare items: ManyToMany<typeof RestaurantMenuItem>
|
||||
}
|
||||
|
||||
Regular → Executable
+25
-5
@@ -1,5 +1,8 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, column } from '@adonisjs/lucid/orm'
|
||||
import { BaseModel, column, belongsTo, manyToMany } from '@adonisjs/lucid/orm'
|
||||
import type { BelongsTo, ManyToMany } from '@adonisjs/lucid/types/relations'
|
||||
import Category from '#models/category'
|
||||
import RestaurantMenu from '#models/restaurant_menu'
|
||||
|
||||
export default class RestaurantMenuItem extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
@@ -14,6 +17,12 @@ export default class RestaurantMenuItem extends BaseModel {
|
||||
@column()
|
||||
declare name: string
|
||||
|
||||
@column({ columnName: 'name_hu' })
|
||||
declare nameHu: string | null
|
||||
|
||||
@column({ columnName: 'name_en' })
|
||||
declare nameEn: string | null
|
||||
|
||||
@column()
|
||||
declare priceSmall: number
|
||||
|
||||
@@ -23,9 +32,20 @@ export default class RestaurantMenuItem extends BaseModel {
|
||||
@column()
|
||||
declare description: string | null
|
||||
|
||||
//connet to restaurant_menu
|
||||
@column()
|
||||
declare menu_id: number | null
|
||||
@column({ columnName: 'description_hu' })
|
||||
declare descriptionHu: string | null
|
||||
|
||||
@column({ columnName: 'description_en' })
|
||||
declare descriptionEn: string | null
|
||||
|
||||
}
|
||||
@column({ columnName: 'category_id' })
|
||||
declare categoryId: number | null
|
||||
|
||||
@belongsTo(() => Category)
|
||||
declare category: BelongsTo<typeof Category>
|
||||
|
||||
@manyToMany(() => RestaurantMenu, {
|
||||
pivotTable: 'menu_item_menus',
|
||||
})
|
||||
declare menus: ManyToMany<typeof RestaurantMenu>
|
||||
}
|
||||
|
||||
Regular → Executable
+3
@@ -20,6 +20,9 @@ export default class User extends compose(BaseModel, AuthFinder) {
|
||||
@column()
|
||||
declare email: string
|
||||
|
||||
@column()
|
||||
declare role: string
|
||||
|
||||
@column({ serializeAs: null })
|
||||
declare password: string
|
||||
|
||||
|
||||
Regular → Executable
+2
-1
@@ -13,7 +13,8 @@ export const registerValidator = vine.compile(
|
||||
return !match
|
||||
}),
|
||||
password,
|
||||
fullName: vine.string()
|
||||
fullName: vine.string(),
|
||||
role: vine.string().optional(),
|
||||
})
|
||||
)
|
||||
export const loginValidator = vine.compile(
|
||||
|
||||
Regular → Executable
+12
-8
@@ -1,10 +1,14 @@
|
||||
import vine from "@vinejs/vine";
|
||||
import vine from '@vinejs/vine'
|
||||
|
||||
export const menuItemValidator = vine.compile(
|
||||
vine.object({
|
||||
name: vine.string(),
|
||||
priceSmall: vine.number(),
|
||||
priceLarge: vine.number(),
|
||||
description: vine.string(),
|
||||
})
|
||||
);
|
||||
vine.object({
|
||||
name: vine.string(),
|
||||
nameHu: vine.string(),
|
||||
nameEn: vine.string(),
|
||||
priceSmall: vine.number(),
|
||||
priceLarge: vine.number(),
|
||||
description: vine.string().optional(),
|
||||
descriptionHu: vine.string().optional(),
|
||||
descriptionEn: vine.string().optional(),
|
||||
})
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user