|
| 1 | +import type { ManufacturerRequestType } from '@stacksjs/orm' |
| 2 | +import type { ManufacturerJsonResponse, NewManufacturer } from '../../../../orm/src/models/Manufacturer' |
| 3 | +import { db } from '@stacksjs/database' |
| 4 | + |
| 5 | +/** |
| 6 | + * Create a new product manufacturer |
| 7 | + * |
| 8 | + * @param request The manufacturer data to store |
| 9 | + * @returns The newly created manufacturer record |
| 10 | + */ |
| 11 | +export async function store(request: ManufacturerRequestType): Promise<ManufacturerJsonResponse | undefined> { |
| 12 | + // Validate the request data |
| 13 | + await request.validate() |
| 14 | + |
| 15 | + const manufacturerData: NewManufacturer = { |
| 16 | + manufacturer: request.get<string>('manufacturer'), |
| 17 | + description: request.get<string>('description'), |
| 18 | + country: request.get<string>('country'), |
| 19 | + featured: request.get<boolean>('featured') || false, |
| 20 | + } |
| 21 | + |
| 22 | + try { |
| 23 | + // Insert the manufacturer record |
| 24 | + const createdManufacturer = await db |
| 25 | + .insertInto('manufacturers') |
| 26 | + .values(manufacturerData) |
| 27 | + .executeTakeFirst() |
| 28 | + |
| 29 | + // If insert was successful, retrieve the newly created manufacturer |
| 30 | + if (createdManufacturer.insertId) { |
| 31 | + const manufacturer = await db |
| 32 | + .selectFrom('manufacturers') |
| 33 | + .where('id', '=', Number(createdManufacturer.insertId)) |
| 34 | + .selectAll() |
| 35 | + .executeTakeFirst() |
| 36 | + |
| 37 | + return manufacturer |
| 38 | + } |
| 39 | + |
| 40 | + return undefined |
| 41 | + } |
| 42 | + catch (error) { |
| 43 | + if (error instanceof Error) { |
| 44 | + // Check for duplicate entries if you have unique constraints |
| 45 | + if (error.message.includes('Duplicate entry')) { |
| 46 | + throw new Error('A manufacturer with this name already exists') |
| 47 | + } |
| 48 | + |
| 49 | + throw new Error(`Failed to create manufacturer: ${error.message}`) |
| 50 | + } |
| 51 | + |
| 52 | + throw error |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Create multiple manufacturers at once |
| 58 | + * |
| 59 | + * @param requests Array of manufacturer data to store |
| 60 | + * @returns Number of manufacturers created |
| 61 | + */ |
| 62 | +export async function bulkStore(requests: ManufacturerRequestType[]): Promise<number> { |
| 63 | + if (!requests.length) |
| 64 | + return 0 |
| 65 | + |
| 66 | + let createdCount = 0 |
| 67 | + |
| 68 | + try { |
| 69 | + // Process each manufacturer |
| 70 | + await db.transaction().execute(async (trx) => { |
| 71 | + for (const request of requests) { |
| 72 | + // Validate request data |
| 73 | + await request.validate() |
| 74 | + |
| 75 | + // Prepare manufacturer data |
| 76 | + const manufacturerData: NewManufacturer = { |
| 77 | + manufacturer: request.get<string>('manufacturer'), |
| 78 | + description: request.get<string>('description'), |
| 79 | + country: request.get<string>('country'), |
| 80 | + featured: request.get<boolean>('featured') || false, |
| 81 | + } |
| 82 | + |
| 83 | + await trx |
| 84 | + .insertInto('manufacturers') |
| 85 | + .values(manufacturerData) |
| 86 | + .executeTakeFirst() |
| 87 | + |
| 88 | + createdCount++ |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + return createdCount |
| 93 | + } |
| 94 | + catch (error) { |
| 95 | + if (error instanceof Error) { |
| 96 | + throw new TypeError(`Failed to create manufacturers in bulk: ${error.message}`) |
| 97 | + } |
| 98 | + |
| 99 | + throw error |
| 100 | + } |
| 101 | +} |
0 commit comments