Skip to content

Commit

Permalink
implemented zplex-api v1
Browse files Browse the repository at this point in the history
  • Loading branch information
itszechs committed Jun 21, 2024
1 parent 6c574f4 commit 497a927
Show file tree
Hide file tree
Showing 16 changed files with 680 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ apt-get clean && \
rm -rf /var/lib/apt/lists/*

COPY target/zplex-api*.jar /app/zplex-api.jar
CMD ["java", "-jar", "/app/zplex-api.jar", "--server.port=${PORT}"]
CMD ["java", "-jar", "/app/zplex-api.jar", "--server.port=${PORT}", "--spring.profiles.active=prod", "springdoc.api-docs.enabled=false", \
"--spring.application.name=${SPRING_APPLICATION_NAME}", \
"--spring.datasource.url=${SPRING_DATASOURCE_URL}", \
"--spring.datasource.username=${SPRING_DATASOURCE_USERNAME}", \
"--spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}"]
15 changes: 8 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
<java.version>21</java.version>
</properties>
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -35,16 +33,19 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>

</dependencies>

<build>
Expand Down
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 src/main/java/zechs/zplex/zplex_api/controller/ShowController.java
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 src/main/java/zechs/zplex/zplex_api/model/ErrorResponse.java
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;
}
}
72 changes: 72 additions & 0 deletions src/main/java/zechs/zplex/zplex_api/model/Movie.java
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 src/main/java/zechs/zplex/zplex_api/model/PaginatedResponse.java
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;
}
}
Loading

0 comments on commit 497a927

Please sign in to comment.