feat(api): add getListens endpoint with pagination

This commit is contained in:
Julian Tölle 2020-05-02 20:03:43 +02:00
parent a770b48441
commit de6d057f80
7 changed files with 83 additions and 1 deletions

View file

@ -0,0 +1,5 @@
import { User } from "src/users/user.entity";
export interface GetListensDto {
user: User;
}

View file

@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing";
import { ListensController } from "./listens.controller";
describe("Listens Controller", () => {
let controller: ListensController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ListensController],
}).compile();
controller = module.get<ListensController>(ListensController);
});
it("should be defined", () => {
expect(controller).toBeDefined();
});
});

View file

@ -0,0 +1,24 @@
import { Controller, Get, Query } from "@nestjs/common";
import { Pagination } from "nestjs-typeorm-paginate";
import { Auth } from "../auth/decorators/auth.decorator";
import { ReqUser } from "../auth/decorators/req-user.decorator";
import { User } from "../users/user.entity";
import { Listen } from "./listen.entity";
import { ListensService } from "./listens.service";
@Controller("api/v1/listens")
export class ListensController {
constructor(private readonly listensService: ListensService) {}
@Get()
@Auth()
async getRecentlyPlayed(
@Query("page") page: number = 1,
@Query("limit") limit: number = 10,
@ReqUser() user: User
): Promise<Pagination<Listen>> {
limit = limit > 100 ? 100 : limit;
return this.listensService.getListens({ page, limit, user });
}
}

View file

@ -2,10 +2,12 @@ import { Module } from "@nestjs/common";
import { ListensService } from "./listens.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ListenRepository } from "./listen.repository";
import { ListensController } from "./listens.controller";
@Module({
imports: [TypeOrmModule.forFeature([ListenRepository])],
providers: [ListensService],
exports: [ListensService],
controllers: [ListensController],
})
export class ListensModule {}

View file

@ -1,7 +1,13 @@
import { Injectable } from "@nestjs/common";
import {
IPaginationOptions,
paginate,
Pagination,
} from "nestjs-typeorm-paginate";
import { CreateListenDto } from "./dto/create-listen.dto";
import { Listen } from "./listen.entity";
import { ListenRepository } from "./listen.repository";
import { CreateListenDto } from "./dto/create-listen.dto";
import { GetListensDto } from "./dto/get-listens.dto";
@Injectable()
export class ListensService {
@ -32,4 +38,25 @@ export class ListensService {
return listen;
}
async getListens(
options: GetListensDto & IPaginationOptions
): Promise<Pagination<Listen>> {
const { page, limit, user } = options;
const queryBuilder = this.listenRepository
.createQueryBuilder("l")
.leftJoin("l.user", "user")
.where("user.id = :userID", { userID: user.id })
.leftJoinAndSelect("l.track", "track")
.leftJoinAndSelect("track.artists", "artists")
.leftJoinAndSelect("track.album", "album")
.leftJoinAndSelect("album.artists", "albumArtists")
.orderBy("l.playedAt", "DESC");
return paginate<Listen>(queryBuilder, {
page,
limit,
});
}
}