Skip to content

Commit

Permalink
added user data model and amended news
Browse files Browse the repository at this point in the history
  • Loading branch information
kashw2 committed Dec 22, 2020
1 parent 5627366 commit 986a2ff
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 5 deletions.
1 change: 0 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
}
},
"packages": [
".",
"./libs/util",
"./libs/external",
"./libs/typescript"
Expand Down
2 changes: 2 additions & 0 deletions libs/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './misc/json-keys';
export * from './models/user';
export * from './models/news';
14 changes: 14 additions & 0 deletions libs/typescript/src/misc/json-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const idKey = 'id';
export const userKey = 'user';
export const ccKey = 'cc';
export const titleKey = 'title';
export const contentKey = 'content';
export const dateKey = 'date';
export const usernameKey = 'username';
export const localeKey = 'locale';
export const avatarKey = 'avatar';
export const discordIdKey = 'discord_id';
export const discordDiscriminatorKey = 'discord_discriminator';
export const groupKey = 'group';
export const permissionsKey = 'permissions';
export const memberSinceKey = 'member_since';
59 changes: 55 additions & 4 deletions libs/typescript/src/models/news.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
import {JsonBuilder, JsonSerializer} from '@ffk/lib-util';
import {JsonBuilder, JsonSerializer, parseDate, parseSetSerialized, parseString} from '@ffk/lib-util';
import {None, Option} from 'funfix-core';
import {Moment} from 'moment';
import {User, UserJsonSerializer} from './user';
import {Set} from 'immutable';
import {ccKey, contentKey, dateKey, idKey, titleKey, userKey} from '../misc/json-keys';

export class News {

constructor(
private id: Option<string> = None,
private user: Option<User> = None,
private cc: Set<User> = Set(),
private title: Option<string> = None,
private content: Option<string> = None,
private date: Option<Moment> = None,
) {
}

public getId(): Option<string> {
return this.id;
}

public getUser(): Option<User> {
return this.user;
}

public getCc(): Set<User> {
return this.cc;
}

public getTitle(): Option<string> {
return this.title;
}

public getContent(): Option<string> {
return this.content;
}

public getDate(): Option<Moment> {
return this.date;
}

}

export class NewsJsonSerializer extends JsonSerializer<News> {

static instance: NewsJsonSerializer = new NewsJsonSerializer();

fromJson(json: any): News {
return new News();
return new News(
parseString(json[idKey]),
UserJsonSerializer.instance.fromJsonImpl(json[userKey]),
parseSetSerialized(json[ccKey], UserJsonSerializer.instance),
parseString(json[titleKey]),
parseString(json[contentKey]),
parseDate(json[dateKey]),
);
}

toJson(value: News, builder: JsonBuilder): object {
return {};
return builder.addOptional(value.getId(), idKey)
.addOptionalSerialized(value.getUser(), userKey, UserJsonSerializer.instance)
.addIterableSerialized(value.getCc(), ccKey, UserJsonSerializer.instance)
.addOptional(value.getTitle(), titleKey)
.addOptional(value.getContent(), contentKey)
.addOptionalDate(value.getDate(), dateKey)
.build();
}

}
100 changes: 100 additions & 0 deletions libs/typescript/src/models/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {JsonBuilder, JsonSerializer, parseDate, parseSet, parseString} from '@ffk/lib-util';
import {None, Option} from 'funfix-core';
import {Set} from 'immutable';
import {Moment} from 'moment';
import {
avatarKey,
discordDiscriminatorKey,
discordIdKey,
groupKey,
idKey,
localeKey,
memberSinceKey,
permissionsKey,
usernameKey
} from '../misc/json-keys';

export class User {

constructor(
private id: Option<string> = None,
private username: Option<string> = None,
private locale: Option<string> = None,
private avatar: Option<string> = None,
private discordId: Option<string> = None,
private discordDiscriminator: Option<string> = None,
private group: Option<string> = None,
private permissions: Set<string> = Set(), // TODO: Make this Set<Enum>
private memberSince: Option<Moment> = None,
) {
}

public getId(): Option<string> {
return this.id;
}

public getUsername(): Option<string> {
return this.username;
}

public getLocale(): Option<string> {
return this.locale;
}

public getAvatar(): Option<string> {
return this.avatar;
}

public getDiscordId(): Option<string> {
return this.discordId;
}

public getDiscordDiscriminator(): Option<string> {
return this.discordDiscriminator;
}

public getGroup(): Option<string> {
return this.group;
}

public getPermissions(): Set<string> {
return this.permissions;
}

public getMemberSince(): Option<Moment> {
return this.memberSince;
}

}

export class UserJsonSerializer extends JsonSerializer<User> {

static instance: UserJsonSerializer = new UserJsonSerializer();

fromJson(json: any): User {
return new User(
parseString(json[idKey]),
parseString(json[usernameKey]),
parseString(json[localeKey]),
parseString(json[avatarKey]),
parseString(json[discordIdKey]),
parseString(json[discordDiscriminatorKey]),
parseString(json[groupKey]),
parseSet(json[permissionsKey]),
parseDate(json[memberSinceKey]),
);
}

toJson(value: User, builder: JsonBuilder): object {
return builder.addOptional(value.getId(), idKey)
.addOptional(value.getUsername(), usernameKey)
.addOptional(value.getLocale(), localeKey)
.addOptional(value.getAvatar(), avatarKey)
.addOptional(value.getDiscordId(), discordIdKey)
.addOptional(value.getDiscordDiscriminator(), discordDiscriminatorKey)
.addOptional(value.getGroup(), groupKey)
.addIterable(value.getPermissions(), permissionsKey)
.addOptionalDate(value.getMemberSince(), memberSinceKey)
.build();
}
}

0 comments on commit 986a2ff

Please sign in to comment.