Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from dcramer/ref/overhaul-api
ref: Overhaul the api to use jsonschema
- Loading branch information
Showing
81 changed files
with
1,607 additions
and
941 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,65 @@ | ||
import { Bottle, Entity, User } from "../../db/schema"; | ||
import { inArray } from "drizzle-orm"; | ||
import { Result, Serializer, serialize } from "."; | ||
import { db } from "../../db"; | ||
import { Bottle, User, bottlesToDistillers, entities } from "../../db/schema"; | ||
import { EntitySerializer } from "./entity"; | ||
|
||
export const serializeBottle = ( | ||
bottle: Bottle & { | ||
brand: Entity; | ||
distillers?: Entity[]; | ||
export const BottleSerializer: Serializer<Bottle> = { | ||
attrs: async (itemList: Bottle[], currentUser?: User) => { | ||
const itemIds = itemList.map((t) => t.id); | ||
|
||
const distillerList = await db | ||
.select() | ||
.from(bottlesToDistillers) | ||
.where(inArray(bottlesToDistillers.bottleId, itemIds)); | ||
|
||
const entityIds = Array.from( | ||
new Set([ | ||
...itemList.map((i) => i.brandId), | ||
...distillerList.map((d) => d.distillerId), | ||
]), | ||
); | ||
|
||
const entityList = await db | ||
.select() | ||
.from(entities) | ||
.where(inArray(entities.id, entityIds)); | ||
const entitiesById = Object.fromEntries( | ||
(await serialize(EntitySerializer, entityList, currentUser)).map( | ||
(data, index) => [entityList[index].id, data], | ||
), | ||
); | ||
|
||
const distillersByBottleId: { | ||
[bottleId: number]: Result; | ||
} = {}; | ||
distillerList.forEach((d) => { | ||
if (!distillersByBottleId[d.bottleId]) | ||
distillersByBottleId[d.bottleId] = [entitiesById[d.distillerId]]; | ||
else distillersByBottleId[d.bottleId].push(entitiesById[d.distillerId]); | ||
}); | ||
|
||
return Object.fromEntries( | ||
itemList.map((item) => { | ||
return [ | ||
item.id, | ||
{ | ||
brand: entitiesById[item.brandId], | ||
distillers: distillersByBottleId[item.id] || [], | ||
}, | ||
]; | ||
}), | ||
); | ||
}, | ||
|
||
item: (item: Bottle, attrs: Record<string, any>, currentUser?: User) => { | ||
return { | ||
id: `${item.id}`, | ||
name: item.name, | ||
statedAge: item.statedAge, | ||
category: item.category, | ||
brand: attrs.brand, | ||
distillers: attrs.distillers, | ||
}; | ||
}, | ||
currentUser?: User, | ||
) => { | ||
return { | ||
id: bottle.id, | ||
name: bottle.name, | ||
statedAge: bottle.statedAge, | ||
category: bottle.category, | ||
brand: bottle.brand, | ||
distillers: bottle.distillers || [], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Collection, User } from "../../db/schema"; | ||
|
||
import { Serializer } from "."; | ||
|
||
export const CollectionSerializer: Serializer<Collection> = { | ||
item: (item: Collection, attrs: Record<string, any>, currentUser?: User) => { | ||
return { | ||
id: `${item.id}`, | ||
name: item.name, | ||
createdAt: item.createdAt, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { inArray } from "drizzle-orm"; | ||
import { Serializer } from "."; | ||
import { db } from "../../db"; | ||
import { Comment, User, users } from "../../db/schema"; | ||
|
||
export const CommentSerializer: Serializer<Comment> = { | ||
attrs: async (itemList: Comment[], currentUser?: User) => { | ||
const usersById = Object.fromEntries( | ||
( | ||
await db | ||
.select() | ||
.from(users) | ||
.where( | ||
inArray( | ||
users.id, | ||
itemList.map((i) => i.createdById), | ||
), | ||
) | ||
).map((u) => [u.id, u]), | ||
); | ||
|
||
return Object.fromEntries( | ||
itemList.map((item) => { | ||
return [ | ||
item.id, | ||
{ | ||
createdBy: usersById[item.id], | ||
}, | ||
]; | ||
}), | ||
); | ||
}, | ||
|
||
item: (item: Comment, attrs: Record<string, any>, currentUser?: User) => { | ||
return { | ||
id: `${item.id}`, | ||
comments: item.comment, | ||
createdAt: item.createdAt, | ||
createdBy: attrs.createdBy, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Edition, User } from "../../db/schema"; | ||
|
||
import { Serializer } from "."; | ||
|
||
export const EditionSerializer: Serializer<Edition> = { | ||
item: (item: Edition, attrs: Record<string, any>, currentUser?: User) => { | ||
return { | ||
id: `${item.id}`, | ||
name: item.name, | ||
barrel: item.barrel, | ||
vintageYear: item.vintageYear, | ||
createdAt: item.createdAt, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Entity, User } from "../../db/schema"; | ||
|
||
import { Serializer } from "."; | ||
|
||
export const EntitySerializer: Serializer<Entity> = { | ||
item: (item: Entity, attrs: Record<string, any>, currentUser?: User) => { | ||
return { | ||
id: `${item.id}`, | ||
name: item.name, | ||
country: item.country, | ||
region: item.region, | ||
type: item.type, | ||
createdAt: item.createdAt, | ||
|
||
totalTastings: item.totalTastings, | ||
totalBottles: item.totalBottles, | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.