-
Notifications
You must be signed in to change notification settings - Fork 0
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 #27 from prtcl/spike/project-details
spike/project details
- Loading branch information
Showing
43 changed files
with
3,987 additions
and
2,891 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { ConvexError, v } from 'convex/values'; | ||
import type { Doc, Id } from './_generated/dataModel'; | ||
import { internalMutation, type MutationCtx, query } from './_generated/server'; | ||
|
||
export const loadProjectDetails = query({ | ||
args: { projectId: v.id('projects') }, | ||
handler: async (ctx, { projectId }) => { | ||
const project = await ctx.db.get(projectId); | ||
|
||
if (!project || project.deletedAt !== null) { | ||
throw new ConvexError({ | ||
message: 'Project not found', | ||
code: 404, | ||
}); | ||
} | ||
|
||
const details = await ctx.db | ||
.query('details') | ||
.withIndex('project', (q) => q.eq('projectId', projectId)) | ||
.filter((q) => q.eq(q.field('deletedAt'), null)) | ||
.unique(); | ||
|
||
if (!details) { | ||
throw new ConvexError({ | ||
message: 'Project details not found', | ||
code: 404, | ||
}); | ||
} | ||
|
||
const { title } = project; | ||
const { coverImageId, embedId } = details; | ||
const coverImageUrl: string | null = coverImageId | ||
? await ctx.storage.getUrl(coverImageId) | ||
: null; | ||
const embed: Doc<'embeds'> | null = embedId | ||
? await ctx.db.get(embedId) | ||
: null; | ||
|
||
return { | ||
...details, | ||
coverImage: coverImageUrl | ||
? { | ||
alt: title, | ||
url: coverImageUrl, | ||
} | ||
: null, | ||
embed: embed | ||
? { | ||
...embed, | ||
title, | ||
} | ||
: null, | ||
}; | ||
}, | ||
}); | ||
|
||
const services = v.union( | ||
v.literal('bandcamp'), | ||
v.literal('youtube'), | ||
v.literal('soundcloud'), | ||
); | ||
|
||
const getOrInsertProjectDetails = async ( | ||
ctx: MutationCtx, | ||
projectId: Id<'projects'>, | ||
) => { | ||
let details = await ctx.db | ||
.query('details') | ||
.withIndex('project', (q) => q.eq('projectId', projectId)) | ||
.filter((q) => q.eq(q.field('deletedAt'), null)) | ||
.unique(); | ||
|
||
if (!details) { | ||
const insertedId = await ctx.db.insert('details', { | ||
content: null, | ||
coverImageId: null, | ||
deletedAt: null, | ||
embedId: null, | ||
projectId, | ||
}); | ||
details = await ctx.db.get(insertedId); | ||
} | ||
|
||
if (!details) { | ||
throw new ConvexError({ | ||
message: 'Details not found', | ||
code: 500, | ||
}); | ||
} | ||
|
||
return details; | ||
}; | ||
|
||
export const attachProjectEmbed = internalMutation({ | ||
args: { | ||
projectId: v.id('projects'), | ||
service: services, | ||
src: v.string(), | ||
}, | ||
handler: async (ctx, { projectId, service, src }) => { | ||
const project = await ctx.db.get(projectId); | ||
|
||
if (!project) { | ||
throw new ConvexError({ | ||
message: 'Project not found', | ||
code: 500, | ||
}); | ||
} | ||
|
||
const existingDetails = await getOrInsertProjectDetails(ctx, projectId); | ||
const existingEmbed = existingDetails?.embedId | ||
? await ctx.db.get(existingDetails?.embedId) | ||
: null; | ||
|
||
if (existingEmbed) { | ||
await ctx.db.delete(existingEmbed._id); | ||
} | ||
|
||
const embedId = await ctx.db.insert('embeds', { | ||
deletedAt: null, | ||
service, | ||
src, | ||
}); | ||
|
||
return await ctx.db.patch(existingDetails._id, { | ||
embedId, | ||
}); | ||
}, | ||
}); | ||
|
||
export const attachProjectCoverImage = internalMutation({ | ||
args: { | ||
coverImageId: v.id('_storage'), | ||
projectId: v.id('projects'), | ||
}, | ||
handler: async (ctx, { coverImageId, projectId }) => { | ||
const project = await ctx.db.get(projectId); | ||
|
||
if (!project) { | ||
throw new ConvexError({ | ||
message: 'Project not found', | ||
code: 500, | ||
}); | ||
} | ||
|
||
const existingCoverImage = await ctx.storage.getUrl(coverImageId); | ||
|
||
if (!existingCoverImage) { | ||
throw new ConvexError({ | ||
message: 'Cover image does not exist', | ||
code: 500, | ||
}); | ||
} | ||
|
||
const existingDetails = await getOrInsertProjectDetails(ctx, projectId); | ||
|
||
return await ctx.db.patch(existingDetails._id, { | ||
coverImageId, | ||
}); | ||
}, | ||
}); |
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
Oops, something went wrong.