Skip to content

Commit

Permalink
feat: add school and charity repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Smith committed Feb 24, 2024
1 parent 4b5dd08 commit b581bcd
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
14 changes: 14 additions & 0 deletions appsync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export type CharityProfile = {
request?: Maybe<ProfileItems>;
};

export type CharityUser = {
__typename?: 'CharityUser';
firstName: Scalars['String']['output'];
lastName: Scalars['String']['output'];
name: Scalars['String']['output'];
};

export type JoinRequest = {
__typename?: 'JoinRequest';
email: Scalars['String']['output'];
Expand Down Expand Up @@ -162,6 +169,13 @@ export type SchoolProfile = {
request?: Maybe<ProfileItems>;
};

export type SchoolUser = {
__typename?: 'SchoolUser';
firstName: Scalars['String']['output'];
lastName: Scalars['String']['output'];
name: Scalars['String']['output'];
};

export type SignUpData = {
__typename?: 'SignUpData';
email: Scalars['String']['output'];
Expand Down
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ type LocalAuthorityUser {
notes: String
}

type SchoolUser {
name: String!
firstName: String!
lastName: String!
}

type CharityUser {
name: String!
firstName: String!
lastName: String!
}

type LocalAuthority {
code: String!
name: String!
Expand Down
62 changes: 62 additions & 0 deletions src/repository/charityUserRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Collection, Db, Filter, MongoClient, WithId } from 'mongodb';
import { CharityUser } from '../../appsync';

export class CharityUserRepository {
private static instance: CharityUserRepository;
private readonly client: MongoClient;
private readonly db: Db;
private readonly collection: Collection<CharityUser>;

private constructor() {
this.client = new MongoClient(
process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/',
{
auth: {
username: process?.env?.MONGODB_ADMIN_USERNAME,
password: process?.env?.MONGODB_ADMIN_PASSWORD,
},
}
);
this.db = this.client.db('D2E');
this.collection = this.db.collection<CharityUser>('CharityUser');
}

static getInstance(): CharityUserRepository {
if (!this.instance) {
this.instance = new CharityUserRepository();
}
return this.instance;
}

private async getByQuery(query: Filter<CharityUser>): Promise<WithId<CharityUser>[]> {
const cursor = this.collection.find(query);

if (!(await cursor.hasNext())) {
return [];
}

return await cursor.toArray();
}

private async getOne(query: Filter<CharityUser>): Promise<WithId<CharityUser> | undefined> {
const result = await this.collection.findOne(query);

if (!result) {
return undefined;
}

return result;
}

public async getByEmail(email: string): Promise<WithId<CharityUser> | undefined> {
return await this.getOne({ email });
}

public async list(): Promise<WithId<CharityUser>[]> {
return await this.getByQuery({});
}

public async insert(charity: CharityUser): Promise<boolean> {
return (await this.collection.insertOne(charity)).acknowledged;
}
}
62 changes: 62 additions & 0 deletions src/repository/schoolUserRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Collection, Db, Filter, MongoClient, WithId } from 'mongodb';
import { SchoolUser } from '../../appsync';

export class SchoolUserRepository {
private static instance: SchoolUserRepository;
private readonly client: MongoClient;
private readonly db: Db;
private readonly collection: Collection<SchoolUser>;

private constructor() {
this.client = new MongoClient(
process?.env?.MONGODB_CONNECTION_STRING ?? 'mongodb://localhost:27017/',
{
auth: {
username: process?.env?.MONGODB_ADMIN_USERNAME,
password: process?.env?.MONGODB_ADMIN_PASSWORD,
},
}
);
this.db = this.client.db('D2E');
this.collection = this.db.collection<SchoolUser>('SchoolUser');
}

static getInstance(): SchoolUserRepository {
if (!this.instance) {
this.instance = new SchoolUserRepository();
}
return this.instance;
}

private async getByQuery(query: Filter<SchoolUser>): Promise<WithId<SchoolUser>[]> {
const cursor = this.collection.find(query);

if (!(await cursor.hasNext())) {
return [];
}

return await cursor.toArray();
}

private async getOne(query: Filter<SchoolUser>): Promise<WithId<SchoolUser> | undefined> {
const result = await this.collection.findOne(query);

if (!result) {
return undefined;
}

return result;
}

public async getByEmail(email: string): Promise<WithId<SchoolUser> | undefined> {
return await this.getOne({ email });
}

public async list(): Promise<WithId<SchoolUser>[]> {
return await this.getByQuery({});
}

public async insert(school: SchoolUser): Promise<boolean> {
return (await this.collection.insertOne(school)).acknowledged;
}
}

0 comments on commit b581bcd

Please sign in to comment.