Skip to content

Commit

Permalink
API: Add Database manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalvarezz committed Jun 29, 2020
1 parent 7d7356a commit bb8b26d
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 117 deletions.
13 changes: 0 additions & 13 deletions data/098323ks.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/2zd33b8c.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/6trfgkkj.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/7bcr49em.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/b8uthe2y.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/ewxsd6xb.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/fpr72m9k.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/t345w48y.json

This file was deleted.

13 changes: 0 additions & 13 deletions data/t9dv25gs.json

This file was deleted.

130 changes: 130 additions & 0 deletions database/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const data: Record<TProductId, TProduct> = {
'2zd33b8c': {
name: 'Maluma Hass Avocado',
id: '2zd33b8c',
sku: 'NUR72KCM',
price: 1.15,
image: '/images/maluma.jpg',
attributes: {
description:
'A relatively new cultivar, it was, the pretty boy baby, discovered in South Africa in the early 1990s by Mr. A.G. (Dries) Joubert. Maluma Babyy. It is a chance seedling of unknown parentage',
shape: 'Oval',
hardiness: '1 °C',
taste: 'Catchy, is an avocado',
},
},
'6trfgkkj': {
name: 'Fuerte Avocado',
id: '6trfgkkj',
sku: 'AX4M8SJV',
price: 1.21,
image: '/images/fuerte.jpg',
attributes: {
description:
'The Fuerte avocado is the second largest commercial variety behind Hass. It is a long time California commercial variety valued for its winter season ripening and its B-Type blossom type which most growers plant adjacent to the Hass for a more consistent production cycle. This avocado tends to produce heavily in alternate years.',
shape: 'Pear',
hardiness: '-1 °C',
taste: 'Magnificent, is a strong avocado',
},
},
'7bcr49em': {
name: 'Gwen Hass Avocado',
id: '7bcr49em',
sku: 'HYA78F6J',
price: 1.25,
image: '/images/gwen.jpg',
attributes: {
description:
"A seedling bred from 'Hass' x 'Thille' in 1982, 'Gwen' is higher yielding and more dwarfing than 'Hass' in California. The fruit has an oval shape, slightly smaller than 'Hass' (100–200 g or 3.5–7.1 oz), with a rich, nutty flavor. The skin texture is more finely pebbled than 'Hass', and is dull green when ripe. It is frost-hardy down to −1 °C (30 °F)",
shape: 'Plump',
hardiness: '−1 °C',
taste: 'Superb, is an avocado',
},
},
'098323ks': {
name: 'Bacon Avocado',
id: '098323ks',
sku: 'BXD100BLK',
price: 1.51,
image: '/images/bacon.jpg',
attributes: {
description:
'Developed by a farmer, James Bacon, in 1954, Bacon has medium-sized fruit with smooth, green skin with yellow-green, light-tasting flesh. When ripe, the skin remains green, but darkens slightly, and fruit yields to gentle pressure. It is cold-hardy down to −5 °C (23 °F)',
shape: 'Oval',
hardiness: '−5 °C',
taste: 'Creamy, is an avocado',
},
},
b8uthe2y: {
name: 'Hass Avocado',
id: 'b8uthe2y',
sku: 'RMRCZN7E',
price: 1.39,
image: '/images/hass.jpg',
attributes: {
description:
"The 'Hass' is the most common cultivar of avocado. It produces fruit year-round and accounts for 80% of cultivated avocados in the world.[21][55] All 'Hass' trees are descended from a single 'mother tree' raised by a mail carrier named Rudolph Hass, of La Habra Heights, California.[20][55] Hass patented the productive tree in 1935. The 'mother tree', of uncertain subspecies, died of root rot and was cut down in September 2002.[21][55][56] 'Hass' trees have medium-sized (150–250 g or 5.3–8.8 oz), ovate fruit with a black, pebbled skin. The flesh has a nutty, rich flavor with 19% oil. A hybrid Guatemalan type can withstand temperatures to −1 °C (30 °F)",
shape: 'Oval',
hardiness: '−1 °C',
taste: 'Gorgeous, is an avocado',
},
},
ewxsd6xb: {
name: 'Lamb Hass Avocado',
id: 'ewxsd6xb',
sku: 'N55229ZA',
price: 1.34,
image: '/images/lamb.jpg',
attributes: {
description:
'The Lamb Hass avocado is a cross between a Hass and Gwen avocado. The fruits are larger in size and later maturing than Hass. It is gaining in popularity as a commercial and backyard variety due to its exceptional flavor and easy peeling qualities. The tree has an upright, compact habit.',
shape: 'Obovate',
hardiness: '-2 °C',
taste: 'Great, is an avocado',
},
},
fpr72m9k: {
name: 'Pinkerton Avocado',
id: 'fpr72m9k',
sku: 'B4HZ42TM',
price: 1.27,
image: '/images/pinkerton.jpg',
attributes: {
description:
"First grown on the Pinkerton Ranch in Saticoy, California, in the early 1970s, 'Pinkerton' is a seedling of 'Hass' x 'Rincon'. The large fruit has a small seed, and its green skin deepens in color as it ripens. The thick flesh has a smooth, creamy texture, pale green color, good flavor, and high oil content. It shows some cold tolerance, to −1 °C (30 °F) and bears consistently heavy crops. A hybrid Guatemalan type, it has excellent peeling characteristics",
shape: 'Long pear',
hardiness: '−1 °C',
taste: 'Marvelous, is an avocado',
},
},
t9dv25gs: {
name: 'Reed Avocado',
id: 't9dv25gs',
sku: 'ZY3T9XXC',
price: 1.18,
image: '/images/reed.jpg',
attributes: {
description:
'Developed from a chance seedling found in 1948 by James S. Reed in California, this cultivar has large, round, green fruit with a smooth texture and dark, thick, glossy skin. Smooth and delicate, the flesh has a slightly nutty flavor. The skin ripens green. A Guatemalan type, it is hardy to −1 °C (30 °F). Tree size is about 5 by 4 m (16.4 by 13.1 ft)',
shape: 'Round',
hardiness: '−1 °C',
taste: 'Exquisite, is an avocado',
},
},
t345w48y: {
name: 'Zutano Avocado',
id: 't345w48y',
sku: 'MW79ZZ6Y',
price: 1.25,
image: '/images/zutano.jpg',
attributes: {
description:
'The Zutano avocado is a cold hardy, consistent producing avocado variety. It resembles the Fuerte in appearance but is less flavorful but more cold hardy. The green fruits are obovate in shape with waxy bumps on the skin. The flesh has a low oil but high water content which causes it to have a more fibrous texture.',
shape: 'Pear',
hardiness: '-5 °C',
taste: 'Splendid, is an avocado',
},
},
}

