Skip to content

Commit

Permalink
Added loaded the comic formats via messaging [#642]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Mar 19, 2021
1 parent 8c312e5 commit b1dc713
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@

package org.comixedproject.controller.comic;

import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.auditlog.AuditableEndpoint;
import org.comixedproject.model.comic.ComicFormat;
import org.comixedproject.service.comic.ComicFormatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -37,17 +35,20 @@
@RestController
@Log4j2
public class ComicFormatController {
private static final String LOAD_COMIC_FORMATS = "load.comic-formats";
private static final String ADD_COMIC_FORMAT_QUEUE = "/queue/comic-format.add";

@Autowired private ComicFormatService comicFormatService;
@Autowired private SimpMessagingTemplate messagingTemplate;

/**
* Returns the list of all defined comic formats.
*
* @return the list
*/
@GetMapping(value = "/api/comics/formats", produces = MediaType.APPLICATION_JSON_VALUE)
@AuditableEndpoint
public List<ComicFormat> getAll() {
log.info("Retrieving the list of comic formats");
return this.comicFormatService.getAll();
/** Retrieves the list of all comic formats and publishes them. */
@MessageMapping(LOAD_COMIC_FORMATS)
public void getAll() {
log.info("Getting all comic formats");
this.comicFormatService
.getAll()
.forEach(
comicFormat ->
this.messagingTemplate.convertAndSend(ADD_COMIC_FORMAT_QUEUE, comicFormat));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

package org.comixedproject.controller.comic;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;

import java.util.List;
import org.comixedproject.model.comic.ComicFormat;
import org.comixedproject.service.comic.ComicFormatService;
Expand All @@ -41,10 +38,7 @@ public class ComicFormatControllerTest {
public void testGetAll() {
Mockito.when(comicFormatService.getAll()).thenReturn(comicFormatList);

final List<ComicFormat> result = controller.getAll();

assertNotNull(result);
assertSame(comicFormatList, result);
controller.getAll();

Mockito.verify(comicFormatService, Mockito.times(1)).getAll();
}
Expand Down
29 changes: 29 additions & 0 deletions comixed-web/src/app/library/actions/comic-format.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2021, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { createAction, props } from '@ngrx/store';
import { ComicFormat } from '@app/library';

export const resetComicFormats = createAction(
'[Comic Format] Resets the state'
);

export const comicFormatAdded = createAction(
'[Comic Format] Comic format added',
props<{ format: ComicFormat }>()
);
46 changes: 46 additions & 0 deletions comixed-web/src/app/library/effects/comic-format.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2021, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Observable } from 'rxjs';
import { ComicFormatEffects } from './comic-format.effects';
import { ComicFormatService } from '@app/library/services/comic-format.service';
import { LoggerModule } from '@angular-ru/logger';

describe('ComicFormatEffects', () => {
const actions$: Observable<any> = null;
let effects: ComicFormatEffects;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [LoggerModule.forRoot()],
providers: [
ComicFormatEffects,
provideMockActions(() => actions$),
{ provide: ComicFormatService, useValue: {} }
]
});

effects = TestBed.inject(ComicFormatEffects);
});

it('should be created', () => {
expect(effects).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions comixed-web/src/app/library/effects/comic-format.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2021, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { Injectable } from '@angular/core';
import { Actions } from '@ngrx/effects';
import { ComicFormatService } from '@app/library/services/comic-format.service';
import { LoggerService } from '@angular-ru/logger';

@Injectable()
export class ComicFormatEffects {
constructor(
private logger: LoggerService,
private actions$: Actions,
private comicFormatService: ComicFormatService
) {}
}
9 changes: 8 additions & 1 deletion comixed-web/src/app/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import {
SCAN_TYPE_FEATURE_KEY,
ScanTypeState
} from '@app/library/reducers/scan-type.reducer';
import {
COMIC_FORMAT_FEATURE_KEY,
ComicFormatState,
reducer as comicFormatReducer
} from '@app/library/reducers/comic-format.reducer';

export { Comic } from '@app/library/models/comic';
export { ComicCredit } from '@app/library/models/comic-credit';
Expand Down Expand Up @@ -76,6 +81,7 @@ export interface LibraryModuleState {
[SCRAPING_FEATURE_KEY]: ScrapingState;
[BLOCKED_PAGE_FEATURE_KEY]: BlockedPageState;
[SCAN_TYPE_FEATURE_KEY]: ScanTypeState;
[COMIC_FORMAT_FEATURE_KEY]: ComicFormatState;
}

export type ModuleState = LibraryModuleState;
Expand All @@ -87,5 +93,6 @@ export const reducers: ActionReducerMap<LibraryModuleState> = {
[LIBRARY_FEATURE_KEY]: libraryReducer,
[SCRAPING_FEATURE_KEY]: scrapingReducer,
[BLOCKED_PAGE_FEATURE_KEY]: blockedPageReducer,
[SCAN_TYPE_FEATURE_KEY]: scanTypeReducer
[SCAN_TYPE_FEATURE_KEY]: scanTypeReducer,
[COMIC_FORMAT_FEATURE_KEY]: comicFormatReducer
};
3 changes: 3 additions & 0 deletions comixed-web/src/app/library/library.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ export const PAGINATION_DEFAULT = PAGINATION_OPTIONS[0];
// messaging
export const LOAD_SCAN_TYPES_MESSAGE = '/comixed/load.scan-types';
export const SCAN_TYPE_ADD_QUEUE = '/queue/scan-type.add';

export const LOAD_COMIC_FORMATS_MESSAGE = '/comixed/load.comic-formats';
export const COMIC_FORMAT_ADD_QUEUE = '/queue/comic-format.add';
9 changes: 8 additions & 1 deletion comixed-web/src/app/library/library.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ import {
reducer as scanTypeReducer
} from '@app/library/reducers/scan-type.reducer';
import { ScanTypeEffects } from '@app/library/effects/scan-type.effects';
import {
COMIC_FORMAT_FEATURE_KEY,
reducer as comicFormatReducer
} from '@app/library/reducers/comic-format.reducer';
import { ComicFormatEffects } from '@app/library/effects/comic-format.effects';

@NgModule({
declarations: [
Expand Down Expand Up @@ -150,13 +155,15 @@ import { ScanTypeEffects } from '@app/library/effects/scan-type.effects';
StoreModule.forFeature(SCRAPING_FEATURE_KEY, scrapingReducer),
StoreModule.forFeature(BLOCKED_PAGE_FEATURE_KEY, blockedPageReducer),
StoreModule.forFeature(SCAN_TYPE_FEATURE_KEY, scanTypeReducer),
StoreModule.forFeature(COMIC_FORMAT_FEATURE_KEY, comicFormatReducer),
EffectsModule.forFeature([
DisplayEffects,
ComicImportEffects,
LibraryEffects,
ScrapingEffects,
BlockedPageEffects,
ScanTypeEffects
ScanTypeEffects,
ComicFormatEffects
]),
MatInputModule,
MatSelectModule,
Expand Down
102 changes: 102 additions & 0 deletions comixed-web/src/app/library/reducers/comic-format.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2021, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import {
ComicFormatState,
initialState,
reducer
} from './comic-format.reducer';
import { FORMAT_1, FORMAT_3, FORMAT_5 } from '@app/library/library.fixtures';
import {
comicFormatAdded,
resetComicFormats
} from '@app/library/actions/comic-format.actions';

describe('ComicFormat Reducer', () => {
let state: ComicFormatState;

beforeEach(() => {
state = { ...initialState };
});

describe('the initial state', () => {
beforeEach(() => {
state = reducer({ ...state }, {} as any);
});

it('has no formats', () => {
expect(state.formats).toEqual([]);
});
});

describe('resetting the state', () => {
beforeEach(() => {
state = reducer(
{ ...state, formats: [FORMAT_1, FORMAT_3, FORMAT_5] },
resetComicFormats()
);
});

it('clears the formats', () => {
expect(state.formats).toEqual([]);
});
});

describe('adding a new comic format', () => {
const EXISTING_FORMAT = FORMAT_1;
const NEW_FORMAT = FORMAT_3;

beforeEach(() => {
state = reducer(
{ ...state, formats: [EXISTING_FORMAT] },
comicFormatAdded({ format: NEW_FORMAT })
);
});

it('retains the existing format', () => {
expect(state.formats).toContain(EXISTING_FORMAT);
});

it('adds the new format', () => {
expect(state.formats).toContain(NEW_FORMAT);
});
});

describe('updating an exisiting comic format', () => {
const EXISTING_FORMAT = FORMAT_1;
const UPDATED_FORMAT = {
...EXISTING_FORMAT,
name: EXISTING_FORMAT.name.substr(1)
};

beforeEach(() => {
state = reducer(
{ ...state, formats: [EXISTING_FORMAT] },
comicFormatAdded({ format: UPDATED_FORMAT })
);
});

it('removes the existing format', () => {
expect(state.formats).not.toContain(EXISTING_FORMAT);
});

it('adds the updated format', () => {
expect(state.formats).toContain(UPDATED_FORMAT);
});
});
});
47 changes: 47 additions & 0 deletions comixed-web/src/app/library/reducers/comic-format.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2021, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

import { createReducer, on } from '@ngrx/store';
import {
comicFormatAdded,
resetComicFormats
} from '../actions/comic-format.actions';
import { ComicFormat } from '@app/library';

export const COMIC_FORMAT_FEATURE_KEY = 'comic_format_state';

export interface ComicFormatState {
formats: ComicFormat[];
}

export const initialState: ComicFormatState = {
formats: []
};

export const reducer = createReducer(
initialState,

on(resetComicFormats, state => ({ ...state, formats: [] })),
on(comicFormatAdded, (state, action) => {
const formats = state.formats.filter(
format => format.id !== action.format.id
);
formats.push(action.format);
return { ...state, formats };
})
);

0 comments on commit b1dc713

Please sign in to comment.