All files / src/api movie.ts

50% Statements 10/20
0% Branches 0/12
10% Functions 1/10
50% Lines 10/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                      5x                                                       5x                           5x                                   5x                                     5x 3x                 5x                                     5x                                     5x                       5x            
import axios from "./axiosInstance";
import { DateTime } from "luxon";
import {
  IGenre,
  IPopularMoviesParams,
  IFavoriteMoviePayload,
  IWatchListPayload,
  IRatingPayload,
  IDeleteRatingPayload,
  IMovie,
  IMovieResponse,
} from "@/interfaces";
 
/**
 * Fetches a list of movies based on various filters.
 *
 * @param {IPopularMoviesParams} params - The parameters for fetching popular movies.
 * @returns {Promise<IMovie[]>} A promise that resolves to a list of movies.
 */
export const getMovies = ({
  sortBy,
  selectedGenres,
  startDate,
  endDate,
  selectedLanguage,
  page,
}: IPopularMoviesParams): Promise<IMovie[]> => {
  return axios.get(`discover/movie?language=${selectedLanguage}`, {
    params: {
      page: page,
      sort_by: typeof sortBy === "object" && "key" in sortBy ? sortBy.key : sortBy,
      with_genres: selectedGenres?.map((obj: IGenre) => obj.id).join(",") || null,
      "primary_release_date.gte": startDate
        ? DateTime.fromISO(startDate).toFormat("yyyy-MM-dd")
        : null,
      "primary_release_date.lte": endDate ? DateTime.fromISO(endDate).toFormat("yyyy-MM-dd") : null,
    },
  });
};
 
/**
 * Fetches a list of upcoming movies.
 *
 * @param {string} selectedLanguage - The language for the movie data.
 * @param {number} [page=1] - The page number for pagination.
 * @returns {Promise<IMovie[]>} A promise that resolves to a list of upcoming movies.
 */
export const getUpcomingMovies = (
  selectedLanguage: string,
  page: number = 1
): Promise<IMovie[]> => {
  return axios.get(`/movie/upcoming?language=${selectedLanguage}&page=${page}`);
};
 
/**
 * Searches for movies based on a query string.
 *
 * @param {string} query - The search query.
 * @param {number} [page=1] - The page number for pagination.
 * @returns {Promise<IMovie[]>} A promise that resolves to a list of movies matching the query.
 */
export const searchMovies = async (query: string, page: number = 1): Promise<IMovie[]> => {
  const response = await axios.get("/search/movie", {
    params: {
      query: query,
      page: page,
    },
  });
  return response.data;
};
 
/**
 * Fetches details for a specific movie.
 *
 * @param {string | undefined} movieId - The ID of the movie.
 * @param {string} sessionId - The session ID for the user.
 * @param {string} selectedLanguage - The language for the movie data.
 * @returns {Promise<IMovie>} A promise that resolves to the movie details.
 */
export const getMovie = (
  movieId: string | undefined,
  sessionId: string,
  selectedLanguage: string
): Promise<IMovie> => {
  return axios.get(`movie/${movieId}?language=${selectedLanguage}`, {
    params: {
      session_id: sessionId,
      append_to_response: "credits,keywords,reviews,recommendations,account_states",
    },
  });
};
 
/**
 * Fetches a list of movie genres.
 *
 * @param {string} selectedLanguage - The language for the genre data.
 * @returns {Promise<any>} A promise that resolves to the list of genres.
 */
export const getGenres = (selectedLanguage: string): Promise<any> => {
  return axios.get(`/genre/movie/list?language=${selectedLanguage}`);
};
 
/**
 * Marks a movie as favorite for the user.
 *
 * @param {IFavoriteMoviePayload} payload - The payload containing account ID, session ID, movie ID, and favorite status.
 * @returns {Promise<IMovieResponse>} A promise that resolves to the response of the favorite action.
 */
export const setFavoriteMovie = ({
  accountId,
  sessionId,
  id,
  favorite,
}: IFavoriteMoviePayload): Promise<IMovieResponse> => {
  return axios.post(`/account/${accountId}/favorite?session_id=${sessionId}`, {
    media_type: "movie",
    media_id: id,
    favorite: favorite,
  });
};
 
/**
 * Adds or removes a movie from the user's watchlist.
 *
 * @param {IWatchListPayload} payload - The payload containing account ID, session ID, movie ID, and watchlist status.
 * @returns {Promise<IMovieResponse>} A promise that resolves to the response of the watchlist action.
 */
export const setMovieInWatchList = ({
  accountId,
  sessionId,
  id,
  isInWatchlist,
}: IWatchListPayload): Promise<IMovieResponse> => {
  return axios.post(`/account/${accountId}/watchlist?session_id=${sessionId}`, {
    media_type: "movie",
    media_id: id,
    watchlist: isInWatchlist,
  });
};
 
/**
 * Rates a movie for the user.
 *
 * @param {IRatingPayload} payload - The payload containing movie ID, rating, and session ID.
 * @returns {Promise<IMovieResponse>} A promise that resolves to the response of the rating action.
 */
export const rateMovie = ({ id, rating, sessionId }: IRatingPayload): Promise<IMovieResponse> => {
  return axios.post(`/movie/${id}/rating?session_id=${sessionId}`, {
    value: rating,
  });
};
 
/**
 * Deletes the user's rating for a movie.
 *
 * @param {IDeleteRatingPayload} payload - The payload containing movie ID and session ID.
 * @returns {Promise<IMovieResponse>} A promise that resolves to the response of the delete rating action.
 */
export const deleteMovieRating = ({
  id,
  sessionId,
}: IDeleteRatingPayload): Promise<IMovieResponse> => {
  return axios.delete(`/movie/${id}/rating?session_id=${sessionId}`);
};