-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added user data model and amended news
- Loading branch information
Showing
5 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ | |
} | ||
}, | ||
"packages": [ | ||
".", | ||
"./libs/util", | ||
"./libs/external", | ||
"./libs/typescript" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |