Skip to content

Commit

Permalink
add ts types, change spotify link to link and account tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
IkeHunter committed Oct 30, 2024
1 parent 6dff2ce commit 560c207
Show file tree
Hide file tree
Showing 21 changed files with 255 additions and 144 deletions.
7 changes: 5 additions & 2 deletions src/jukebox/dto/add-jukebox-link.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsEmail } from 'class-validator'

export class AddJukeboxLinkDto {
export class AddJukeboxLinkDto implements Partial<IJukeboxLink> {
@ApiProperty()
type: JukeboxLinkType

@ApiProperty()
@IsEmail()
spotifyEmail: string
email: string
}
2 changes: 1 addition & 1 deletion src/jukebox/dto/create-jukebox.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsNumber, IsString } from 'class-validator'

export class CreateJukeboxDto {
export class CreateJukeboxDto implements Partial<IJukebox> {
@ApiProperty()
@IsString()
name: string
Expand Down
52 changes: 32 additions & 20 deletions src/jukebox/dto/jukebox.dto.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
import { ApiProperty } from '@nestjs/swagger'
import { Expose } from 'class-transformer'
import { BaseDto } from 'src/config/dtos'
import { SpotifyLinkDto, SpotifyLinkNestedDto } from 'src/spotify/dto/spotify-link.dto'
import { Jukebox } from '../entities/jukebox.entity'

export class JukeboxDto extends BaseDto {
export class JukeboxLinkDto implements IJukeboxLink {
@Expose()
@ApiProperty()
id: number

@Expose()
@ApiProperty()
name: string
type: JukeboxLinkType

@Expose()
@ApiProperty()
club_id: number
email: string

@Expose()
@ApiProperty()
active: boolean
}

export class JukeboxDto extends BaseDto implements IJukebox {
@Expose()
@ApiProperty()
spotify_links: SpotifyLinkNestedDto[]
id: number

@Expose()
@ApiProperty()
name: string

@Expose()
@ApiProperty()
club_id: number

@Expose()
@ApiProperty()
active_spotify_link?: SpotifyLinkDto
links: JukeboxLinkDto[]

static serialize(entity: Jukebox): JukeboxDto {
return {
...super.serialize(entity),
name: entity.name,
club_id: entity.club_id,
spotify_links:
entity.spotify_link_assignments?.map((assignment) => ({
spotify_email: assignment.spotify_link.spotify_email,
})) ?? [],
active_spotify_link: entity.spotify_link_assignments?.find((assignment) => assignment.active)
?.spotify_link,
}
}
// static serialize(entity: Jukebox): JukeboxDto {
// return {
// ...super.serialize(entity),
// name: entity.name,
// club_id: entity.club_id,
// links:
// entity.spotify_link_assignments?.map((assignment) => ({
// type: 'spotify',
// email: assignment.spotify_link.spotify_email,
// active: assignment.active,
// })) ?? [],
// }
// }
}
10 changes: 2 additions & 8 deletions src/jukebox/dto/update-jukebox.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { ApiProperty, PartialType } from '@nestjs/swagger'
import { SpotifyLinkNestedDto } from 'src/spotify/dto/spotify-link.dto'
import { CreateJukeboxDto } from './create-jukebox.dto'
import { ApiProperty } from '@nestjs/swagger'
import { IsOptional } from 'class-validator'

export class UpdateJukeboxDto {
export class UpdateJukeboxDto implements Partial<IJukebox> {
@IsOptional()
@ApiProperty()
name?: string

@IsOptional()
@ApiProperty()
active_spotify_link?: SpotifyLinkNestedDto
}
37 changes: 29 additions & 8 deletions src/jukebox/entities/jukebox.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseEntity } from 'src/config/entities'
import { SpotifyLink } from 'src/spotify/entities/spotify-link.entity'
import { SpotifyAccount } from 'src/spotify/entities/spotify-account.entity'
import {
Column,
Entity,
Expand All @@ -9,6 +9,7 @@ import {
PrimaryColumn,
PrimaryGeneratedColumn,
} from 'typeorm'
import { JukeboxDto, JukeboxLinkDto } from '../dto/jukebox.dto'

@Entity('jukebox')
export class Jukebox extends BaseEntity {
Expand All @@ -21,26 +22,46 @@ export class Jukebox extends BaseEntity {
@Column()
club_id: number

@OneToMany(() => JukeboxSpotifyLinkAssignment, (assignment) => assignment.jukebox)
spotify_link_assignments: JukeboxSpotifyLinkAssignment[]
@OneToMany(() => JukeboxLinkAssignment, (assignment) => assignment.jukebox)
link_assignments: JukeboxLinkAssignment[]

serialize(): JukeboxDto {
return {
id: this.id,
name: this.name,
club_id: this.club_id,
links: this.link_assignments?.map((assignment) => assignment.serialize()) ?? [],
created_at: this.created_at,
updated_at: this.updated_at,
}
}
}

@Entity('jukebox_spotify_link_assignment')
export class JukeboxSpotifyLinkAssignment extends BaseEntity {
@Entity('jukebox_link_assignment')
export class JukeboxLinkAssignment extends BaseEntity {
@PrimaryColumn({ name: 'jukebox_id' })
jukebox_id: number

@ManyToOne(() => Jukebox, (jukebox) => jukebox.spotify_link_assignments)
@ManyToOne(() => Jukebox, (jukebox) => jukebox.link_assignments)
@JoinColumn({ name: 'jukebox_id' })
jukebox: Jukebox

@PrimaryColumn({ name: 'spotify_link_id' })
spotify_link_id: number

@ManyToOne(() => SpotifyLink, (link) => link.jukebox_assignments)
@ManyToOne(() => SpotifyAccount, (link) => link.jukebox_assignments)
@JoinColumn({ name: 'spotify_link_id' })
spotify_link: SpotifyLink
spotify_link: SpotifyAccount

@Column({ default: false })
active: boolean

serialize(): JukeboxLinkDto {
return {
id: this.id,
type: 'spotify',
email: this.spotify_link.spotify_email,
active: this.active,
}
}
}
49 changes: 33 additions & 16 deletions src/jukebox/jukebox.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import {
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { CurrentUser } from '../auth/current-user.decorator'
import { SpotifyLinkDto } from '../spotify/dto/spotify-link.dto'
import { SpotifyService } from '../spotify/spotify.service'
import { AddJukeboxLinkDto } from './dto/add-jukebox-link.dto'
import { CreateJukeboxDto } from './dto/create-jukebox.dto'
import { JukeboxDto } from './dto/jukebox.dto'
import { JukeboxDto, JukeboxLinkDto } from './dto/jukebox.dto'
import { UpdateJukeboxDto } from './dto/update-jukebox.dto'
import { JukeboxService } from './jukebox.service'

Expand All @@ -29,19 +28,19 @@ export class JukeboxController {
@Post('jukeboxes/')
async create(@Body() createJukeboxDto: CreateJukeboxDto): Promise<JukeboxDto> {
const jbx = await this.jukeboxService.create(createJukeboxDto)
return JukeboxDto.serialize(jbx)
return jbx.serialize()
}

@Get('jukeboxes/')
async findAll(): Promise<JukeboxDto[]> {
const jbxs = await this.jukeboxService.findAll()
return jbxs.map((jbx) => JukeboxDto.serialize(jbx))
return jbxs.map((jbx) => jbx.serialize())
}

@Get('jukeboxes/:id/')
async findOne(@Param('id') id: number): Promise<JukeboxDto> {
const jbx = await this.jukeboxService.findOne(id)
return JukeboxDto.serialize(jbx)
return jbx.serialize()
}

@Patch('jukeboxes/:id/')
Expand All @@ -50,44 +49,62 @@ export class JukeboxController {
@Body() updateJukeboxDto: UpdateJukeboxDto,
): Promise<JukeboxDto> {
const jbx = await this.jukeboxService.update(id, updateJukeboxDto)
return JukeboxDto.serialize(jbx)
return jbx.serialize()
}

@Delete('jukeboxes/:id/')
async remove(@Param('id') id: number): Promise<JukeboxDto> {
const jbx = await this.jukeboxService.remove(id)
return JukeboxDto.serialize(jbx)
return jbx.serialize()
}

@Get('/:jukebox_id/links/')
getJukeboxLinks(@Param('jukebox_id') jukeboxId: number): Promise<SpotifyLinkDto[]> {
return this.jukeboxService.getJukeboxSpotifyLinks(jukeboxId)
getJukeboxLinks(@Param('jukebox_id') jukeboxId: number): Promise<JukeboxLinkDto[]> {
return this.jukeboxService.getJukeboxLinks(jukeboxId)
}

@Post('/:jukebox_id/links/')
async addLinkToJukebox(
@CurrentUser() user: IUser,
@Param('jukebox_id') jukeboxId: number,
@Body() spotifyLink: AddJukeboxLinkDto,
): Promise<SpotifyLinkDto> {
const link = await this.spotifyService.findOneUserLink(user.id, spotifyLink.spotifyEmail)
@Body() jukeboxLink: AddJukeboxLinkDto,
): Promise<JukeboxLinkDto> {
const link = await this.spotifyService.findOneUserLink(user.id, jukeboxLink.email)

if (!link) {
throw new NotFoundException(
`Spotify link with email ${spotifyLink.spotifyEmail} not found for current user.`,
`Spotify link with email ${jukeboxLink.email} not found for current user.`,
)
}

await this.jukeboxService.addSpotifyLinkToJukebox(jukeboxId, link)
return await this.jukeboxService.addLinkToJukebox(jukeboxId, link)
}

@Delete('/:jukebox_id/links/:id/')
async deleteJukeboxLink(@Param('jukebox_id') jukeboxId: number, @Param('id') linkId: number) {
const link = await this.jukeboxService.removeJukeboxLink(jukeboxId, linkId)
return link
}

@Get('/:jukebox_id/active-link/')
async refreshActiveJukeboxLink(@Param('jukebox_id') jukeboxId: number) {
async getActiveJukeboxLink(@Param('jukebox_id') jukeboxId: number) {
const link = await this.jukeboxService.getJukeboxActiveSpotifyLink(jukeboxId)
const refreshed = await this.spotifyService.refreshSpotifyLink(link)
if (!link) {
return
}

const refreshed = await this.spotifyService.refreshSpotifyLink(link)
return refreshed
}

@Post('/:jukebox_id/active-link/')
async setActiveJukeboxLink(
@Param('jukebox_id') jukeboxId: number,
@Body() jukeboxLink: AddJukeboxLinkDto,
) {
const link = await this.jukeboxService.findJukeboxLink(jukeboxId, jukeboxLink)
const activeLink = await this.jukeboxService.setActiveLink(jukeboxId, link.id)

return await this.spotifyService.refreshSpotifyLink(activeLink.spotify_link)
}
}
4 changes: 2 additions & 2 deletions src/jukebox/jukebox.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { SpotifyModule } from '../spotify/spotify.module'
import { Jukebox, JukeboxSpotifyLinkAssignment } from './entities/jukebox.entity'
import { Jukebox, JukeboxLinkAssignment } from './entities/jukebox.entity'
import { JukeboxController } from './jukebox.controller'
import { JukeboxService } from './jukebox.service'

Expand All @@ -10,7 +10,7 @@ import { JukeboxService } from './jukebox.service'
providers: [JukeboxService],
imports: [
// MongooseModule.forFeature([{ name: Jukebox.name, schema: JukeboxSchema }]),
TypeOrmModule.forFeature([Jukebox, JukeboxSpotifyLinkAssignment]),
TypeOrmModule.forFeature([Jukebox, JukeboxLinkAssignment]),
SpotifyModule,
],
exports: [JukeboxService],
Expand Down
Loading

0 comments on commit 560c207

Please sign in to comment.