Skip to content

Commit

Permalink
Added a new REST API for getting the comic formats [#465]
Browse files Browse the repository at this point in the history
 * Added a new controller and service.
 * Made the API auditable.
 * Removed the old REST API.
  • Loading branch information
mcpierce committed Oct 18, 2020
1 parent 832540c commit 3469523
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@
package org.comixedproject.repositories.comic;

import org.comixedproject.model.comic.ComicFormat;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
* <code>ComicFormatRepository</code> provides methods for working with persisted instances of
* {@link ComicFormat}.
*
* @author Darryl L. Pierce
*/
@Repository
public interface ComicFormatRepository extends CrudRepository<ComicFormat, Long> {}
public interface ComicFormatRepository extends JpaRepository<ComicFormat, Long> {}
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,6 @@ public Comic getComic(Principal principal, @PathVariable("id") long id) throws C
return result;
}

@GetMapping(value = "/formats")
public Iterable<ComicFormat> getComicFormats() {
log.debug("Fetching all comic format types");
return this.comicFormatRepository.findAll();
}

@PostMapping(
value = "/since/{timestamp}",
produces = MediaType.APPLICATION_JSON_VALUE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, 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>
*/

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.model.net.ApiResponse;
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.web.bind.annotation.RestController;

/**
* <code>ComicFormatController</code> provides a set of REST APIs for working with the instances of
* {@link ComicFormat}.
*
* @author Darryl L. Pierce
*/
@RestController
@Log4j2
public class ComicFormatController {
@Autowired private ComicFormatService comicFormatService;

/**
* Returns the list of all defined comic formats.
*
* @return the list
*/
@GetMapping(value = "/api/comics/formats", produces = MediaType.APPLICATION_JSON_VALUE)
@AuditableEndpoint
public ApiResponse<List<ComicFormat>> getAll() {
log.info("Retrieving the list of comic formats");
return new ApiResponse<>(this.comicFormatService.getAll());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, 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>
*/

package org.comixedproject.controller.comic;

import static junit.framework.TestCase.*;

import java.util.List;
import org.comixedproject.model.comic.ComicFormat;
import org.comixedproject.model.net.ApiResponse;
import org.comixedproject.service.comic.ComicFormatService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ComicFormatControllerTest {
@InjectMocks private ComicFormatController controller;
@Mock private ComicFormatService comicFormatService;
@Mock private List<ComicFormat> comicFormatList;

@Test
public void testGetAll() {
Mockito.when(comicFormatService.getAll()).thenReturn(comicFormatList);

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

assertNotNull(result);
assertTrue(result.isSuccess());
assertSame(comicFormatList, result.getResult());

Mockito.verify(comicFormatService, Mockito.times(1)).getAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, 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>
*/

package org.comixedproject.service.comic;

import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.model.comic.ComicFormat;
import org.comixedproject.repositories.comic.ComicFormatRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* <code>ComicFormatService</code> provides business logic when working with instances of {@link
* ComicFormat}.
*
* @author Darryl L. Pierce
*/
@Service
@Log4j2
public class ComicFormatService {
@Autowired private ComicFormatRepository comicFormatRepository;

public List<ComicFormat> getAll() {
log.debug("Getting all comic formts");
return this.comicFormatRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, 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>
*/

package org.comixedproject.service.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.repositories.comic.ComicFormatRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ComicFormatServiceTest {
@InjectMocks private ComicFormatService service;
@Mock private ComicFormatRepository comicFormatRepository;
@Mock private List<ComicFormat> comicFormatList;

@Test
public void testGetAll() {
Mockito.when(comicFormatRepository.findAll()).thenReturn(comicFormatList);

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

assertNotNull(result);
assertSame(comicFormatList, result);

Mockito.verify(comicFormatRepository, Mockito.times(1)).findAll();
}
}

0 comments on commit 3469523

Please sign in to comment.