Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,34 @@
package com.codedifferently.lesson23.web;
import java.util.UUID;

import com.codedifferently.lesson23.library.MediaItem;
import com.codedifferently.lesson23.library.Book;
import java.util.List;
import lombok.Builder;
import lombok.Value;
import jakarta.validation.constraints.NotNull;

@Value
@Builder
public class AddMediaItemRequest {
@NotNull
Item item;

@Value
@Builder
public static class Item {
String id;
String type;
String title;
String isbn;
List<String> authors;
Integer pages;

public MediaItem toDomain() {
int p = pages == null ? 0 : pages;
return new Book(UUID.fromString(id), title, isbn, authors, p);
}
}

public Item getItem() { return item; }
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package com.codedifferently.lesson23.web;

import java.util.Collections;
import java.util.Map;
import com.codedifferently.lesson23.library.Librarian;
import java.util.UUID;

import com.codedifferently.lesson23.library.Library;
import com.codedifferently.lesson23.library.MediaItem;
import com.codedifferently.lesson23.library.search.SearchCriteria;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class MediaItemsController {

private final Library library;
private final Librarian librarian;

public MediaItemsController(Library library) throws IOException {
this.library = library;
this.librarian = library.getLibrarians().stream().findFirst().orElseThrow();
}

@GetMapping("/items")
Expand All @@ -31,4 +37,33 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@GetMapping("/items/{id}")
public ResponseEntity<Map<String, MediaItemResponse>> getItem(@PathVariable String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}
MediaItem item = items.iterator().next();
var body = Collections.singletonMap("item", MediaItemResponse.from(item));
return ResponseEntity.ok(body);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should just return MediaItemResponse.

}

@PostMapping("/items")
public ResponseEntity<Map<String, MediaItemResponse>> addItem(@Valid @RequestBody AddMediaItemRequest request) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the existing CreateMediaItemRequest and CreateMediaItemResponse types.

MediaItem item = request.getItem().toDomain();
library.addMediaItem(item, new Librarian("System", "[email protected]"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a librarian instance in the class you can (and should) use.

var body = Collections.singletonMap("item", MediaItemResponse.from(item));
return ResponseEntity.ok(body);
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable String id) {
Set<MediaItem> items = library.search(SearchCriteria.builder().id(id).build());
if (items.isEmpty()) {
return ResponseEntity.notFound().build();
}
library.removeMediaItem(UUID.fromString(id), new Librarian("System", "[email protected]"));
return ResponseEntity.noContent().build();
}
}
Loading