/** * Models */ import Category from 'App/Models/Category'; /** * Validators */ import CreateCategoryValidator from 'App/Validators/Category/CreateCategoryValidator'; export default class CategoryRepository { private Category: typeof Category; constructor() { this.Category = Category; } /** * Get all categories * * @returns Array of categories */ public async getAll(): Promise { const categories = await this.Category.query(); return categories; } /** * Get category by id * * @param id Category id * @returns Category or null */ public async getById(id: string | number): Promise { const category = await this.Category.query().where('id', id).first(); return category; } /** * Create new category * * @param data Data input * @returns Created category */ public async create(data: CreateCategoryValidator['schema']['props']): Promise { const category = await this.Category.create({ title: data.title, description: data.description, }); return category; } /** * Delete Category * * @param id Category id * @returns Deleted category or null */ public async delete(id: string | number): Promise { const category = await this.Category.query().where('id', id).first(); if (category) { await category.delete(); return category; } return null; } }