青衫productservice

青衫productservice,第1张

import { HttpClient } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;
import { Product } from ‘./product’;
@Injectable()
export class ProductService {
status: string[] = [‘OUTOFSTOCK’, ‘INSTOCK’, ‘LOWSTOCK’];
productNames: string[] = [
“Bamboo Watch”,
“Black Watch”,
“Blue Band”,
“Blue T-Shirt”,
“Bracelet”,
“Brown Purse”,
“Chakra Bracelet”,
“Galaxy Earrings”,
“Game Controller”,
“Gaming Set”,
“Gold Phone Case”,
“Green Earbuds”,
“Green T-Shirt”,
“Grey T-Shirt”,
“Headphones”,
“Light Green T-Shirt”,
“Lime Band”,
“Mini Speakers”,
“Painted Phone Case”,
“Pink Band”,
“Pink Purse”,
“Purple Band”,
“Purple Gemstone Necklace”,
“Purple T-Shirt”,
“Shoes”,
“Sneakers”,
“Teal T-Shirt”,
“Yellow Earbuds”,
“Yoga Mat”,
“Yoga Set”,
];
constructor(private http: HttpClient) { }
getProductsSmall() {
return this.http.get(‘assets/products-small.json’)
.toPromise()
.then(res => res.data)
.then(data => { return data; });
}
getProducts() {
return this.http.get(‘assets/products.json’)
.toPromise()
.then(res => res.data)
.then(data => { return data; });
}
getProductsWithOrdersSmall() {
return this.http.get(‘assets/products-orders-small.json’)
.toPromise()
.then(res => res.data)
.then(data => { return data; });
}
generatePrduct(): Product {
const product: Product = {
id: this.generateId(),
name: this.generateName(),
description: “Product Description”,
price: this.generatePrice(),
quantity: this.generateQuantity(),
category: “Product Category”,
inventoryStatus: this.generateStatus(),
rating: this.generateRating()
};
product.image = product.name.toLocaleLowerCase().split(/[ ,]+/).join(‘-’)+“.jpg”;;
return product;
}
generateId() {
let text = “”;
let possible = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”;
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
generateName() {
return this.productNames[Math.floor(Math.random() * Math.floor(30))];
}
generatePrice() {
return Math.floor(Math.random() * Math.floor(299)+1);
}
generateQuantity() {
return Math.floor(Math.random() * Math.floor(75)+1);
}
generateStatus() {
return this.status[Math.floor(Math.random() * Math.floor(3))];
}
generateRating() {
return Math.floor(Math.random() * Math.floor(5)+1);
}
}

export interface Product {
id?:string;
code?:string;
name?:string;
description?:string;
price?:number;
quantity?:number;
inventoryStatus?:string;
category?:string;
image?:string;
rating?:number;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/940167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存