From 3cd58aa6c5455a021464fef84d80d7eee46e7e27 Mon Sep 17 00:00:00 2001 From: Sean Engelhardt Date: Mon, 25 Mar 2019 09:45:11 +0100 Subject: [PATCH] Cleanup the repo-cleanup Adds a "patch" function to allow more modles to send patches with minimal data rather than just the whole model. Re-Adds the throwing of some errors to prevent developers from calling methods that should not be called --- .../core/core-services/data-send.service.ts | 1 - .../app/core/repositories/base-repository.ts | 24 +++++++++--- .../config/config-repository.service.ts | 19 ++++++++++ .../history/history-repository.service.ts | 30 +++++++++++++++ .../mediafile-repository.service.ts | 2 +- .../motions/motion-repository.service.ts | 22 +++++------ .../users/personal-note-repository.service.ts | 37 +++++++++++++++++++ .../users/user-repository.service.ts | 2 +- .../motion-detail/motion-detail.component.ts | 2 +- 9 files changed, 117 insertions(+), 22 deletions(-) diff --git a/client/src/app/core/core-services/data-send.service.ts b/client/src/app/core/core-services/data-send.service.ts index d3e200b3f..f3a362cf5 100644 --- a/client/src/app/core/core-services/data-send.service.ts +++ b/client/src/app/core/core-services/data-send.service.ts @@ -32,7 +32,6 @@ export class DataSendService { /** * Function to fully update a model on the server. - * TODO: Deprecated (?) * * @param model The model that is meant to be changed. */ diff --git a/client/src/app/core/repositories/base-repository.ts b/client/src/app/core/repositories/base-repository.ts index 9983af550..bc44f1547 100644 --- a/client/src/app/core/repositories/base-repository.ts +++ b/client/src/app/core/repositories/base-repository.ts @@ -173,17 +173,31 @@ export abstract class BaseRepository { + public async update(update: Partial, viewModel: V): Promise { const sendUpdate = new this.baseModelCtor(); sendUpdate.patchValues(viewModel.getModel()); sendUpdate.patchValues(update); - return await this.dataSend.partialUpdateModel(sendUpdate); + return await this.dataSend.updateModel(sendUpdate); + } + + /** + * patches an existing model with new data, + * rather than sending a full update + * + * @param update the update to send + * @param viewModel the motion to update + */ + public async patch(update: Partial, viewModel: V): Promise { + const patch = new this.baseModelCtor(); + patch.id = viewModel.id; + patch.patchValues(update); + return await this.dataSend.partialUpdateModel(patch); } /** @@ -192,7 +206,7 @@ export abstract class BaseRepository { + public async delete(viewModel: V): Promise { return await this.dataSend.deleteModel(viewModel.getModel()); } @@ -202,7 +216,7 @@ export abstract class BaseRepository { + public async create(model: M): Promise { // this ensures we get a valid base model, even if the view was just // sending an object with "as MyModelClass" const sendModel = new this.baseModelCtor(); diff --git a/client/src/app/core/repositories/config/config-repository.service.ts b/client/src/app/core/repositories/config/config-repository.service.ts index 1c13b6d8d..f92c3a96a 100644 --- a/client/src/app/core/repositories/config/config-repository.service.ts +++ b/client/src/app/core/repositories/config/config-repository.service.ts @@ -8,6 +8,7 @@ import { DataSendService } from 'app/core/core-services/data-send.service'; import { DataStoreService } from 'app/core/core-services/data-store.service'; import { ConstantsService } from 'app/core/ui-services/constants.service'; import { HttpService } from 'app/core/core-services/http.service'; +import { Identifiable } from 'app/shared/models/base/identifiable'; import { CollectionStringMapperService } from 'app/core/core-services/collectionStringMapper.service'; import { ViewModelStoreService } from 'app/core/core-services/view-model-store.service'; import { ViewConfig } from 'app/site/config/models/view-config'; @@ -127,6 +128,24 @@ export class ConfigRepositoryService extends BaseRepository return viewConfig; } + /** + * Overwrites the default delete procedure + * + * @ignore + */ + public async delete(): Promise { + throw new Error('Config variables cannot be deleted'); + } + + /** + * Overwrite the default create procedure. + * + * @ignore + */ + public async create(): Promise { + throw new Error('Config variables cannot be created'); + } + /** * Overwritten setup. Does only care about the custom list observable and inserts changed configs into the * config group structure. diff --git a/client/src/app/core/repositories/history/history-repository.service.ts b/client/src/app/core/repositories/history/history-repository.service.ts index e0c48a9d5..2c58e748e 100644 --- a/client/src/app/core/repositories/history/history-repository.service.ts +++ b/client/src/app/core/repositories/history/history-repository.service.ts @@ -4,6 +4,7 @@ import { CollectionStringMapperService } from 'app/core/core-services/collection import { DataStoreService } from 'app/core/core-services/data-store.service'; import { BaseRepository } from 'app/core/repositories/base-repository'; import { History } from 'app/shared/models/core/history'; +import { Identifiable } from 'app/shared/models/base/identifiable'; import { User } from 'app/shared/models/users/user'; import { HttpService } from 'app/core/core-services/http.service'; import { ViewHistory } from 'app/site/history/models/view-history'; @@ -60,6 +61,35 @@ export class HistoryRepositoryService extends BaseRepository { + throw new Error('You cannot create a history object'); + } + + /** + * Overwrites the default procedure + * + * @ignore + */ + public async update(): Promise { + throw new Error('You cannot update a history object'); + } + + /** + * Overwrites the default procedure + * + * @ignore + */ + public async patch(): Promise { + throw new Error('You cannot patch a history object'); + } + + /** + * Overwrites the default procedure + * * Sends a post-request to delete history objects */ public async delete(): Promise { diff --git a/client/src/app/core/repositories/mediafiles/mediafile-repository.service.ts b/client/src/app/core/repositories/mediafiles/mediafile-repository.service.ts index 4ef62e80c..f1c7bb3c8 100644 --- a/client/src/app/core/repositories/mediafiles/mediafile-repository.service.ts +++ b/client/src/app/core/repositories/mediafiles/mediafile-repository.service.ts @@ -33,7 +33,7 @@ export class MediafileRepositoryService extends BaseRepository { - const motion = viewMotion.motion; - motion.category_id = categoryId; - await this.update(motion, viewMotion); + await this.patch({ category_id: categoryId }, viewMotion); } /** @@ -329,9 +327,7 @@ export class MotionRepositoryService extends BaseAgendaContentObjectRepository { - const motion = viewMotion.motion; - motion.motion_block_id = blockId; - await this.update(motion, viewMotion); + await this.patch({ motion_block_id: blockId }, viewMotion); } /** @@ -341,17 +337,17 @@ export class MotionRepositoryService extends BaseAgendaContentObjectRepository { - const motion = viewMotion.motion; - const tagIndex = motion.tags_id.findIndex(tag => tag === tagId); + const tags = viewMotion.motion.tags_id.map(tag => tag); + const tagIndex = tags.findIndex(tag => tag === tagId); if (tagIndex === -1) { // add tag to motion - motion.tags_id.push(tagId); + tags.push(tagId); } else { // remove tag from motion - motion.tags_id.splice(tagIndex, 1); + tags.splice(tagIndex, 1); } - await this.update(motion, viewMotion); + await this.patch({ tags_id: tags }, viewMotion); } /** @@ -780,7 +776,7 @@ export class MotionRepositoryService extends BaseAgendaContentObjectRepository { if (viewMotion.state.show_state_extension_field) { - return this.update({ state_extension: value }, viewMotion); + return this.patch({ state_extension: value }, viewMotion); } } @@ -792,7 +788,7 @@ export class MotionRepositoryService extends BaseAgendaContentObjectRepository { if (viewMotion.recommendation.show_recommendation_extension_field) { - return this.update({ recommendation_extension: value }, viewMotion); + return this.patch({ recommendation_extension: value }, viewMotion); } } diff --git a/client/src/app/core/repositories/users/personal-note-repository.service.ts b/client/src/app/core/repositories/users/personal-note-repository.service.ts index 12714c593..6ed69d2d8 100644 --- a/client/src/app/core/repositories/users/personal-note-repository.service.ts +++ b/client/src/app/core/repositories/users/personal-note-repository.service.ts @@ -6,6 +6,7 @@ import { DataSendService } from 'app/core/core-services/data-send.service'; import { DataStoreService } from '../../core-services/data-store.service'; import { BaseRepository } from '../base-repository'; import { CollectionStringMapperService } from '../../core-services/collectionStringMapper.service'; +import { Identifiable } from 'app/shared/models/base/identifiable'; import { PersonalNote } from 'app/shared/models/users/personal-note'; import { ViewPersonalNote } from 'app/site/users/models/view-personal-note'; import { ViewModelStoreService } from 'app/core/core-services/view-model-store.service'; @@ -39,4 +40,40 @@ export class PersonalNoteRepositoryService extends BaseRepository { + throw new Error('Not supported'); + } + + /** + * Overwrite the default procedure + * + * @ignore + */ + public async update(): Promise { + throw new Error('Not supported'); + } + + /** + * Overwrite the default procedure + * + * @ignore + */ + public async patch(): Promise { + throw new Error('Not supported'); + } + + /** + * Overwrite the default procedure + * + * @ignore + */ + public async delete(): Promise { + throw new Error('Not supported'); + } } diff --git a/client/src/app/core/repositories/users/user-repository.service.ts b/client/src/app/core/repositories/users/user-repository.service.ts index ab510b5cd..6d2423dbe 100644 --- a/client/src/app/core/repositories/users/user-repository.service.ts +++ b/client/src/app/core/repositories/users/user-repository.service.ts @@ -104,7 +104,7 @@ export class UserRepositoryService extends BaseRepository { updateUser.gender = ''; } - return await this.dataSend.partialUpdateModel(updateUser); + return await this.dataSend.updateModel(updateUser); } /** diff --git a/client/src/app/site/motions/modules/motion-detail/components/motion-detail/motion-detail.component.ts b/client/src/app/site/motions/modules/motion-detail/components/motion-detail/motion-detail.component.ts index 95d16594c..14ce9e3d8 100644 --- a/client/src/app/site/motions/modules/motion-detail/components/motion-detail/motion-detail.component.ts +++ b/client/src/app/site/motions/modules/motion-detail/components/motion-detail/motion-detail.component.ts @@ -671,7 +671,7 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit, origin: [''], statute_amendment: [''], // Internal value for the checkbox, not saved to the model statute_paragraph_id: [''], - motion_block_id: [], + motion_block_id: [], // TODO: Can be removed if this is not required parent_id: [] }); this.updateWorkflowIdForCreateForm();