Skip to content

Commit

Permalink
Merge pull request #179 from goodeats/177-designs-to-polymorphic-model
Browse files Browse the repository at this point in the history
designs to polymorphic model
  • Loading branch information
goodeats authored Jun 17, 2024
2 parents 4659479 + 31d4ad2 commit f0ae6b2
Show file tree
Hide file tree
Showing 45 changed files with 777 additions and 224 deletions.
4 changes: 2 additions & 2 deletions app/models/asset/image/image.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export interface IAssetImageFileData {
size: number
lastModified?: number
filename: string
fit?: IAssetImageFit
hideOnDraw?: boolean
}

// when adding attributes to an asset type,
// make sure it starts as optional or is set to a default value
// for when parsing the asset from the deserializer
export interface IAssetAttributesImage extends IAssetImageFileData {
altText?: string
fit?: IAssetImageFit
hideOnDraw?: boolean
}

export interface IAssetImageSrc {
Expand Down
4 changes: 2 additions & 2 deletions app/models/asset/image/image.update.fit.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { prisma } from '#app/utils/db.server'
import {
type IAssetImageFit,
type IAssetImage,
type IAssetImageFileData,
type IAssetAttributesImage,
} from './image.server'
import { stringifyAssetImageAttributes } from './utils'

Expand All @@ -32,7 +32,7 @@ export interface IAssetImageUpdateFitSubmission {
}

interface IAssetImageUpdateFitData {
attributes: IAssetImageFileData
attributes: IAssetAttributesImage
}

export const updateAssetImageFit = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { filterVisibleDesigns } from '#app/utils/design'
import { orderLinkedItems } from '#app/utils/linked-list.utils'
import { filterNonArrayRotates } from '#app/utils/rotate'
import { type IArtworkVersion } from '../artwork-version/artwork-version.server'
import { findManyDesignsWithType } from '../design/design.get.server'
import {
findManyDesignsWithType,
type IDesignWithPalette,
type IDesign,
type IDesignWithRotate,
Expand Down
2 changes: 1 addition & 1 deletion app/models/design-layer/design-layer.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { prisma } from '#app/utils/db.server'
import { filterVisibleDesigns } from '#app/utils/design'
import { orderLinkedItems } from '#app/utils/linked-list.utils'
import { filterNonArrayRotates } from '#app/utils/rotate'
import { findManyDesignsWithType } from '../design/design.get.server'
import {
findManyDesignsWithType,
type IDesignWithPalette,
type IDesign,
type IDesignWithRotate,
Expand Down
30 changes: 28 additions & 2 deletions app/models/design/design.get.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { z } from 'zod'
import { DesignTypeEnum } from '#app/schema/design'
import { zodStringOrNull } from '#app/schema/zod-helpers'
import { arrayOfIds, zodStringOrNull } from '#app/schema/zod-helpers'
import { prisma } from '#app/utils/db.server'
import { type IDesign, type IDesignWithType } from '../design/design.server'

export type queryDesignWhereArgsType = z.infer<typeof whereArgs>
const whereArgs = z.object({
id: z.string().optional(),
id: z.union([z.string(), arrayOfIds]).optional(),
type: z.nativeEnum(DesignTypeEnum).optional(),
selected: z.boolean().optional(),
ownerId: z.string().optional(),
artworkId: z.string().optional(),
artworkVersionId: z.string().optional(),
Expand Down Expand Up @@ -99,3 +100,28 @@ export const getDesignWithType = async ({
})
return design
}

export const findManyDesignsWithType = async ({
where,
}: {
where: queryDesignWhereArgsType
}): Promise<IDesignWithType[]> => {
validateQueryWhereArgsPresent(where)
const designs = await prisma.design.findMany({
where,
include: {
palette: true,
size: true,
fill: true,
stroke: true,
line: true,
rotate: true,
layout: true,
template: true,
},
orderBy: {
type: 'asc',
},
})
return designs
}
179 changes: 68 additions & 111 deletions app/models/design/design.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { type Design } from '@prisma/client'
import {
type selectArgsType,
type findDesignArgsType,
type whereArgsType,
type designTypeEnum,
} from '#app/schema/design'
import { prisma } from '#app/utils/db.server'
import { type DateOrString } from '#app/definitions/prisma-helper'
import { type designTypeEnum } from '#app/schema/design'
import {
type IArtworkVersion,
type IArtworkVersionWithChildren,
Expand All @@ -26,8 +21,73 @@ import { type IStrokeCreateOverrides } from '../design-type/stroke/stroke.create
import { type IStroke } from '../design-type/stroke/stroke.server'
import { type ITemplateCreateOverrides } from '../design-type/template/template.create.server'
import { type ITemplate } from '../design-type/template/template.server'
import { type ILayerWithChildren } from '../layer/layer.server'
import { type IUser } from '../user/user.server'
import {
type IDesignAttributesFill,
type IDesignFill,
} from './fill/fill.server'

// Omitting 'createdAt' and 'updatedAt' from the Design interface
// prisma query returns a string for these fields
// omit type string to ensure type safety with designTypeEnum
// omit attributes string so that extended design types can insert their own attributes
type BaseDesign = Omit<
Design,
'type' | 'attributes' | 'createdAt' | 'updatedAt'
>

export interface IDesign extends BaseDesign {
type: string
attributes: string
createdAt: DateOrString
updatedAt: DateOrString
}

// when adding attributes to a design type,
// make sure it starts as optional or is set to a default value
// for when parsing the design from the deserializer
export type IDesignAttributes = IDesignAttributesFill

export interface IDesignParsed extends BaseDesign {
type: designTypeEnum
attributes: IDesignAttributes
createdAt: DateOrString
updatedAt: DateOrString
}

// export type IDesignType = IDesignFill
// TODO: replace with this ^^
export type IDesignByType = {
designFills: IDesignFill[]
}

// export interface IDesignsByTypeWithType {
// type: designTypeEnum
// designs: IDesignType[]
// }
// TODO: replace with this ^^

export interface IDesign extends Design {}
export type IDesignParent = IArtworkVersionWithChildren | ILayerWithChildren

interface IDesignData {
visible: boolean
selected: boolean
}

export interface IDesignSubmission extends IDesignData {
userId: IUser['id']
}

export interface IDesignCreateData extends IDesignData {
ownerId: IUser['id']
type: designTypeEnum
attributes: IDesignAttributes
}

export interface IDesignUpdateData extends IDesignData {
attributes: IDesignAttributes
}

export type IDesignIdOrNull = IDesign['id'] | null | undefined

Expand Down Expand Up @@ -152,106 +212,3 @@ export interface ISelectedDesignsFiltered {
layout?: ILayout
template?: ITemplate
}

export const findManyDesignsWithType = async ({
where,
}: {
where: whereArgsType
}): Promise<IDesignWithType[]> => {
const designs = await prisma.design.findMany({
where,
include: {
palette: true,
size: true,
fill: true,
stroke: true,
line: true,
rotate: true,
layout: true,
template: true,
},
orderBy: {
type: 'asc',
},
})
return designs
}

export const findFirstDesign = async ({
where,
select,
}: findDesignArgsType): Promise<IDesign | null> => {
const design = await prisma.design.findFirst({
where,
select,
})
return design
}

export const findDesignByIdAndOwner = async ({
id,
ownerId,
select,
}: {
id: whereArgsType['id']
ownerId: whereArgsType['ownerId']
select?: selectArgsType
}): Promise<Design | null> => {
const where = { id, ownerId }
return await findFirstDesign({ where, select })
}

// only use in transactions
export const connectPrevAndNextDesigns = ({
prevId,
nextId,
}: {
prevId: IDesign['id']
nextId: IDesign['id']
}) => {
const connectNextToPrev = prisma.design.update({
where: { id: prevId },
data: { nextId },
})
const connectPrevToNext = prisma.design.update({
where: { id: nextId },
data: { prevId },
})
return [connectNextToPrev, connectPrevToNext]
}

export const updateDesignToHead = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { prevId: null },
})
}

export const updateDesignToTail = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { nextId: null },
})
}

