Skip to content

Commit

Permalink
- Add list, tree actions to category model api
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaro-canepa committed Jan 13, 2024
1 parent c2f65bf commit 2a4b32d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
4 changes: 3 additions & 1 deletion packages/shopaholic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@planetadeleste/pinia-orm-shopaholic",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"repository": {
"type": "git",
Expand Down Expand Up @@ -47,7 +47,9 @@
"vitest": "^1.1.3"
},
"peerDependencies": {
"@pinia-orm/axios": "^1.7.0",
"@planetadeleste/pinia-orm-core": "^1.0.0",
"axios": "^1.6.5",
"pinia-orm": "^1.7.2"
},
"packageManager": "[email protected]"
Expand Down
35 changes: 22 additions & 13 deletions packages/shopaholic/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
import Model from "@planetadeleste/pinia-orm-core";
import type { BrandData, CategoryData, CurrencyData, OfferData, ProductData } from "./types";
import Model from "@planetadeleste/pinia-orm-core";
import type {
BrandData,
CategoryData,
CurrencyData,
OfferData,
ProductData,
} from "./types";

// BRAND
interface Brand extends BrandData {
}
interface Brand extends BrandData {}

declare class Brand extends Model<BrandData> {}

// CATEGORY
interface Category extends CategoryData {
}
interface Category extends CategoryData {}

declare class Category extends Model<CategoryData> {}

// CURRENCY
interface Currency extends CurrencyData {
}
interface Currency extends CurrencyData {}

declare class Currency extends Model<CurrencyData> {}

// OFFER
interface Offer extends OfferData {
}
interface Offer extends OfferData {}

declare class Offer extends Model<OfferData> {}

// PRODUCT
interface Product extends ProductData {
}
interface Product extends ProductData {}

declare class Product extends Model<ProductData> {}

export { Brand, Category, Currency, Offer, Product };

export type { BrandData, CategoryData, CurrencyData, OfferData, ProductData } from "./types";
export type {
BrandData,
CategoryData,
CategoryApiAxiosRepository,
CategoryAxiosRepository,
CurrencyData,
OfferData,
ProductData,
} from "./types";
32 changes: 24 additions & 8 deletions packages/shopaholic/src/models/Category.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import Model from '@planetadeleste/pinia-orm-core';

import type { ModelFields } from 'pinia-orm';
import type { Result } from "@planetadeleste/pinia-orm-core";
import Model from "@planetadeleste/pinia-orm-core";
import type { Request } from "@pinia-orm/axios";
import type { ModelFields } from "pinia-orm";
import type { AxiosResponse } from "axios";
import type { CategoryData } from "../types";

class Category extends Model<CategoryData> {
static entity = 'sha_categories';
static baseUrl = 'categories';
static namespace = 'shopaholic';
static entity = "sha_categories";
static baseUrl = "categories";
static namespace = "shopaholic";
static config = {
axiosApi: {
actions: {
async list(this: Request) {
const obResponse = await this.get(`${Category.baseUrl}/list`);
return obResponse.response as AxiosResponse<Result<CategoryData[]>>;
},
async tree(this: Request) {
const obResponse = await this.get(`${Category.baseUrl}/tree`);
return obResponse.response as AxiosResponse<Result<CategoryData[]>>;
},
},
},
};

static fields(): ModelFields {
return {
id: this.attr(''),
id: this.attr(""),
parent_id: this.attr(null),
company_id: this.attr(null),
active: this.boolean(true),
Expand All @@ -25,7 +41,7 @@ class Category extends Model<CategoryData> {
created_at: this.attr(null),
updated_at: this.attr(null),

children: this.hasMany(Category, 'parent_id').onDelete('cascade'),
children: this.hasMany(Category, "parent_id").onDelete("cascade"),
};
}
}
Expand Down
13 changes: 12 additions & 1 deletion packages/shopaholic/src/types/Category.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { FileData } from "@planetadeleste/pinia-orm-core";
import type { AxiosRepository, Request } from "@pinia-orm/axios";
import type { Result, FileData } from "@planetadeleste/pinia-orm-core";
import type { AxiosResponse } from "axios";
import type Category from "../models/Category";

export interface CategoryData {
Expand All @@ -18,3 +20,12 @@ export interface CategoryData {
external_id: string;
children: Category[];
}

export interface CategoryAxiosRepository extends Request {
list(): Promise<AxiosResponse<Result<CategoryData[]>>>;
tree(): Promise<AxiosResponse<Result<CategoryData[]>>>;
}

export interface CategoryApiAxiosRepository extends AxiosRepository<Category> {
api(): CategoryAxiosRepository;
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a4b32d

Please sign in to comment.