export default data
36 changes: 36 additions & 0 deletions database/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Oh you curious...
// This is not a real database,
// But let's imagine it is one :)
import allData from './data'

class Database {
constructor() {}

async getAll(): Promise<TProduct[]> {
const asArray = Object.values(allData)
await randomDelay()
return asArray
}

async getById(id: string): Promise<TProduct | null> {
if (!Object.prototype.hasOwnProperty.call(allData, id)) {
return null
}

const entry = allData[id]
await randomDelay()
return entry
}
}

// Let's also add a delay to make it a bit closer to reality
const randomDelay = () =>
new Promise((resolve) => {
const max = 350
const min = 100
const delay = Math.floor(Math.random() * (max - min + 1)) + min

setTimeout(resolve, delay)
})

export default Database
34 changes: 34 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type Url = string
type Json =
| string
| number
| boolean
| null
| { [property: string]: Json }
| Json[]

type TProductId = string

type TProductAttributes = {
description: string
shape: string
hardiness: string
taste: string
}

type TProduct = {
id: TProductId
name: string
sku: string
price: number
image: Url
attributes: TProductAttributes
}

type TAPIAVODetailResponse = TProduct

type TAPIAvoResponse = {
lenght: number
data: TProduct[]
error?: string
}
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@database": ["database/db.ts"]
},
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": false,
Expand Down

0 comments on commit bb8b26d

Please sign in to comment.