|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { PublicOf } from '~/common'; |
| 3 | +import { ChangesOf } from '~/core/database/changes'; |
| 4 | +import { e, RepoFor } from '~/core/edgedb'; |
| 5 | +import { CreateFieldZone, FieldZone, UpdateFieldZone } from './dto'; |
| 6 | +import { FieldZoneRepository } from './field-zone.repository'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class FieldZoneEdgeDBRepository |
| 10 | + extends RepoFor(FieldZone, { |
| 11 | + hydrate: (zone) => ({ |
| 12 | + ...zone['*'], |
| 13 | + director: true, |
| 14 | + }), |
| 15 | + }).customize((cls) => { |
| 16 | + return class extends cls { |
| 17 | + async create(input: CreateFieldZone) { |
| 18 | + const created = e.insert(e.FieldZone, { |
| 19 | + name: input.name, |
| 20 | + director: e.cast(e.User, e.cast(e.uuid, input.directorId)), |
| 21 | + }); |
| 22 | + const query = e.select(created, this.hydrate); |
| 23 | + return await this.db.run(query); |
| 24 | + } |
| 25 | + |
| 26 | + async update( |
| 27 | + { id }: Pick<FieldZone, 'id'>, |
| 28 | + changes: ChangesOf<FieldZone, UpdateFieldZone>, |
| 29 | + ) { |
| 30 | + const zone = e.cast(e.FieldZone, e.cast(e.uuid, id)); |
| 31 | + const updated = e.update(zone, () => ({ |
| 32 | + set: { |
| 33 | + ...(changes.name && { name: changes.name }), |
| 34 | + ...(changes.directorId && { |
| 35 | + director: e.cast(e.User, e.cast(e.uuid, changes.directorId)), |
| 36 | + }), |
| 37 | + }, |
| 38 | + })); |
| 39 | + const query = e.select(updated, this.hydrate); |
| 40 | + return await this.db.run(query); |
| 41 | + } |
| 42 | + }; |
| 43 | + }) |
| 44 | + implements PublicOf<FieldZoneRepository> {} |
0 commit comments