Browse Source

abstract class Service added

tags/v0.9.1
akimmig 4 years ago
parent
commit
0d6ecc64f9
10 changed files with 125 additions and 67 deletions
  1. +5
    -2
      server/schema.gql
  2. +7
    -13
      server/src/apparatus/apparatus.service.ts
  3. +13
    -11
      server/src/event/event.service.ts
  4. +4
    -1
      server/src/event/models/Event.ts
  5. +24
    -0
      server/src/event/resolver/event.mutation.ts
  6. +28
    -0
      server/src/event/resolver/event.ts
  7. +28
    -0
      server/src/global/service.ts
  8. +6
    -18
      server/src/organizer/organizer.service.ts
  9. +8
    -20
      server/src/person/person.service.ts
  10. +2
    -2
      server/src/person/resolver/person.mutation.ts

+ 5
- 2
server/schema.gql View File

@@ -52,9 +52,11 @@ scalar Time

type Event {
_id: UUID!
name: String
date: Date!
timeslots: [Timeslot!]!
_organizer: UUID!
organizer: Organizer!
}

"""A date string YYYY-MM-DD"""
@@ -72,9 +74,10 @@ type Query {
}

type Mutation {
login(token: String, passwort: String, email: String): Person
login(token: String, passwort: String, email: String): Person!
PersonRegister(passwort: String!, email: EmailAddress!, familyName: String!, givenName: String!, organizer: UUID!): Person!
ConfirmMail(confirmCode: String!, email: String!): Person
PersonConfirmMail(confirmCode: String!, email: String!): Person
ChangePassword(newPassword: String!, oldPassword: String!): Boolean!
OrganizerRegister(ort: String, plz: Int, name: String!): Organizer!
EventCreate(name: String, date: Date!, organizer: UUID!): Event!
}

+ 7
- 13
server/src/apparatus/apparatus.service.ts View File

@@ -1,22 +1,16 @@
import { Injectable } from '@nestjs/common';
import { db } from '../db';
import { Apparatus } from './models/Apparatus';
import { Client } from '../client';
import { UUID } from '../global/scalars/UUID';
import { Service } from '../global/service'

