-
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.
feat: add CategoriesController and CategoriesService
- Loading branch information
1 parent
5233cf0
commit 9e56b44
Showing
8 changed files
with
169 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// import type { HttpContext } from '@adonisjs/core/http' | ||
|
||
import { inject } from '@adonisjs/core' | ||
import { HttpContext } from '@adonisjs/core/http' | ||
import { CategoriesService } from '../services/categories_service.js' | ||
|
||
@inject() | ||
export default class CategoriesController { | ||
constructor(private categoryService: CategoriesService) {} | ||
|
||
async getAll({ response }: HttpContext) { | ||
try { | ||
const categories = await this.categoryService.getAll() | ||
return response.json(categories) | ||
} catch (error) { | ||
return response.status(500).json({ message: 'Internal server error' }) | ||
} | ||
} | ||
} |
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,26 @@ | ||
export const categoriesMock = [ | ||
{ | ||
id: 1, | ||
name: 'Food', | ||
image: 'assets/img/categories/category_food.png', | ||
products: [], | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Cooking', | ||
image: 'assets/img/categories/category_kitchen.png', | ||
products: [], | ||
}, | ||
{ | ||
id: 3, | ||
name: 'Fashion', | ||
image: 'assets/img/categories/category_fashion.png', | ||
products: [], | ||
}, | ||
{ | ||
id: 4, | ||
name: 'Culture', | ||
image: 'assets/img/categories/category_culture.png', | ||
products: [], | ||
}, | ||
] |
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,72 @@ | ||
export const productsMock = [ | ||
{ | ||
id: 1, | ||
categoryID: 1, | ||
image: 'assets/img/products/caputo.png', | ||
title: "Caputo® - Farina TIP0 '00' Pizzeria", | ||
info: '1 kg - Il Mulino di Napoli', | ||
price: 12, | ||
isBestSeller: true, | ||
isFavorite: true, | ||
}, | ||
{ | ||
id: 2, | ||
categoryID: 1, | ||
image: 'assets/img/products/olive_oil.png', | ||
title: 'De Cecco Olio di Oliva', | ||
info: '1L - Extra Virgin', | ||
price: 22.95, | ||
isBestSeller: true, | ||
isFavorite: false, | ||
}, | ||
{ | ||
id: 3, | ||
categoryID: 3, | ||
image: 'assets/img/products/tshirt_girl.png', | ||
title: "T-Shirt 'Everybody Loves Italian Girls'", | ||
info: 'Sizes from S to XL', | ||
price: 19.99, | ||
isBestSeller: true, | ||
isFavorite: false, | ||
}, | ||
{ | ||
id: 4, | ||
categoryID: 1, | ||
image: 'assets/img/products/kimbo.png', | ||
title: 'Kimbo® Espresso Crema Intensa', | ||
info: '1kg - Caffè di Napoli', | ||
price: 4.99, | ||
isBestSeller: true, | ||
isFavorite: false, | ||
}, | ||
{ | ||
id: 5, | ||
categoryID: 1, | ||
image: 'assets/img/products/mascarpone.png', | ||
title: 'Galbani® Mascarpone', | ||
info: '250mg - Best for Tiramisù', | ||
price: 2.99, | ||
isBestSeller: false, | ||
isFavorite: false, | ||
}, | ||
{ | ||
id: 6, | ||
categoryID: 1, | ||
image: 'assets/img/products/bacio_latte.png', | ||
title: 'Baci® Latte Vellutato Scatola Bijou', | ||
info: '200g - quotes by famous poets inside!', | ||
price: 12.49, | ||
isBestSeller: false, | ||
isFavorite: false, | ||
}, | ||
{ | ||
id: 7, | ||
categoryID: 3, | ||
image: 'assets/img/products/scarf.png', | ||
title: 'Sciarpa rossa', | ||
info: '70% wool / 30% acrylic', | ||
price: 99.99, | ||
isBestSeller: false, | ||
isFavorite: false, | ||
}, | ||
] |
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,37 @@ | ||
import { inject } from '@adonisjs/core' | ||
import { categoriesMock } from '../mocks/categories_mock.js' | ||
import { productsMock } from '../mocks/products_mock.js' | ||
|
||
export interface Category { | ||
id: number | ||
name: string | ||
image: string | ||
products: Product[] | ||
} | ||
|
||
export interface Product { | ||
id: number | ||
categoryID: number | ||
image: string | ||
title: string | ||
info: string | ||
price: number | ||
isBestSeller: boolean | ||
isFavorite: boolean | ||
} | ||
|
||
@inject() | ||
export class CategoriesService { | ||
async getAll() { | ||
// Retrieve the categories from a mock json file | ||
const categories = categoriesMock as Category[] | ||
const products = productsMock as Product[] | ||
|
||
// Assign products to their respective categories | ||
categories.forEach((category) => { | ||
category.products = products.filter((product) => product.categoryID === category.id) | ||
}) | ||
|
||
return categories | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export const environment = { | ||
production: false, | ||
API_Endpoint : '../assets/mocks/', | ||
API_Endpoint : '/api/', | ||
}; |