Model controllers added
This commit is contained in:
@@ -5,11 +5,19 @@ 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.
|
||||
* @returns The user access token.
|
||||
*/
|
||||
async register({ request }: HttpContext) {
|
||||
try {
|
||||
const data = await request.validateUsing(registerValidator)
|
||||
const user = await User.create(data)
|
||||
logger.info('User registered: %s', user.fullName, user.email, request.ip())
|
||||
logger.info('User registered: %s, %s, %s', user.fullName, user.email, request.ip())
|
||||
return User.accessTokens.create(user)
|
||||
}
|
||||
catch(error) {
|
||||
@@ -18,6 +26,13 @@ export default class AuthController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
async login({ request }: HttpContext) {
|
||||
try {
|
||||
const { email, password } = await request.validateUsing(loginValidator)
|
||||
@@ -31,6 +46,14 @@ export default class AuthController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deletes the user's access token and logs them out. If the user is not
|
||||
* authenticated, returns an unauthorized message.
|
||||
*/
|
||||
async logout({ auth }: HttpContext) {
|
||||
const user = auth.user!
|
||||
logger.info('User logout: %s', auth.getUserOrFail().email)
|
||||
@@ -44,6 +67,15 @@ export default class AuthController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the authenticated user's information if authenticated,
|
||||
* otherwise deletes the access token and returns an unauthorized message.
|
||||
*
|
||||
* @param auth - The HTTP context's auth object.
|
||||
* @returns An object containing the user data if authenticated,
|
||||
* otherwise an unauthorized message.
|
||||
*/
|
||||
|
||||
async me({ auth }: HttpContext) {
|
||||
if (await auth.check()) {
|
||||
return { user: auth.user }
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import RestaurantMenuItem from '#models/restaurant_menu_item'
|
||||
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.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.find(id)
|
||||
return item
|
||||
}
|
||||
catch (error) {
|
||||
logger.error('Menu item retrieval 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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// 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"
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user