feat(recipe): añadidas funciones para ver recetas, añadir/eliminar favorita de…

feat(recipe): añadidas funciones para ver recetas, añadir/eliminar favorita de un usuario en stores y services
parent a49880b9
import api from './api';
export const recipeService = {
readAll: async (page, size, sortDirection) => {
const params = new URLSearchParams();
if (page != null) params.append('page', page);
if (size != null) params.append('size', size);
if (sortDirection != null) params.append('sortDirection', sortDirection);
const response = await api.get(`/recipe?${params.toString()}`);
return response.data;
},
madeFavorite: async (id) => {
const response = await api.patch(`/recipe/${id}/favorite`);
return response.data;
},
removeFavorite: async (id) => {
const response = await api.delete(`/recipe/${id}/favorite`);
return response.data;
},
readDetail: async (id) => {
const response = await api.get(`/recipe/${id}`);
return response.data;
}
}
\ No newline at end of file
import { defineStore } from "pinia";
import { recipeService } from "@/services/recipe";
export const useRecipeStore = defineStore('recipe', {
state: () => ({
recipe: null,
recipes: [],
currentPage: 0,
pageSize: 6,
totalElements: 0,
totalPages: 0,
//sortBy: 'createdAt',
sortDirection: 'desc'
}),
actions: {
async readAll(page, size, sortDirection) {
try {
const response = await recipeService.readAll(page, size, sortDirection);
this.recipes = response.content;
this.totalElements = response.totalElements;
this.totalPages = response.totalPages;
this.currentPage = response.number;
this.pageSize = size;
//this.sortBy = sortBy;
this.sortDirection = sortDirection;
} catch (error) {
console.error('Error al obtener la lista de recetas:', error);
this.recipes = [];
this.totalElements = 0;
this.totalPages = 0;
throw error;
}
},
async madeFavorite(id) {
try {
await recipeService.madeFavorite(id);
} catch (error) {
throw error;
}
},
async removeFavorite(id) {
try {
await recipeService.removeFavorite(id);
} catch (error) {
throw error;
}
},
async readDetail(id) {
try {
const response = await recipeService.readDetail(id);
this.recipe = response.content;
} catch (error) {
this.recipe = null;
throw error;
}
}
}
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment