-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
680 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/zechs/zplex/zplex_api/controller/MovieController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package zechs.zplex.zplex_api.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import zechs.zplex.zplex_api.model.ErrorResponse; | ||
import zechs.zplex.zplex_api.model.Movie; | ||
import zechs.zplex.zplex_api.model.PaginatedResponse; | ||
import zechs.zplex.zplex_api.service.ZPlexService; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class MovieController { | ||
|
||
private final ZPlexService zplexService; | ||
|
||
@Autowired | ||
public MovieController(ZPlexService zplexService) { | ||
this.zplexService = zplexService; | ||
} | ||
|
||
@GetMapping("/movies") | ||
public ResponseEntity<Object> getMovies( | ||
@RequestParam(defaultValue = "") String query, | ||
@RequestParam(defaultValue = "0") int pageSize, | ||
@RequestParam(defaultValue = "1") int pageNumber | ||
) { | ||
try { | ||
if (pageSize < 0 || pageNumber <= 0) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse("Page size must be greater than or equal to zero, and page number must be greater than zero.")); | ||
} | ||
|
||
List<Movie> movies; | ||
int pageCount = 0; | ||
if (pageSize == 0) { | ||
movies = zplexService.getAllMovies(query); | ||
} else { | ||
pageCount = zplexService.getMoviesPageCount(query, pageSize); | ||
if (pageNumber > pageCount) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse("Page number is greater than the total number of pages.")); | ||
} | ||
movies = zplexService.getMovies(query, pageSize, pageNumber); | ||
} | ||
|
||
return ResponseEntity.status(HttpStatus.OK) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new PaginatedResponse<>(movies, pageNumber, pageCount)); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/zechs/zplex/zplex_api/controller/ShowController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package zechs.zplex.zplex_api.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import zechs.zplex.zplex_api.model.ErrorResponse; | ||
import zechs.zplex.zplex_api.model.PaginatedResponse; | ||
import zechs.zplex.zplex_api.model.tv.Episode; | ||
import zechs.zplex.zplex_api.model.tv.Season; | ||
import zechs.zplex.zplex_api.model.tv.Show; | ||
import zechs.zplex.zplex_api.service.ZPlexService; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
public class ShowController { | ||
|
||
private final ZPlexService zplexService; | ||
|
||
@Autowired | ||
public ShowController(ZPlexService zplexService) { | ||
this.zplexService = zplexService; | ||
} | ||
|
||
@GetMapping("/shows") | ||
public ResponseEntity<Object> getShows( | ||
@RequestParam(defaultValue = "") String query, | ||
@RequestParam(defaultValue = "0") int pageSize, | ||
@RequestParam(defaultValue = "1") int pageNumber | ||
) { | ||
try { | ||
if (pageSize < 0 || pageNumber <= 0) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse("Page size must be greater than or equal to zero, and page number must be greater than zero.")); | ||
} | ||
|
||
List<Show> shows; | ||
int pageCount = 0; | ||
if (pageSize == 0) { | ||
shows = zplexService.getAllShows(query); | ||
} else { | ||
pageCount = zplexService.getShowsPageCount(query, pageSize); | ||
if (pageNumber > pageCount) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse("Page number is greater than the total number of pages.")); | ||
} | ||
shows = zplexService.getShows(query, pageSize, pageNumber); | ||
} | ||
|
||
return ResponseEntity.status(HttpStatus.OK) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new PaginatedResponse<>(shows, pageNumber, pageCount)); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
@GetMapping("/shows/{id}/seasons") | ||
public ResponseEntity<Object> getShowSeasons(@PathVariable("id") int showId) { | ||
try { | ||
List<Season> seasons = zplexService.getSeasons(showId); | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(seasons); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse(e.getMessage())); | ||
} | ||
} | ||
|
||
@GetMapping("/shows/{id}/seasons/{seasonId}") | ||
public ResponseEntity<Object> getSeasonEpisodes( | ||
@PathVariable("id") int showId, | ||
@PathVariable("seasonId") int seasonId) { | ||
try { | ||
List<Episode> episodes = zplexService.getEpisodes(seasonId); | ||
return ResponseEntity.status(HttpStatus.OK) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(episodes); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(new ErrorResponse(e.getMessage())); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/zechs/zplex/zplex_api/model/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package zechs.zplex.zplex_api.model; | ||
|
||
public class ErrorResponse { | ||
|
||
private String message; | ||
private String details; | ||
|
||
public ErrorResponse(String message, String details) { | ||
this.message = message; | ||
this.details = details; | ||
} | ||
|
||
public ErrorResponse(String details) { | ||
this.message = "An error occurred"; | ||
this.details = details; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getDetails() { | ||
return details; | ||
} | ||
|
||
public void setDetails(String details) { | ||
this.details = details; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package zechs.zplex.zplex_api.model; | ||
|
||
public class Movie { | ||
|
||
private Long id; | ||
private String title; | ||
private String posterPath; | ||
private Double voteAverage; | ||
private Integer year; | ||
private String fileId; | ||
|
||
public Movie() { | ||
} | ||
|
||
public Movie(Long id, String title, String posterPath, Double voteAverage, Integer year, String fileId) { | ||
this.id = id; | ||
this.title = title; | ||
this.posterPath = posterPath; | ||
this.voteAverage = voteAverage; | ||
this.year = year; | ||
this.fileId = fileId; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getPosterPath() { | ||
return posterPath; | ||
} | ||
|
||
public void setPosterPath(String posterPath) { | ||
this.posterPath = posterPath; | ||
} | ||
|
||
public Double getVoteAverage() { | ||
return voteAverage; | ||
} | ||
|
||
public void setVoteAverage(Double voteAverage) { | ||
this.voteAverage = voteAverage; | ||
} | ||
|
||
public Integer getYear() { | ||
return year; | ||
} | ||
|
||
public void setYear(Integer year) { | ||
this.year = year; | ||
} | ||
|
||
public String getFileId() { | ||
return fileId; | ||
} | ||
|
||
public void setFileId(String fileId) { | ||
this.fileId = fileId; | ||
} | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
src/main/java/zechs/zplex/zplex_api/model/PaginatedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package zechs.zplex.zplex_api.model; | ||
|
||
import java.util.List; | ||
|
||
public class PaginatedResponse<T> { | ||
|
||
private List<T> data; | ||
private int pageNumber; | ||
private int pageCount; | ||
|
||
public PaginatedResponse(List<T> data, int pageNumber, int pageCount) { | ||
this.data = data; | ||
this.pageNumber = pageNumber; | ||
this.pageCount = pageCount; | ||
} | ||
|
||
public List<T> getData() { | ||
return data; | ||
} | ||
|
||
public void setData(List<T> data) { | ||
this.data = data; | ||
} | ||
|
||
public int getPageNumber() { | ||
return pageNumber; | ||
} | ||
|
||
public void setPageNumber(int pageNumber) { | ||
this.pageNumber = pageNumber; | ||
} | ||
|
||
public int getPageCount() { | ||
return pageCount; | ||
} | ||
|
||
public void setPageCount(int pageCount) { | ||
this.pageCount = pageCount; | ||
} | ||
} |
Oops, something went wrong.