From 98250f4e458db29d258b6353cb8974c443885548 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Tue, 4 Sep 2018 11:33:28 +0200 Subject: [PATCH] less constructors --- .gitignore | 1 + client/src/app/core/services/auth.service.ts | 2 +- .../app/core/services/autoupdate.service.ts | 2 +- .../app/core/services/data-store.service.ts | 2 +- .../src/app/core/services/operator.service.ts | 2 +- client/src/app/shared/models/agenda/item.ts | 39 ++--------- .../src/app/shared/models/agenda/speaker.ts | 31 ++------- .../models/assignments/assignment-user.ts | 19 ++---- .../shared/models/assignments/assignment.ts | 36 +++-------- .../shared/models/assignments/poll-option.ts | 28 ++------ .../src/app/shared/models/assignments/poll.ts | 43 +++---------- client/src/app/shared/models/base.model.ts | 3 +- .../app/shared/models/core/chat-message.ts | 14 ++-- client/src/app/shared/models/core/config.ts | 8 +-- .../src/app/shared/models/core/countdown.ts | 16 ++--- .../shared/models/core/projector-message.ts | 7 +- .../src/app/shared/models/core/projector.ts | 24 ++----- client/src/app/shared/models/core/tag.ts | 7 +- .../app/shared/models/deserializable.model.ts | 8 ++- .../src/app/shared/models/mediafiles/file.ts | 10 +-- .../app/shared/models/mediafiles/mediafile.ts | 32 +++------- .../src/app/shared/models/motions/category.ts | 8 +-- .../app/shared/models/motions/motion-block.ts | 8 +-- .../models/motions/motion-change-reco.ts | 24 ++----- .../app/shared/models/motions/motion-log.ts | 12 ++-- .../shared/models/motions/motion-submitter.ts | 12 ++-- .../shared/models/motions/motion-version.ts | 27 +++----- .../src/app/shared/models/motions/motion.ts | 64 +++++-------------- .../shared/models/motions/workflow-state.ts | 58 ++--------------- .../src/app/shared/models/motions/workflow.ts | 14 ++-- client/src/app/shared/models/topics/topic.ts | 10 ++- client/src/app/shared/models/users/group.ts | 8 +-- .../app/shared/models/users/personal-note.ts | 13 ++-- client/src/app/shared/models/users/user.ts | 39 ++--------- .../motion-detail/motion-detail.component.ts | 9 +-- client/src/app/site/start/start.component.ts | 56 ++++++++-------- 36 files changed, 205 insertions(+), 491 deletions(-) diff --git a/.gitignore b/.gitignore index ddfd4ee0b..bcc3cb318 100644 --- a/.gitignore +++ b/.gitignore @@ -63,6 +63,7 @@ client/node_modules # misc Compodoc +Compodocmodules client/.sass-cache client/connect.lock client/coverage diff --git a/client/src/app/core/services/auth.service.ts b/client/src/app/core/services/auth.service.ts index 50fc5e159..77fd75c70 100644 --- a/client/src/app/core/services/auth.service.ts +++ b/client/src/app/core/services/auth.service.ts @@ -56,7 +56,7 @@ export class AuthService extends OpenSlidesComponent { }; return this.http.post(environment.urlPrefix + '/users/login/', user).pipe( tap((response: LoginResponse) => { - this.operator.user = new User().deserialize(response.user); + this.operator.user = new User(response.user); }), catchError(this.handleError()) ) as Observable; diff --git a/client/src/app/core/services/autoupdate.service.ts b/client/src/app/core/services/autoupdate.service.ts index 2496d9bfb..4b5e95e0e 100644 --- a/client/src/app/core/services/autoupdate.service.ts +++ b/client/src/app/core/services/autoupdate.service.ts @@ -66,7 +66,7 @@ export class AutoupdateService extends OpenSlidesComponent { /*throw new Error*/ console.log(`Unregistered resource ${collection}`); return; } - this.DS.add(...autoupdate.changed[collection].map(_obj => new targetClass().deserialize(_obj.data))); + this.DS.add(...autoupdate.changed[collection].map(_obj => new targetClass(_obj.data))); }); } diff --git a/client/src/app/core/services/data-store.service.ts b/client/src/app/core/services/data-store.service.ts index 764447dad..f9fdf261f 100644 --- a/client/src/app/core/services/data-store.service.ts +++ b/client/src/app/core/services/data-store.service.ts @@ -129,7 +129,7 @@ export class DataStoreService { if (target) { Object.keys(serializedStore[collectionString]).forEach(id => { const data = JSON.parse(serializedStore[collectionString][id]); - storage[collectionString][id] = new target().deserialize(data); + storage[collectionString][id] = new target(data); }); } }); diff --git a/client/src/app/core/services/operator.service.ts b/client/src/app/core/services/operator.service.ts index 0667d7363..0983ff233 100644 --- a/client/src/app/core/services/operator.service.ts +++ b/client/src/app/core/services/operator.service.ts @@ -107,7 +107,7 @@ export class OperatorService extends OpenSlidesComponent { return this.http.get(environment.urlPrefix + '/users/whoami/').pipe( tap((response: WhoAmIResponse) => { if (response && response.user_id) { - this.user = new User().deserialize(response.user); + this.user = new User(response.user); } }), catchError(this.handleError()) diff --git a/client/src/app/shared/models/agenda/item.ts b/client/src/app/shared/models/agenda/item.ts index 055767de7..b93ab3b6b 100644 --- a/client/src/app/shared/models/agenda/item.ts +++ b/client/src/app/shared/models/agenda/item.ts @@ -28,38 +28,12 @@ export class Item extends BaseModel { public weight: number; public parent_id: number; - public constructor( - id?: number, - item_number?: string, - title?: string, - list_view_title?: string, - comment?: string, - closed?: boolean, - type?: number, - is_hidden?: boolean, - duration?: number, - speakers?: Speaker[], - speaker_list_closed?: boolean, - content_object?: ContentObject, - weight?: number, - parent_id?: number - ) { + public constructor(input?: any) { super(); this._collectionString = 'agenda/item'; - this.id = id; - this.item_number = item_number; - this.title = title; - this.list_view_title = list_view_title; - this.comment = comment; - this.closed = closed; - this.type = type; - this.is_hidden = is_hidden; - this.duration = duration; - this.speakers = speakers; - this.speaker_list_closed = speaker_list_closed; - this.content_object = content_object; - this.weight = weight; - this.parent_id = parent_id; + if (input) { + this.deserialize(input); + } } public getSpeakers(): User[] { @@ -75,15 +49,14 @@ export class Item extends BaseModel { return this.DS.get(this.content_object.collection, this.content_object.id); } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); if (input.speakers instanceof Array) { this.speakers = input.speakers.map(speakerData => { - return new Speaker().deserialize(speakerData); + return new Speaker(speakerData); }); } - return this; } } diff --git a/client/src/app/shared/models/agenda/speaker.ts b/client/src/app/shared/models/agenda/speaker.ts index e167f7dd3..e37d0e093 100644 --- a/client/src/app/shared/models/agenda/speaker.ts +++ b/client/src/app/shared/models/agenda/speaker.ts @@ -17,34 +17,15 @@ export class Speaker implements Deserializable { /** * Needs to be completely optional because agenda has (yet) the optional parameter 'speaker' - * @param id - * @param user_id - * @param begin_time - * @param end_time - * @param weight - * @param marked - * @param item_id + * @param input */ - public constructor( - id?: number, - user_id?: number, - begin_time?: string, - end_time?: string, - weight?: number, - marked?: boolean, - item_id?: number - ) { - this.id = id; - this.user_id = user_id; - this.begin_time = begin_time; - this.end_time = end_time; - this.weight = weight; - this.marked = marked; - this.item_id = item_id; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/assignments/assignment-user.ts b/client/src/app/shared/models/assignments/assignment-user.ts index b783c1df5..2ad7a82e5 100644 --- a/client/src/app/shared/models/assignments/assignment-user.ts +++ b/client/src/app/shared/models/assignments/assignment-user.ts @@ -13,22 +13,15 @@ export class AssignmentUser implements Deserializable { /** * Needs to be completely optional because assignment has (yet) the optional parameter 'assignment_related_users' - * @param id - * @param user_id - * @param elected - * @param assignment_id - * @param weight + * @param input */ - public constructor(id?: number, user_id?: number, elected?: boolean, assignment_id?: number, weight?: number) { - this.id = id; - this.user_id = user_id; - this.elected = elected; - this.assignment_id = assignment_id; - this.weight = weight; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/assignments/assignment.ts b/client/src/app/shared/models/assignments/assignment.ts index 1ae32d750..d90616936 100644 --- a/client/src/app/shared/models/assignments/assignment.ts +++ b/client/src/app/shared/models/assignments/assignment.ts @@ -21,30 +21,15 @@ export class Assignment extends BaseModel { public agenda_item_id: number; public tags_id: number[]; - public constructor( - id?: number, - title?: string, - description?: string, - open_posts?: number, - phase?: number, - assignment_related_users?: AssignmentUser[], - poll_description_default?: number, - polls?: Poll[], - agenda_item_id?: number, - tags_id?: number[] - ) { + public constructor(input?: any) { super(); this._collectionString = 'assignments/assignment'; - this.id = id; - this.title = title; - this.description = description; - this.open_posts = open_posts; - this.phase = phase; - this.assignment_related_users = assignment_related_users || []; // TODO Array - this.poll_description_default = poll_description_default; - this.polls = polls || Array(); // TODO Array - this.agenda_item_id = agenda_item_id; - this.tags_id = tags_id; + this.assignment_related_users = []; // TODO Array + this.polls = Array(); // TODO Array + + if (input) { + this.deserialize(input); + } } public getAssignmentReleatedUsers(): BaseModel | BaseModel[] { @@ -59,23 +44,22 @@ export class Assignment extends BaseModel { return this.DS.getMany('core/tag', this.tags_id); } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); if (input.assignment_related_users instanceof Array) { this.assignment_related_users = []; input.assignment_related_users.forEach(assignmentUserData => { - this.assignment_related_users.push(new AssignmentUser().deserialize(assignmentUserData)); + this.assignment_related_users.push(new AssignmentUser(assignmentUserData)); }); } if (input.polls instanceof Array) { this.polls = []; input.polls.forEach(pollData => { - this.polls.push(new Poll().deserialize(pollData)); + this.polls.push(new Poll(pollData)); }); } - return this; } } diff --git a/client/src/app/shared/models/assignments/poll-option.ts b/client/src/app/shared/models/assignments/poll-option.ts index 32ec3f08d..308b0088a 100644 --- a/client/src/app/shared/models/assignments/poll-option.ts +++ b/client/src/app/shared/models/assignments/poll-option.ts @@ -16,31 +16,15 @@ export class PollOption implements Deserializable { /** * Needs to be completely optional because poll has (yet) the optional parameter 'poll-options' - * @param id - * @param candidate_id - * @param is_elected - * @param votes - * @param poll_id - * @param weight + * @param input */ - public constructor( - id?: number, - candidate_id?: number, - is_elected?: boolean, - votes?: number[], - poll_id?: number, - weight?: number - ) { - this.id = id; - this.candidate_id = candidate_id; - this.is_elected = is_elected; - this.votes = votes; - this.poll_id = poll_id; - this.weight = weight; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/assignments/poll.ts b/client/src/app/shared/models/assignments/poll.ts index d4dca9b83..8f402a99a 100644 --- a/client/src/app/shared/models/assignments/poll.ts +++ b/client/src/app/shared/models/assignments/poll.ts @@ -19,50 +19,23 @@ export class Poll implements Deserializable { /** * Needs to be completely optional because assignment has (yet) the optional parameter 'polls' - * @param id - * @param pollmethod - * @param description - * @param published - * @param options - * @param votesvalid - * @param votesinvalid - * @param votescast - * @param has_votes - * @param assignment_id + * @param input */ - public constructor( - id?: number, - pollmethod?: string, - description?: string, - published?: boolean, - options?: PollOption[], - votesvalid?: number, - votesinvalid?: number, - votescast?: number, - has_votes?: boolean, - assignment_id?: number - ) { - this.id = id; - this.pollmethod = pollmethod; - this.description = description; - this.published = published; - this.options = options || Array(new PollOption()); // TODO Array - this.votesvalid = votesvalid; - this.votesinvalid = votesinvalid; - this.votescast = votescast; - this.has_votes = has_votes; - this.assignment_id = assignment_id; + public constructor(input?: any) { + this.options = [new PollOption()]; + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); if (input.options instanceof Array) { this.options = []; input.options.forEach(pollOptionData => { - this.options.push(new PollOption().deserialize(pollOptionData)); + this.options.push(new PollOption(pollOptionData)); }); } - return this; } } diff --git a/client/src/app/shared/models/base.model.ts b/client/src/app/shared/models/base.model.ts index 4bc0f68db..5de4186d4 100644 --- a/client/src/app/shared/models/base.model.ts +++ b/client/src/app/shared/models/base.model.ts @@ -57,8 +57,7 @@ export abstract class BaseModel extends OpenSlidesComponent implements Deseriali * Inherited to children, can be overwritten for special use cases * @param input JSON data for deserialization. */ - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/core/chat-message.ts b/client/src/app/shared/models/core/chat-message.ts index cc2685ca5..60080a8f0 100644 --- a/client/src/app/shared/models/core/chat-message.ts +++ b/client/src/app/shared/models/core/chat-message.ts @@ -1,4 +1,5 @@ import { BaseModel } from '../base.model'; +import { User } from '../users/user'; /** * Representation of chat messages. @@ -11,17 +12,16 @@ export class ChatMessage extends BaseModel { public timestamp: string; // TODO: Type for timestamp public user_id: number; - public constructor(id?: number, message?: string, timestamp?: string, user_id?: number) { + public constructor(input?: any) { super(); this._collectionString = 'core/chat-message'; - this.id = id; - this.message = message; - this.timestamp = timestamp; - this.user_id = user_id; + if (input) { + this.deserialize(input); + } } - public getUser(): BaseModel | BaseModel[] { - return this.DS.get('users/user', this.user_id); + public getUser(): User { + return this.DS.get('users/user', this.user_id); } } diff --git a/client/src/app/shared/models/core/config.ts b/client/src/app/shared/models/core/config.ts index 88c659f77..c85275442 100644 --- a/client/src/app/shared/models/core/config.ts +++ b/client/src/app/shared/models/core/config.ts @@ -10,12 +10,12 @@ export class Config extends BaseModel { public key: string; public value: Object; - public constructor(id?: number, key?: string, value?: Object) { + public constructor(input?: any) { super(); this._collectionString = 'core/config'; - this.id = id; - this.key = key; - this.value = value; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/core/countdown.ts b/client/src/app/shared/models/core/countdown.ts index 5a287a94d..d2fe0320a 100644 --- a/client/src/app/shared/models/core/countdown.ts +++ b/client/src/app/shared/models/core/countdown.ts @@ -12,20 +12,12 @@ export class Countdown extends BaseModel { public countdown_time: number; public running: boolean; - public constructor( - id?: number, - countdown_time?: number, - default_time?: number, - description?: string, - running?: boolean - ) { + public constructor(input?: any) { super(); this._collectionString = 'core/countdown'; - this.id = id; - this.description = description; - this.default_time = default_time; - this.countdown_time = countdown_time; - this.running = running; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/core/projector-message.ts b/client/src/app/shared/models/core/projector-message.ts index d1c377dab..782fbbd56 100644 --- a/client/src/app/shared/models/core/projector-message.ts +++ b/client/src/app/shared/models/core/projector-message.ts @@ -9,11 +9,12 @@ export class ProjectorMessage extends BaseModel { public id: number; public message: string; - public constructor(id?: number, message?: string) { + public constructor(input?: any) { super(); this._collectionString = 'core/projector-message'; - this.id = id; - this.message = message; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/core/projector.ts b/client/src/app/shared/models/core/projector.ts index 0145074bb..271a708c6 100644 --- a/client/src/app/shared/models/core/projector.ts +++ b/client/src/app/shared/models/core/projector.ts @@ -16,28 +16,12 @@ export class Projector extends BaseModel { public height: number; public projectiondefaults: Object[]; - public constructor( - id?: number, - elements?: Object, - scale?: number, - scroll?: number, - name?: string, - blank?: boolean, - width?: number, - height?: number, - projectiondefaults?: Object[] - ) { + public constructor(input?: any) { super(); this._collectionString = 'core/projector'; - this.id = id; - this.elements = elements; - this.scale = scale; - this.scroll = scroll; - this.name = name; - this.blank = blank; - this.width = width; - this.height = height; - this.projectiondefaults = projectiondefaults; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/core/tag.ts b/client/src/app/shared/models/core/tag.ts index 82d344d07..e8678bb15 100644 --- a/client/src/app/shared/models/core/tag.ts +++ b/client/src/app/shared/models/core/tag.ts @@ -9,11 +9,12 @@ export class Tag extends BaseModel { public id: number; public name: string; - public constructor(id?: number, name?: string) { + public constructor(input?: any) { super(); this._collectionString = 'core/tag'; - this.id = id; - this.name = name; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/deserializable.model.ts b/client/src/app/shared/models/deserializable.model.ts index 0ed73c2bc..7d2cfd844 100644 --- a/client/src/app/shared/models/deserializable.model.ts +++ b/client/src/app/shared/models/deserializable.model.ts @@ -2,12 +2,16 @@ * Interface tells models to offer a 'deserialize' function * * Also nested objects and arrays have have to be handled. - * @example const myUser = new User().deserialize(jsonData) + * @example + * ``` ts + * const myUser = new User(); + * myUser.deserialize(jsonData); + * ``` */ export interface Deserializable { /** * should be used to assign JSON values to the object itself. * @param input */ - deserialize(input: any): this; + deserialize(input: any): void; } diff --git a/client/src/app/shared/models/mediafiles/file.ts b/client/src/app/shared/models/mediafiles/file.ts index e41373c95..02009935b 100644 --- a/client/src/app/shared/models/mediafiles/file.ts +++ b/client/src/app/shared/models/mediafiles/file.ts @@ -13,13 +13,13 @@ export class File implements Deserializable { * @param name The name of the file * @param type The tape (jpg, png, pdf) */ - public constructor(name?: string, type?: string) { - this.name = name; - this.type = type; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/mediafiles/mediafile.ts b/client/src/app/shared/models/mediafiles/mediafile.ts index 5c7247491..c7a163676 100644 --- a/client/src/app/shared/models/mediafiles/mediafile.ts +++ b/client/src/app/shared/models/mediafiles/mediafile.ts @@ -1,5 +1,6 @@ import { BaseModel } from '../base.model'; import { File } from './file'; +import { User } from '../users/user'; /** * Representation of MediaFile. Has the nested property "File" @@ -16,36 +17,21 @@ export class Mediafile extends BaseModel { public hidden: boolean; public timestamp: string; - public constructor( - id?: number, - title?: string, - mediafile?: File, - media_url_prefix?: string, - uploader_id?: number, - filesize?: string, - hidden?: boolean, - timestamp?: string - ) { + public constructor(input?: any) { super(); this._collectionString = 'mediafiles/mediafile'; - this.id = id; - this.title = title; - this.mediafile = mediafile; - this.media_url_prefix = media_url_prefix; - this.uploader_id = uploader_id; - this.filesize = filesize; - this.hidden = hidden; - this.timestamp = timestamp; + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - this.mediafile = new File().deserialize(input.mediafile); - return this; + this.mediafile = new File(input.mediafile); } - public getUploader(): BaseModel | BaseModel[] { - return this.DS.get('users/user', this.uploader_id); + public getUploader(): User { + return this.DS.get('users/user', this.uploader_id); } } diff --git a/client/src/app/shared/models/motions/category.ts b/client/src/app/shared/models/motions/category.ts index 45a9083d0..aae765e05 100644 --- a/client/src/app/shared/models/motions/category.ts +++ b/client/src/app/shared/models/motions/category.ts @@ -10,12 +10,12 @@ export class Category extends BaseModel { public name: string; public prefix: string; - public constructor(id?: number, name?: string, prefix?: string) { + public constructor(input?: any) { super(); this._collectionString = 'motions/category'; - this.id = id; - this.name = name; - this.prefix = prefix; + if (input) { + this.deserialize(input); + } } public toString = (): string => { diff --git a/client/src/app/shared/models/motions/motion-block.ts b/client/src/app/shared/models/motions/motion-block.ts index bc28442ac..23c425ab3 100644 --- a/client/src/app/shared/models/motions/motion-block.ts +++ b/client/src/app/shared/models/motions/motion-block.ts @@ -10,12 +10,12 @@ export class MotionBlock extends BaseModel { public title: string; public agenda_item_id: number; - public constructor(id?: number, title?: string, agenda_item_id?: number) { + public constructor(input?: any) { super(); this._collectionString = 'motions/motion-block'; - this.id = id; - this.title = title; - this.agenda_item_id = agenda_item_id; + if (input) { + this.deserialize(input); + } } public getAgenda(): BaseModel | BaseModel[] { diff --git a/client/src/app/shared/models/motions/motion-change-reco.ts b/client/src/app/shared/models/motions/motion-change-reco.ts index a11a42b3f..fb8f9eb4c 100644 --- a/client/src/app/shared/models/motions/motion-change-reco.ts +++ b/client/src/app/shared/models/motions/motion-change-reco.ts @@ -16,28 +16,12 @@ export class MotionChangeReco extends BaseModel { public text: string; public creation_time: string; - public constructor( - id?: number, - motion_version_id?: number, - rejected?: boolean, - type?: number, - other_description?: string, - line_from?: number, - line_to?: number, - text?: string, - creation_time?: string - ) { + public constructor(input?: any) { super(); this._collectionString = 'motions/motion-change-recommendation'; - this.id = id; - this.motion_version_id = motion_version_id; - this.rejected = rejected; - this.type = type; - this.other_description = other_description; - this.line_from = line_from; - this.line_to = line_to; - this.text = text; - this.creation_time = creation_time; + if (input) { + this.deserialize(input); + } } } diff --git a/client/src/app/shared/models/motions/motion-log.ts b/client/src/app/shared/models/motions/motion-log.ts index f2ec96ef2..2e240ecbc 100644 --- a/client/src/app/shared/models/motions/motion-log.ts +++ b/client/src/app/shared/models/motions/motion-log.ts @@ -11,15 +11,13 @@ export class MotionLog implements Deserializable { public time: string; public message: string; - public constructor(message_list?: string[], person_id?: number, time?: string, message?: string) { - this.message_list = message_list; - this.person_id = person_id; - this.time = time; - this.message = message; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/motions/motion-submitter.ts b/client/src/app/shared/models/motions/motion-submitter.ts index 334528740..b1399e639 100644 --- a/client/src/app/shared/models/motions/motion-submitter.ts +++ b/client/src/app/shared/models/motions/motion-submitter.ts @@ -11,15 +11,13 @@ export class MotionSubmitter implements Deserializable { public motion_id: number; public weight: number; - public constructor(id?: number, user_id?: number, motion_id?: number, weight?: number) { - this.id = id; - this.user_id = user_id; - this.motion_id = motion_id; - this.weight = weight; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/motions/motion-version.ts b/client/src/app/shared/models/motions/motion-version.ts index 0fe3a5b68..6fc9ff580 100644 --- a/client/src/app/shared/models/motions/motion-version.ts +++ b/client/src/app/shared/models/motions/motion-version.ts @@ -14,26 +14,17 @@ export class MotionVersion implements Deserializable { public amendment_paragraphs: string; public reason: string; - public constructor( - id?: number, - version_number?: number, - creation_time?: string, - title?: string, - text?: string, - amendment_paragraphs?: string, - reason?: string - ) { - this.id = id; - this.version_number = version_number; - this.creation_time = creation_time; - this.title = title || ''; - this.text = text || ''; - this.amendment_paragraphs = amendment_paragraphs || ''; - this.reason = reason || ''; + public constructor(input?: any) { + this.title = ''; + this.text = ''; + this.reason = ''; + + if (input) { + this.deserialize(input); + } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } } diff --git a/client/src/app/shared/models/motions/motion.ts b/client/src/app/shared/models/motions/motion.ts index 13939a2d5..4f6ab77bf 100644 --- a/client/src/app/shared/models/motions/motion.ts +++ b/client/src/app/shared/models/motions/motion.ts @@ -44,49 +44,20 @@ export class Motion extends BaseModel { public title: string; public text: string; - public constructor( - id?: number, - identifier?: string, - versions?: MotionVersion[], - active_version?: number, - parent_id?: number, - category_id?: number, - motion_block_id?: number, - origin?: string, - submitters?: MotionSubmitter[], - supporters_id?: number[], - comments?: Object, - state_id?: number, - state_required_permission_to_see?: string, - recommendation_id?: number, - tags_id?: number[], - attachments_id?: number[], - polls?: BaseModel[], - agenda_item_id?: number, - log_messages?: MotionLog[] - ) { + public constructor(input?: any) { super(); this._collectionString = 'motions/motion'; - this.id = id; - this.identifier = identifier || ''; - this.versions = versions || [new MotionVersion()]; - this.active_version = active_version; - this.parent_id = parent_id; - this.category_id = category_id; - this.motion_block_id = motion_block_id; - this.origin = origin || ''; - this.submitters = submitters || []; - this.supporters_id = supporters_id; - this.comments = comments; - this.state_id = state_id; - this.state_required_permission_to_see = state_required_permission_to_see || ''; - this.recommendation_id = recommendation_id; - this.tags_id = tags_id; - this.attachments_id = attachments_id; - this.polls = polls; - this.agenda_item_id = agenda_item_id; - this.log_messages = log_messages || []; + this.identifier = ''; + this.versions = [new MotionVersion()]; + this.origin = ''; + this.submitters = []; + this.supporters_id = []; + this.state_required_permission_to_see = ''; + this.log_messages = []; + if (input) { + this.deserialize(input); + } this.initDataStoreValues(); } @@ -124,7 +95,8 @@ export class Motion extends BaseModel { * @param user the user */ public addSubmitter(user: User) { - const newSubmitter = new MotionSubmitter(null, user.id); + const newSubmitter = new MotionSubmitter(); + newSubmitter.user_id = user.id; this.submitters.push(newSubmitter); console.log('did addSubmitter. this.submitters: ', this.submitters); } @@ -264,31 +236,29 @@ export class Motion extends BaseModel { } } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); if (input.versions instanceof Array) { this.versions = []; input.versions.forEach(motionVersionData => { - this.versions.push(new MotionVersion().deserialize(motionVersionData)); + this.versions.push(new MotionVersion(motionVersionData)); }); } if (input.submitters instanceof Array) { this.submitters = []; input.submitters.forEach(SubmitterData => { - this.submitters.push(new MotionSubmitter().deserialize(SubmitterData)); + this.submitters.push(new MotionSubmitter(SubmitterData)); }); } if (input.log_messages instanceof Array) { this.log_messages = []; input.log_messages.forEach(logData => { - this.log_messages.push(new MotionLog().deserialize(logData)); + this.log_messages.push(new MotionLog(logData)); }); } - - return this; } } diff --git a/client/src/app/shared/models/motions/workflow-state.ts b/client/src/app/shared/models/motions/workflow-state.ts index 5f7af0c12..afeb16aca 100644 --- a/client/src/app/shared/models/motions/workflow-state.ts +++ b/client/src/app/shared/models/motions/workflow-state.ts @@ -27,57 +27,12 @@ export class WorkflowState implements Deserializable { /** * Needs to be completely optional because Workflow has (yet) the optional parameter 'states' - * @param id - * @param name - * @param action_word - * @param recommendation_label - * @param css_class - * @param required_permission_to_see - * @param allow_support - * @param allow_create_poll - * @param allow_submitter_edit - * @param versioning - * @param leave_old_version_active - * @param dont_set_identifier - * @param show_state_extension_field - * @param show_recommendation_extension_field - * @param next_states_id - * @param workflow_id + * @param input If given, it will be deserialized */ - public constructor( - id?: number, - name?: string, - action_word?: string, - recommendation_label?: string, - css_class?: string, - required_permission_to_see?: string, - allow_support?: boolean, - allow_create_poll?: boolean, - allow_submitter_edit?: boolean, - versioning?: boolean, - leave_old_version_active?: boolean, - dont_set_identifier?: boolean, - show_state_extension_field?: boolean, - show_recommendation_extension_field?: boolean, - next_states_id?: number[], - workflow_id?: number - ) { - this.id = id; - this.name = name; - this.action_word = action_word; - this.recommendation_label = recommendation_label; - this.css_class = css_class; - this.required_permission_to_see = required_permission_to_see; - this.allow_support = allow_support; - this.allow_create_poll = allow_create_poll; - this.allow_submitter_edit = allow_submitter_edit; - this.versioning = versioning; - this.leave_old_version_active = leave_old_version_active; - this.dont_set_identifier = dont_set_identifier; - this.show_state_extension_field = show_state_extension_field; - this.show_recommendation_extension_field = show_recommendation_extension_field; - this.next_states_id = next_states_id; - this.workflow_id = workflow_id; + public constructor(input?: any) { + if (input) { + this.deserialize(input); + } } /** @@ -94,9 +49,8 @@ export class WorkflowState implements Deserializable { return nextStates; } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); - return this; } public toString = (): string => { diff --git a/client/src/app/shared/models/motions/workflow.ts b/client/src/app/shared/models/motions/workflow.ts index 30f8093ae..99b2393d8 100644 --- a/client/src/app/shared/models/motions/workflow.ts +++ b/client/src/app/shared/models/motions/workflow.ts @@ -12,13 +12,12 @@ export class Workflow extends BaseModel { public states: WorkflowState[]; public first_state: number; - public constructor(id?: number, name?: string, states?: WorkflowState[], first_state?: number) { + public constructor(input?: any) { super(); this._collectionString = 'motions/workflow'; - this.id = id; - this.name = name; - this.states = states; - this.first_state = first_state; + if (input) { + this.deserialize(input); + } } /** @@ -50,15 +49,14 @@ export class Workflow extends BaseModel { return targetState as WorkflowState; } - public deserialize(input: any): this { + public deserialize(input: any): void { Object.assign(this, input); if (input.states instanceof Array) { this.states = []; input.states.forEach(workflowStateData => { - this.states.push(new WorkflowState().deserialize(workflowStateData)); + this.states.push(new WorkflowState(workflowStateData)); }); } - return this; } } diff --git a/client/src/app/shared/models/topics/topic.ts b/client/src/app/shared/models/topics/topic.ts index 8c489c890..c0db048ab 100644 --- a/client/src/app/shared/models/topics/topic.ts +++ b/client/src/app/shared/models/topics/topic.ts @@ -14,14 +14,12 @@ export class Topic extends BaseModel { public attachments_id: number[]; public agenda_item_id: number; - public constructor(id?: number, title?: string, text?: string, attachments_id?: number[], agenda_item_id?: number) { + public constructor(input?: any) { super(); this._collectionString = 'topics/topic'; - this.id = id; - this.title = title; - this.text = text; - this.attachments_id = attachments_id; - this.agenda_item_id = agenda_item_id; + if (input) { + this.deserialize(input); + } } public getAttachments(): Mediafile[] { diff --git a/client/src/app/shared/models/users/group.ts b/client/src/app/shared/models/users/group.ts index ad3218515..26fac8804 100644 --- a/client/src/app/shared/models/users/group.ts +++ b/client/src/app/shared/models/users/group.ts @@ -11,12 +11,12 @@ export class Group extends BaseModel { public name: string; public permissions: string[]; - public constructor(id?: number, name?: string, permissions?: string[]) { + public constructor(input?: any) { super(); this._collectionString = 'users/group'; - this.id = id; - this.name = name; - this.permissions = permissions; + if (input) { + this.deserialize(input); + } } public get users() { diff --git a/client/src/app/shared/models/users/personal-note.ts b/client/src/app/shared/models/users/personal-note.ts index 223dadd77..26a607704 100644 --- a/client/src/app/shared/models/users/personal-note.ts +++ b/client/src/app/shared/models/users/personal-note.ts @@ -1,4 +1,5 @@ import { BaseModel } from '../base.model'; +import { User } from './user'; /** * Representation of users personal note. @@ -10,16 +11,16 @@ export class PersonalNote extends BaseModel { public user_id: number; public notes: Object; - public constructor(id?: number, user_id?: number, notes?: Object) { + public constructor(input: any) { super(); this._collectionString = 'users/personal-note'; - this.id = id; - this.user_id = user_id; - this.notes = notes; + if (input) { + this.deserialize(input); + } } - public getUser(): BaseModel | BaseModel[] { - return this.DS.get('users/user', this.user_id); + public getUser(): User { + return this.DS.get('users/user', this.user_id); } } diff --git a/client/src/app/shared/models/users/user.ts b/client/src/app/shared/models/users/user.ts index 89acd70c3..7be60bd06 100644 --- a/client/src/app/shared/models/users/user.ts +++ b/client/src/app/shared/models/users/user.ts @@ -24,42 +24,13 @@ export class User extends BaseModel { public is_active: boolean; public default_password: string; - public constructor( - id?: number, - username?: string, - title?: string, - first_name?: string, - last_name?: string, - structure_level?: string, - number?: string, - about_me?: string, - groups_id?: number[], - is_present?: boolean, - is_committee?: boolean, - email?: string, - last_email_send?: string, - comment?: string, - is_active?: boolean, - default_password?: string - ) { + public constructor(input?: any) { super(); this._collectionString = 'users/user'; - this.id = id; - this.username = username; - this.title = title; - this.first_name = first_name; - this.last_name = last_name; - this.structure_level = structure_level; - this.number = number; - this.about_me = about_me; - this.groups_id = groups_id; - this.is_present = is_present; - this.is_committee = is_committee; - this.email = email; - this.last_email_send = last_email_send; - this.comment = comment; - this.is_active = is_active; - this.default_password = default_password; + + if (input) { + this.deserialize(input); + } } public get groups(): Group[] { diff --git a/client/src/app/site/motions/motion-detail/motion-detail.component.ts b/client/src/app/site/motions/motion-detail/motion-detail.component.ts index 4abc80dd8..7b3ca61cf 100644 --- a/client/src/app/site/motions/motion-detail/motion-detail.component.ts +++ b/client/src/app/site/motions/motion-detail/motion-detail.component.ts @@ -106,13 +106,10 @@ export class MotionDetailComponent extends BaseComponent implements OnInit { * Async load the values of the motion in the Form. */ public patchForm(formMotion: Motion) { - console.log('Motion: ', this.motion); - console.log('category_id: ', formMotion); - this.metaInfoForm.patchValue({ - category_id: formMotion.category.id, - state_id: formMotion.state.id, - recommendation_id: formMotion.recommendation.id, + category_id: formMotion.category_id, + state_id: formMotion.state_id, + recommendation_id: formMotion.recommendation_id, identifier: formMotion.identifier, origin: formMotion.origin }); diff --git a/client/src/app/site/start/start.component.ts b/client/src/app/site/start/start.component.ts index 2b677a819..56a354860 100644 --- a/client/src/app/site/start/start.component.ts +++ b/client/src/app/site/start/start.component.ts @@ -80,18 +80,18 @@ export class StartComponent extends BaseComponent implements OnInit { */ public DataStoreTest() { console.log('add a user to dataStore'); - this.DS.add(new User(100)); + this.DS.add(new User({ id: 100 })); console.log('add three users to dataStore'); - this.DS.add(new User(200), new User(201), new User(202)); + this.DS.add(new User({ id: 200 }), new User({ id: 201 }), new User({ id: 202 })); console.log('use the spread operator "..." to add an array'); const userArray = []; for (let i = 300; i < 400; i++) { - userArray.push(new User(i)); + userArray.push(new User({ id: i })); } this.DS.add(...userArray); console.log('try to get user with ID 1:'); - const user1fromStore = this.DS.get(User, 1); + const user1fromStore = this.DS.get(User, 1); console.log('the user: ', user1fromStore); console.log('remove a single user:'); @@ -102,7 +102,7 @@ export class StartComponent extends BaseComponent implements OnInit { this.DS.remove(User, ...[321, 363, 399]); console.log('test filter: '); - console.log(this.DS.filter(User, user => user.id === 1)); + console.log(this.DS.filter(User, user => user.id === 1)); } /** @@ -153,32 +153,30 @@ export class StartComponent extends BaseComponent implements OnInit { for (let i = 1; i <= requiredMotions; ++i) { // version - const newMotionVersion = new MotionVersion( - 200 + i, - 1, - 'now', - 'GenMo ' + i, - longMotionText, - null, - longMotionText - ); + const newMotionVersion = new MotionVersion({ + id: 200 + i, + version_number: 1, + create_time: 'now', + title: 'GenMo ' + i, + text: longMotionText, + reason: longMotionText + }); // submitter - const newMotionSubmitter = new MotionSubmitter(1, 1, 200 + 1, 0); + const newMotionSubmitter = new MotionSubmitter({ + id: 1, + user_id: 1, + motion_id: 200 + i, + weight: 0 + }); // motion - const newMotion = new Motion( - 200 + i, - 'GenMo ' + i, - [newMotionVersion], - null, - null, - null, - null, - 'Generated', - [newMotionSubmitter], - null, - null, - 1 - ); + const newMotion = new Motion({ + id: 200 + i, + identifier: 'GenMo ' + i, + versions: [newMotionVersion], + origin: 'Generated', + submitters: [newMotionSubmitter], + state_id: 1 + }); newMotionsArray.push(newMotion); } this.DS.add(...newMotionsArray);