Commit 94d8232a authored by Omar Luna Hernández's avatar Omar Luna Hernández
Browse files

Se agrega la lógica para registrar y eliminar una categoría y obtener todas...

Se agrega la lógica para registrar y eliminar una categoría y obtener todas las categorías en la API
parent fad0cbba
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
import axios from "axios";
import { CategoryDatasourceInf } from "../../../infraestructure/datasources/category_datasource";
import { Category, CategoryFormValues } from "../../../infraestructure/entities/category";
import { CategoryModel } from "../../models/prod/CategoryModel";
import { APIUrl } from "../../../constants/api_url";

export class CategoryDatasourceProd implements CategoryDatasourceInf{
  async registerCategory(form: CategoryFormValues): Promise<void> {
    await axios.post(
      APIUrl + "/category",
      {
        nameES: form.nameES,
        nameEN: form.nameEN
      }
    );
  }
  
  async getCategories(): Promise<Category[]> {
    const {data: dataES} = await axios.get<CategoryModel[]>(APIUrl+'/category/ES');
    console.log(dataES);
    
    const {data: dataEN} = await axios.get<CategoryModel[]>(APIUrl+'/category/EN');
    const categories : Category[] = [];
    for(let i=0; i<dataES.length; i++){
      const category: Category = {
        idCategory: dataES[i].idCategory,
        nameES: dataES[i].name,
        nameEN: dataEN[i].name
      }
      categories.push(category);
    }

    return categories;
  }

  async deleteCategory(category: Category): Promise<void> {
    await axios.delete(APIUrl + `/category/${category.idCategory}`)
  }
}
 No newline at end of file
+21 −0
Original line number Diff line number Diff line
import { CategoryDatasourceInf } from "../../../infraestructure/datasources/category_datasource";
import { Category, CategoryFormValues } from "../../../infraestructure/entities/category";
import { CategoryRepositoryInf } from "../../../infraestructure/repositories/category_repository";

export class CategoryRepositoryProd implements CategoryRepositoryInf{
  constructor(
    private datasource: CategoryDatasourceInf
  ){}

  async registerCategory(form: CategoryFormValues): Promise<void> {
    return this.datasource.registerCategory(form);
  }

  async getCategories(): Promise<Category[]> {
    return this.datasource.getCategories();
  }

  async deleteCategory(category: Category): Promise<void> {
    return this.datasource.deleteCategory(category);
  }
}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
import { Category, CategoryFormValues } from "../entities/category";

export interface CategoryDatasourceInf{
  registerCategory(form: CategoryFormValues): Promise<void>;
  getCategories(): Promise<Category[]>;
  deleteCategory(category: Category): Promise<void>;
}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
import { Category, CategoryFormValues } from "../entities/category";

export interface CategoryRepositoryInf{
  registerCategory(form: CategoryFormValues): Promise<void>;
  getCategories(): Promise<Category[]>;
  deleteCategory(category: Category): Promise<void>;
}
 No newline at end of file