export const updateDesignRemoveNodes = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { prevId: null, nextId: null },
})
}

export const updateDesignNodes = ({
id,
nextId,
prevId,
}: {
id: string
nextId: string | null
prevId: string | null
}) => {
return prisma.design.update({
where: { id },
data: { prevId, nextId },
})
}
54 changes: 54 additions & 0 deletions app/models/design/design.update.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,57 @@ export const updateDesignVisible = ({
data: { visible },
})
}

export const connectPrevAndNextDesigns = ({
prevId,
nextId,
}: {
prevId: IDesign['id']
nextId: IDesign['id']
}) => {
const connectNextToPrev = prisma.design.update({
where: { id: prevId },
data: { nextId },
})
const connectPrevToNext = prisma.design.update({
where: { id: nextId },
data: { prevId },
})
return [connectNextToPrev, connectPrevToNext]
}

export const updateDesignToHead = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { prevId: null },
})
}

export const updateDesignToTail = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { nextId: null },
})
}

export const updateDesignRemoveNodes = ({ id }: { id: IDesign['id'] }) => {
return prisma.design.update({
where: { id },
data: { prevId: null, nextId: null },
})
}

export const updateDesignNodes = ({
id,
nextId,
prevId,
}: {
id: string
nextId: string | null
prevId: string | null
}) => {
return prisma.design.update({
where: { id },
data: { prevId, nextId },
})
}
13 changes: 13 additions & 0 deletions app/models/design/fill/fill.delete.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { prisma } from '#app/utils/db.server'
import { type IDesignFill } from './fill.server'

export interface IDesignFillDeletedResponse {
success: boolean
message?: string
}

export const deleteDesignFill = ({ id }: { id: IDesignFill['id'] }) => {
return prisma.design.delete({
where: { id },
})
}
Loading

0 comments on commit f0ae6b2

Please sign in to comment.