Skip to content

Commit

Permalink
feat: add CategoriesController and CategoriesService
Browse files Browse the repository at this point in the history
  • Loading branch information
jvondermarck committed Apr 13, 2024
1 parent 5233cf0 commit 9e56b44
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 8 deletions.
19 changes: 19 additions & 0 deletions backend/app/controllers/categories_controller.ts
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' })
}
}
}
26 changes: 26 additions & 0 deletions backend/app/mocks/categories_mock.ts
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: [],
},
]
72 changes: 72 additions & 0 deletions backend/app/mocks/products_mock.ts
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,
},
]
37 changes: 37 additions & 0 deletions backend/app/services/categories_service.ts
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
}
}
5 changes: 5 additions & 0 deletions backend/start/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import router from '@adonisjs/core/services/router'
import { middleware } from './kernel.js'

const CategoriesController = () => import('#controllers/categories_controller')
const UsersController = () => import('#controllers/users_controller')

router.get('/', async () => 'It works!')
Expand All @@ -21,3 +22,7 @@ router
router.put('/', [UsersController, 'update']).middleware(middleware.jwt())
})
.prefix('auth')

router.group(() => {
router.get('/categories', [CategoriesController, 'getAll'])
})
6 changes: 1 addition & 5 deletions frontend/src/app/core/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ import { Category } from '../../shared/models/category.interface';
export class ApiService {

constructor(private http:HttpClient) { }

public getProducts() : Observable<Product[]> {
return this.http.get<Product[]>(environment.API_Endpoint + 'products.mock.json');
}

public getCategories() : Observable<Category[]> {
return this.http.get<Category[]>(environment.API_Endpoint + 'categories.mock.json');
return this.http.get<Category[]>(environment.API_Endpoint + 'categories');
}
}
10 changes: 8 additions & 2 deletions frontend/src/app/core/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import {Product} from "../../shared/models/product.interface";
import { ApiService } from './api.service';
import { BehaviorSubject, Observable, map } from 'rxjs';
import { CategoryService } from './category.service';
import { Category } from '../../shared/models/category.interface';

@Injectable({
providedIn: 'root'
Expand All @@ -11,8 +13,12 @@ export class ProductService {
private _bestSellersProducts = new BehaviorSubject<Product[]>([]);
private _products = new BehaviorSubject<Product[]>([]);

constructor(private apiService: ApiService) {
this.apiService.getProducts().subscribe((products: Product[]) => {
constructor(private categoryService: CategoryService) {
this.categoryService.categories.subscribe((categories: Category[]) => {
const products = categories.reduce((acc: Product[], category: Category) => {
return acc.concat(category.products);
}, []);

this._bestSellersProducts.next(products.filter((product: Product) => product.isBestSeller));
this._products.next(products);
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/environments/environment.ts
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/',
};

0 comments on commit 9e56b44

Please sign in to comment.