Skip to content

feat: implements MediaItemsController #601

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
wants to merge 7 commits into from
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
1 change: 1 addition & 0 deletions lesson_26/api/java/api_app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -297,4 +298,12 @@ public String toString() {
+ guestsById
+ '}';
}

public List<MediaItem> getAllItems() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Careful when using the Quick Fix option without understanding what it does, it may automatically generate methods depending on what option you choose.

throw new UnsupportedOperationException("Not supported yet.");
}

public Object getAnItem(UUID id) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
import com.codedifferently.lesson26.library.Library;
import com.codedifferently.lesson26.library.MediaItem;
import com.codedifferently.lesson26.library.search.SearchCriteria;
import jakarta.validation.Valid;
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
Expand All @@ -31,4 +37,39 @@ public ResponseEntity<GetMediaItemsResponse> getItems() {
var response = GetMediaItemsResponse.builder().items(responseItems).build();
return ResponseEntity.ok(response);
}

@PostMapping("/items")
public ResponseEntity<CreateMediaItemResponse> postItem(
@Valid @RequestBody CreateMediaItemRequest request) {
MediaItemRequest itemRequest = request.getItem();
MediaItem newItem = MediaItemRequest.asMediaItem(itemRequest);
library.addMediaItem(newItem, librarian);
MediaItemResponse itemResponse = MediaItemResponse.from(newItem);
CreateMediaItemResponse response = CreateMediaItemResponse.builder().item(itemResponse).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()) {
MediaItem item = items.iterator().next();
MediaItemResponse response = MediaItemResponse.from(item);
return ResponseEntity.ok(response);
} else {
return ResponseEntity.notFound().build();
}
}

@DeleteMapping("/items/{id}")
public ResponseEntity<Void> deleteMediaItem(@PathVariable("id") UUID id) {
try {
library.removeMediaItem(id, librarian);
return ResponseEntity.noContent().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
}
}