70 lines
1.6 KiB
TypeScript
70 lines
1.6 KiB
TypeScript
import { mutation, query } from './_generated/server';
|
|
import { v } from 'convex/values';
|
|
import { getCurrentUserFunction } from './auth';
|
|
|
|
export const list = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
return await ctx.db.query('produtos').collect();
|
|
}
|
|
});
|
|
|
|
export const search = query({
|
|
args: { query: v.string() },
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db
|
|
.query('produtos')
|
|
.withSearchIndex('search_nome', (q) => q.search('nome', args.query))
|
|
.take(10);
|
|
}
|
|
});
|
|
|
|
export const create = mutation({
|
|
args: {
|
|
nome: v.string(),
|
|
valorEstimado: v.string(),
|
|
tipo: v.union(v.literal('servico'), v.literal('estrutura'), v.literal('insumo'))
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const user = await getCurrentUserFunction(ctx);
|
|
if (!user) throw new Error('Unauthorized');
|
|
|
|
return await ctx.db.insert('produtos', {
|
|
...args,
|
|
criadoPor: user._id,
|
|
criadoEm: Date.now()
|
|
});
|
|
}
|
|
});
|
|
|
|
export const update = mutation({
|
|
args: {
|
|
id: v.id('produtos'),
|
|
nome: v.string(),
|
|
valorEstimado: v.string(),
|
|
tipo: v.union(v.literal('servico'), v.literal('estrutura'), v.literal('insumo'))
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const user = await getCurrentUserFunction(ctx);
|
|
if (!user) throw new Error('Unauthorized');
|
|
|
|
await ctx.db.patch(args.id, {
|
|
nome: args.nome,
|
|
valorEstimado: args.valorEstimado,
|
|
tipo: args.tipo
|
|
});
|
|
}
|
|
});
|
|
|
|
export const remove = mutation({
|
|
args: {
|
|
id: v.id('produtos')
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const user = await getCurrentUserFunction(ctx);
|
|
if (!user) throw new Error('Unauthorized');
|
|
|
|
await ctx.db.delete(args.id);
|
|
}
|
|
});
|