Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds MediaItemsController, PatronsController, and PatronsControllerTest #363

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import com.codedifferently.lesson16.library.Librarian;
import com.codedifferently.lesson16.library.Library;
import com.codedifferently.lesson16.library.MediaItem;
import com.codedifferently.lesson16.library.exceptions.MediaItemCheckedOutException;
import com.codedifferently.lesson16.library.search.SearchCriteria;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class MediaItemsController {

private final Library library;
private final Librarian librarian;

Expand All @@ -21,10 +27,46 @@ public MediaItemsController(Library library) throws IOException {
}

@GetMapping("/items")
public GetMediaItemsResponse getItems() {
public ResponseEntity<GetMediaItemsResponse> getItems() {
Set<MediaItem> items = library.search(SearchCriteria.builder().build());
List<MediaItemResponse> responseItems = items.stream().map(MediaItemResponse::from).toList();
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return response;
List<MediaItemResponse> responseItems =
items.stream().map(MediaItemResponse::from).collect(Collectors.toList());
GetMediaItemsResponse response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@PostMapping("/items")
public CreateMediaItemResponse createItem(@Valid @RequestBody CreateMediaItemRequest request) {
MediaItem item = MediaItemRequest.asMediaItem(request.getItem());
library.addMediaItem(item, librarian);
return CreateMediaItemResponse.builder().item(MediaItemResponse.from(item)).build();
}

@GetMapping("/items/{id}")
public ResponseEntity<MediaItemResponse> getMediaItem(@PathVariable UUID id) {
SearchCriteria criteria = SearchCriteria.builder().id(id.toString()).build();
Set<MediaItem> items = library.search(criteria);
if (!items.isEmpty()) {
MediaItemResponse response = MediaItemResponse.from(items.iterator().next());
return ResponseEntity.ok(response);
} else {
return ResponseEntity.notFound().build();
}
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable UUID id) {
SearchCriteria criteria = SearchCriteria.builder().id(id.toString()).build();
Set<MediaItem> items = library.search(criteria);
if (!items.isEmpty()) {
try {
library.removeMediaItem(id, new Librarian("Default", "librarian@example.com"));
cibezim marked this conversation as resolved.
Show resolved Hide resolved
return ResponseEntity.noContent().build();
} catch (MediaItemCheckedOutException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
} else {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.codedifferently.lesson16.web;

import com.codedifferently.lesson16.library.Library;
import com.codedifferently.lesson16.library.LibraryGuest;
import com.codedifferently.lesson16.library.exceptions.MediaItemCheckedOutException;
import com.codedifferently.lesson16.library.search.SearchCriteria;
import java.util.Collection;
import java.util.UUID;
import java.util.stream.Collectors;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/patrons")
public class PatronsController {

private final Library library;

public PatronsController(Library library) {
this.library = library;
}

@GetMapping
public ResponseEntity<Collection<LibraryGuest>> getPatrons() {
Collection<LibraryGuest> patrons = library.getPatrons();
return ResponseEntity.ok(patrons);
}

@PostMapping
public ResponseEntity<?> createPatron(@RequestBody LibraryGuest patron) {
cibezim marked this conversation as resolved.
Show resolved Hide resolved
library.addLibraryGuest(patron);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping("/{id}")
public ResponseEntity<LibraryGuest> getPatron(@PathVariable UUID id) {
SearchCriteria query = SearchCriteria.builder().id(id.toString()).build();
Collection<LibraryGuest> patrons =
library.search(query).stream()
.filter(item -> item instanceof LibraryGuest)
.map(item -> (LibraryGuest) item)
.collect(Collectors.toList());
if (!patrons.isEmpty()) {
return ResponseEntity.ok(patrons.iterator().next());
} else {
return ResponseEntity.notFound().build();
}
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> removePatron(@PathVariable UUID id) {
try {
library.removeLibraryGuest(id);
return ResponseEntity.noContent().build();
} catch (MediaItemCheckedOutException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
cibezim marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

import com.codedifferently.lesson16.library.Library;
import com.codedifferently.lesson16.library.LibraryGuest;
import com.codedifferently.lesson16.library.exceptions.MediaItemCheckedOutException;
import com.codedifferently.lesson16.web.PatronsController;
import java.util.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class PatronsControllerTest {
cibezim marked this conversation as resolved.
Show resolved Hide resolved

@Mock private Library library;

@InjectMocks private PatronsController patronsController;

@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
}

@Test
public void testGetPatrons() {
LibraryGuest patron1 = mock(LibraryGuest.class);
LibraryGuest patron2 = mock(LibraryGuest.class);
Set<LibraryGuest> patrons = new HashSet<>(Arrays.asList(patron1, patron2));
when(library.getPatrons()).thenReturn(patrons);

ResponseEntity<Collection<LibraryGuest>> response = patronsController.getPatrons();

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(patrons, response.getBody());
}

@Test
public void testCreatePatron() {

LibraryGuest patron = mock(LibraryGuest.class);
doNothing().when(library).addLibraryGuest(patron);

ResponseEntity<?> response = patronsController.createPatron(patron);

assertEquals(HttpStatus.CREATED, response.getStatusCode());
}

@Test
public void testRemovePatron() throws MediaItemCheckedOutException {

UUID id = UUID.randomUUID();
doNothing().when(library).removeLibraryGuest(id);

ResponseEntity<Void> response = patronsController.removePatron(id);

assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
}

@Test
public void testRemovePatronWithCheckedOutItems() throws MediaItemCheckedOutException {

UUID id = UUID.randomUUID();
doThrow(new MediaItemCheckedOutException("")).when(library).removeLibraryGuest(id);

ResponseEntity<Void> response = patronsController.removePatron(id);

assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}

@Test
public void testGetPatron() {

LibraryGuest mockPatron = mock(LibraryGuest.class);
Set<LibraryGuest> patrons = Collections.singleton(mockPatron);

when(library.getPatrons()).thenReturn(patrons);

ResponseEntity<Collection<LibraryGuest>> response = patronsController.getPatrons();

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(patrons, response.getBody());
}
}