-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
228 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,52 @@ | ||
import { Controller, UseGuards } from '@nestjs/common'; | ||
import { Controller, Get, Logger, Req, UnauthorizedException, UseGuards } from '@nestjs/common'; | ||
import { ReleaseOut, SpinEntity, SpinOut, UserEntity } from '@spin-cycle-mono/shared'; | ||
import { Request } from 'express'; | ||
|
||
import { AuthGuard } from '../auth/auth.guard'; | ||
import { DiscogsService } from '../discogs/discogs.service'; | ||
import { UserService } from '../users/user.service'; | ||
import { AllReleasesSpunException } from './spins.exception'; | ||
import { SpinsService } from './spins.service'; | ||
|
||
@Controller('/spins') | ||
@UseGuards(AuthGuard) | ||
export class SpinsController {} | ||
export class SpinsController { | ||
constructor( | ||
private readonly discogsService: DiscogsService, | ||
private readonly spinsService: SpinsService, | ||
private readonly userService: UserService, | ||
) {} | ||
|
||
@Get('/random') | ||
async getRandomRelease(@Req() req: Request): Promise<SpinOut> { | ||
const user: UserEntity = await this.userService | ||
.findByIdWithSpins(req['user'].sub) | ||
.catch((e) => this.handleUnauthorized(e)); | ||
|
||
const nextSpin: ReleaseOut | null = await this.discogsService | ||
.getRandomRecord(user) | ||
.catch((e) => this.handleAllRecordsPlayed(e)); | ||
|
||
if (!nextSpin) { | ||
return null; | ||
} | ||
|
||
const { discogsId, artistName, recordName } = nextSpin; | ||
const releaseSpin: SpinEntity = new SpinEntity(null, user, discogsId, artistName, recordName, new Date()); | ||
const savedSpin: SpinEntity = await this.spinsService.create(releaseSpin); | ||
return SpinOut.fromSpin(savedSpin); | ||
} | ||
|
||
private handleAllRecordsPlayed(e: unknown): null { | ||
if (e instanceof AllReleasesSpunException) { | ||
Logger.error(e); | ||
return null; | ||
} | ||
throw e; | ||
} | ||
|
||
private handleUnauthorized(e: unknown): null { | ||
Logger.error(e); | ||
throw new UnauthorizedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class AllReleasesSpunException extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { SpinEntity } from '@spin-cycle-mono/shared'; | ||
|
||
import { DiscogsModule } from '../discogs/discogs.module'; | ||
import { UserModule } from '../users/user.module'; | ||
import { SpinsController } from './spins.controller'; | ||
import { SpinsService } from './spins.service'; | ||
|
||
@Module({ | ||
controllers: [SpinsController], | ||
imports: [UserModule, DiscogsModule], | ||
providers: [SpinsService], | ||
imports: [TypeOrmModule.forFeature([SpinEntity]), UserModule, DiscogsModule], | ||
exports: [SpinsService], | ||
}) | ||
export class SpinsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export interface IDiscogsReleases extends IDiscogsResponse { | ||
releases: IDiscogsRelease[]; | ||
} | ||
|
||
export interface IDiscogsRelease { | ||
id: number; | ||
basic_information: { | ||
title: string; | ||
resource_url: string; | ||
artists: Array<{ name: string }>; | ||
}; | ||
} | ||
|
||
interface IDiscogsResponse { | ||
pagination: IDiscogsPagination; | ||
} | ||
|
||
interface IDiscogsPagination { | ||
page: number; | ||
pages: number; | ||
per_page: number; | ||
items: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IDiscogsRelease } from '../discogs-response'; | ||
|
||
export class ReleaseOut { | ||
discogsId: number; | ||
artistName: string; | ||
recordName: string; | ||
|
||
constructor(discogsId: number, artistName: string, recordName: string) { | ||
this.discogsId = discogsId; | ||
this.artistName = artistName; | ||
this.recordName = recordName; | ||
} | ||
|
||
static fromDiscogsResponse(release: IDiscogsRelease): ReleaseOut { | ||
const joinedArtists: string = release.basic_information.artists.map((artist) => artist.name).join(', '); | ||
return new ReleaseOut(release.id, joinedArtists, release.basic_information.title); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
import { SpinEntity } from '../entities/spin.entity'; | ||
|
||
export class SpinOut { | ||
id: number; | ||
discogsId: number; | ||
artistName: string; | ||
recordName: string; | ||
createdAt: Date; | ||
played: boolean; | ||
|
||
constructor(artistName: string, recordName: string, createdAt: Date, played: boolean) { | ||
constructor(id: number, discogsId: number, artistName: string, recordName: string, createdAt: Date, played: boolean) { | ||
this.id = id; | ||
this.discogsId = discogsId; | ||
this.artistName = artistName; | ||
this.recordName = recordName; | ||
this.createdAt = createdAt; | ||
this.played = played; | ||
} | ||
|
||
static fromSpin(spin: SpinEntity): SpinOut { | ||
return new SpinOut(spin.id, spin.discogsId, spin.artistName, spin.recordName, spin.createdAt, spin.played); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters