Skip to content

Feat lesson 26 - Library Springboot Integration #605

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

Closed
Closed
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
7 changes: 5 additions & 2 deletions lesson_26/api/java/api_app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ plugins {
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.4.0"
id("com.adarshr.test-logger") version "4.0.0"
id("io.freefair.lombok") version "8.6"
}

apply(plugin = "io.spring.dependency-management")
Expand Down Expand Up @@ -39,6 +38,10 @@ dependencies {
implementation("org.apache.commons:commons-csv:1.10.0")
implementation("org.xerial:sqlite-jdbc:3.36.0")
implementation("org.hibernate.orm:hibernate-community-dialects:6.2.7.Final")
compileOnly("org.projectlombok:lombok:1.18.38")
annotationProcessor("org.projectlombok:lombok:1.18.38")
testCompileOnly("org.projectlombok:lombok:1.18.38")
testAnnotationProcessor("org.projectlombok:lombok:1.18.38")
}

application {
Expand Down Expand Up @@ -76,4 +79,4 @@ configure<com.diffplug.gradle.spotless.SpotlessExtension> {
// fix formatting of type annotations
formatAnnotations()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.UUID;
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;

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

@GetMapping("/items/{id}")
public ResponseEntity<MediaItemResponse> getItemById(@PathVariable UUID id) {
SearchCriteria criteria = SearchCriteria.builder().id(id.toString()).build();
Set<MediaItem> items = library.search(criteria);

if (items.isEmpty()) {
return ResponseEntity.notFound().build();
} else {
MediaItem item = items.iterator().next();
MediaItemResponse response = MediaItemResponse.from(item);
return ResponseEntity.ok(response);
}
}

@PostMapping("/items")
public CreateMediaItemResponse addItem(@RequestBody CreateMediaItemRequest request) {
MediaItemRequest itemCreation = request.getItem();
MediaItem item = MediaItemRequest.asMediaItem(itemCreation);
library.addMediaItem(item, librarian);

CreateMediaItemResponse response =
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 a static method called from() in the MediaItemResponse class that would be more appropriate in this line:
CreateMediaItemResponse.builder().item(getItemById(item.getId()).getBody()).build();

CreateMediaItemResponse.builder().item(getItemById(item.getId()).getBody()).build();
return response;
}

@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()) {
return ResponseEntity.notFound().build();
} else {
MediaItem item = items.iterator().next();
library.removeMediaItem(item, librarian);

return ResponseEntity.noContent().build();
}
}
}
Loading