@Injectable()
export class ApparatusService {
async findOneById(id: UUID): Promise<Apparatus> {
const data = await db.fetch('apparatus', { _id: id });

return data?.[0] || null as Apparatus;
}

async find(filter: any, limit?: number, offset?: number): Promise<Apparatus[]> {
return db.fetch('apparatus', filter, limit, offset);
export class ApparatusService extends Service<Apparatus> {
constructor() {
super();
this.collection = 'apparatus';
}

async update(id: UUID, ops: any, filter: any, client: Client): Promise<Apparatus> {
return db.doOps('apparatus', id, ops, filter, client)
async create(client: Client): Promise<Apparatus> {
return null;
}
}

+ 13
- 11
server/src/event/event.service.ts View File

@@ -1,22 +1,24 @@
import { Injectable } from '@nestjs/common';
import { db } from '../db';
import { Event } from './models/Event';
import { Client } from '../client';
import { UUID } from '../global/scalars/UUID';
import { Date } from '../global/scalars/Date';
import { Service } from '../global/service'

@Injectable()
export class EventService {
async findOneById(id: UUID): Promise<Event> {
const data = await db.fetch('event', { _id: id });

return data?.[0] || null as Event;
export class EventService extends Service<Event> {
constructor() {
super();
this.collection = 'event';
}

async find(filter: any, limit?: number, offset?: number): Promise<Event[]> {
return db.fetch('event', filter, limit, offset);
}
async create(client: Client, date: Date, organizer: UUID, name?: string): Promise<Event> {
const neu = {
name: name || null,
date,
_organizer: organizer,
}

async update(id: UUID, ops: any, filter: any, client: Client): Promise<Event> {
return db.doOps('event', id, ops, filter, client)
return super.insert(client, neu);
}
}

+ 4
- 1
server/src/event/models/Event.ts View File

@@ -9,6 +9,9 @@ export class Event {
@Field(() => UUID,{ nullable: false })
_id: UUID

@Field(() => String, { nullable: true })
name?: string

@Field(() => Date, { nullable: false })
date: Date

@@ -16,5 +19,5 @@ export class Event {
timeslots: Timeslot[]

@Field(() => UUID, { nullable: false })
_organizer: Organizer
_organizer: UUID
}

+ 24
- 0
server/src/event/resolver/event.mutation.ts View File

@@ -3,10 +3,34 @@ import { Event } from '../models/Event';
import { Client } from '../../client';
import { EventService } from '../event.service';
import { HttpException } from '@nestjs/common';
import {Date} from '../../global/scalars/Date'
import {UUID} from '../../global/scalars/UUID'
import {OrganizerService} from '../../organizer/organizer.service'

@Resolver(() => Event)
export class EventResolverM {
constructor(
private readonly service: EventService,
) {}

@Mutation(() => Event, { nullable: false })
async EventCreate(
@Context('client') client: Client,
@Args('organizer') organizer: UUID,
@Args('date') date: Date,
@Args('name', { nullable: true }) name?: string,
): Promise<Event> {
if (!client.isMaster() && !client.isOrganizer(organizer)) {
throw new HttpException('You\'re not allowed to create an Event for this organizer', 403);
}

const organizerService = new OrganizerService();
const o = await organizerService.findOneById(organizer);

if (!o) {
throw new HttpException('Organizer-ID not found!', 404);
}

return this.service.create(client, date, organizer, name);
}
}

+ 28
- 0
server/src/event/resolver/event.ts View File

@@ -4,6 +4,8 @@ import { Client } from '../../client';
import { UUID } from '../../global/scalars/UUID';
import {Date} from '../../global/scalars/Date'
import {Timeslot} from '../models/Timeslot'
import {Organizer} from '../../organizer/models/Organizer'
import {OrganizerService} from '../../organizer/organizer.service'

@Resolver(() => Event)
export class EventResolver {
@@ -15,6 +17,14 @@ export class EventResolver {
return parent._id as UUID;
}

@ResolveField(() => String, { nullable: true })
async name(
@Context('client') client: Client,
@Parent() parent: Event
): Promise<string> {
return parent.name;
}

@ResolveField(() => Date, { nullable: false })
async date(
@Context('client') client: Client,
@@ -30,4 +40,22 @@ export class EventResolver {
): Promise<Timeslot[]> {
return parent.timeslots;
}

@ResolveField(() => UUID, { nullable: false })
async _organizer(
@Context('client') client: Client,
@Parent() parent: Event
): Promise<UUID> {
return parent._organizer;
}

@ResolveField(() => Organizer, { nullable: false })
async organizer(
@Context('client') client: Client,
@Parent() parent: Event
): Promise<Organizer> {
const organizerService = new OrganizerService();

return organizerService.findOneById(parent._organizer);
}
}

+ 28
- 0
server/src/global/service.ts View File

@@ -0,0 +1,28 @@
import { db } from '../db';
import { Client } from '../client';
import { UUID } from '../global/scalars/UUID';
import { v4 as uuid } from 'uuid';

export abstract class Service<E> {
protected collection: string = '';

public async findOneById(id: UUID): Promise<E> {
const data = await db.fetch(this.collection, { _id: id });

return data?.[0] || null as E;
}

public async find(filter: any, limit?: number, offset?: number): Promise<E[]> {
return db.fetch(this.collection, filter, limit, offset);
}

public async update(client: Client, id: UUID, ops: any, filter?: any): Promise<E> {
return db.doOps(this.collection, id, ops, filter, client);
}

protected async insert(client: Client, doc: any): Promise<E> {
doc._id = uuid();

return db.insert(this.collection, doc, client);
}
}

+ 6
- 18
server/src/organizer/organizer.service.ts View File

@@ -1,34 +1,22 @@
import { Injectable } from '@nestjs/common';
import { db } from '../db';
import { Organizer } from './models/Organizer';
import { Client } from '../client';
import { UUID } from '../global/scalars/UUID';
import { v4 as uuid } from 'uuid';
import {Service} from '../global/service'

@Injectable()
export class OrganizerService {
async findOneById(id: UUID): Promise<Organizer> {
const data = await db.fetch('organizer', { _id: id });

return data?.[0] || null as Organizer;
}

async find(filter: any, limit?: number, offset?: number): Promise<Organizer[]> {
return db.fetch('organizer', filter, limit, offset);
}

async update(client: Client, id: UUID, ops: any, filter?: any): Promise<Organizer> {
return db.doOps('organizer', id, ops, filter, client);
export class OrganizerService extends Service<Organizer> {
constructor() {
super();
this.collection = 'organizer';
}

async create(client: Client, name: string, plz?: number, ort?: string): Promise<Organizer> {
const neu = {
_id: uuid(),
name,
plz: plz || null,
ort: ort || null,
}

return db.insert('organizer', neu, client);
return super.insert(client, neu);
}
}

+ 8
- 20
server/src/person/person.service.ts View File

@@ -1,32 +1,20 @@
import { Injectable } from '@nestjs/common';
import { db } from '../db';
import { Person } from './models/Person';
import { Client } from '../client';
import { UUID } from '../global/scalars/UUID';
import { Organizer } from '../organizer/models/Organizer'
import { v4 as uuid } from 'uuid';
import {EmailAddress} from '../global/scalars/EmailAddress'
import {secureHash} from '../generate'
import { EmailAddress } from '../global/scalars/EmailAddress'
import { secureHash } from '../generate'
import { Service } from '../global/service'

@Injectable()
export class PersonService {
async findOneById(id: UUID): Promise<Person> {
const data = await db.fetch('person', { _id: id });

return data?.[0] || null as Person;
}

async find(filter: any, limit?: number, offset?: number): Promise<Person[]> {
return db.fetch('person', filter, limit, offset);
}

async update(client: Client, id: UUID, ops: any, filter?: any): Promise<Person> {
return db.doOps('person', id, ops, filter, client)
export class PersonService extends Service<Person> {
constructor() {
super();
this.collection = 'person';
}

async create(client: Client, givenName: string, familyName: string, email: EmailAddress, passwort: string): Promise<Person> {
const neu = {
_id: uuid(),
givenName,
familyName,
email,
@@ -34,6 +22,6 @@ export class PersonService {
confirmCode: uuid(),
}

return db.insert('person', neu, client);
return super.insert(client, neu);
}
}

+ 2
- 2
server/src/person/resolver/person.mutation.ts View File

@@ -14,7 +14,7 @@ export class PersonResolverM {
private readonly service: PersonService,
) {}

@Mutation(() => Person, { nullable: true })
@Mutation(() => Person, { nullable: false })
async login(
@Context('client') client: Client,
@Args('email', { nullable: true }) email?: string,
@@ -71,7 +71,7 @@ export class PersonResolverM {
}

@Mutation(() => Person, { nullable: true })
async ConfirmMail(
async PersonConfirmMail(
@Context('client') client: Client,
@Args('email') email: string,
@Args('confirmCode') confirmCode: string,

Loading…
Cancel
Save