Trying to update a single movie property with PATCH

Hello, I'm building a REST API and I'm implementing a patchOneMovieById method, that takes an id and a an object of properties to update, I've split my code in the following files: movie.router.ts, movie.controller.ts, movie.service.ts and movie.repository.ts

Here's the code for the service and repository:

// MovieService Class
    patchOneById(id: Number, updatedMovieProps: Partial<Movie>) {
        const movie = this.getOneById(id);

        if (!movie) {
          return null;
        }
      
        const updatedMovie: Movie = { ...movie, ...updatedMovieProps };
        const updatedMovieInstance = this.movieRepository.updateOneById(id, updatedMovie);
      
        return updatedMovieInstance;
    }


// MovieRepository Class
    updateOneById(id: Number, updatedMovie: Movie) {
        const movieIndex = moviesData.findIndex((movie) => movie.id === id);

        if (movieIndex === -1) {
          return undefined;
        }
    
        const updatedMovieInstance: Movie = {
            ...moviesData[movieIndex],
            ...updatedMovie,
        };
    
        moviesData[movieIndex] = updatedMovieInstance;
    
        return updatedMovieInstance;
    }


The issue is, when I update a single property on a movie, the other properties are deleted and I get an output such as:

    {
        "id": 1,
        "summary": "I patched the summary only"
    },
    {
        "id": 2,
        "name": "movie2",
        "summary": "summary 2",
        "trailerURL": "https://www.youtube.com/"
    },
Was this page helpful?