R
R
Raey2021-07-26 00:06:44
Node.js
Raey, 2021-07-26 00:06:44

typeorm. How to make this request?

There is a project on nest.js and a database on postgreSQL.
The entire code can be viewed here: https://github.com/Fblfbl/backend-movies/
There are 3 entities - users, movies and movie statuses for the user (status below).
Users cannot create films, they are just a database of films that users can add to favorites, to the list of views for the future and to history.
Entities have this form:

user
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  email: string;

  @Column()
  password: string;

  @Column()
  login: string;

  @OneToMany((type) => UserDetail, (userDetail) => userDetail.user)
  userDetails: UserDetail[];
}


movie
@Entity()
export class Movie {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  genre: string;

  @OneToOne((type) => UserDetail, (userDetail) => userDetail.movie)
  userDetails: UserDetail;
}


UserDetail(Status)
@Entity()
export class UserDetail {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: false })
  isFavorite: boolean;

  @Column({ default: false })
  isWatched: boolean;

  @Column({ default: false })
  isInWatchlist: boolean;

  @Column({ default: null })
  watchingDate: Date | null;

  @ManyToOne((type) => User, (user) => user.userDetails)
  user: User;
  @Column()
  userId: number;

  @ManyToOne((type) => Movie, (movie) => movie.userDetails)
  movie: Movie;
  @Column()
  movieId: number;

  @BeforeUpdate()
  updateDate() {
    if (this.isWatched && !this.watchingDate) {
      this.watchingDate = new Date();
    } else if (!this.isWatched) {
      this.watchingDate = null;
    }
  }
  @BeforeInsert()
  createDate() {
    this.updateDate();
  }
}


Initially, the status entity does not exist for either the movie or the user. It is only created when the user updates the status of the movie.

There are 2 routes:
'movies/all' returns simply all movies without statuses (for a situation if the user is not authorized)
'movies' should return all movies for the user with their statuses and without, if they are not yet for this user.

Actually a question . How can a user get all movies with a status and the rest without statuses? Examples below.

Those, let's say I logged in and get a list of films (For an example, there are 3 of them in total, I added 1 film to my favorites). I should return something like this:

An example of issuing movies with a logged in user
[
    {
        "id": 1,
        "title": "test1",
        "genre": "test1",
        "userDetails": {
            "id": 1,
            "isFavorite": true,
            "isWatched": false,
            "isInWatchlist": false,
            "watchingDate": null,
            "userId": 1,
            "movieId": 1
        }
    },
    {
        "id": 2,
        "title": "test2",
        "genre": "test2",
    },
    {
        "id": 3,
        "title": "test3",
        "genre": "test3",
    }
]


For an unlogged user, this should return:

An example of issuing films without a login
[
    {
        "id": 1,
        "title": "test1",
        "genre": "test1",
    },
    {
        "id": 2,
        "title": "test2",
        "genre": "test2",
    },
    {
        "id": 3,
        "title": "test3",
        "genre": "test3",
    }
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Golub, 2021-07-26
@RazerVG

What's the problem with doing it via leftJoin ?

this.lfMovieER.createQueryBuilder('m')
  .setParameter('userID', 82)
  .leftJoin(
    LfUserDetailEntity,
    'uD',
    'm.id = uD.movie_id AND uD.user_id = :userID'
  )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question