qnote/web/src/lib/logic/model.ts

157 lines
4.0 KiB
TypeScript

export interface ApiUserResponse {
id: string
username: string
is_admin: boolean
created_at: string
updated_at: string
}
export class User {
id: string
username: string
isAdmin: boolean
createdAt: Date
updatedAt: Date
constructor(apiResponse: ApiUserResponse) {
this.id = apiResponse.id
this.username = apiResponse.username
this.isAdmin = apiResponse.is_admin
this.createdAt = new Date(apiResponse.created_at)
this.updatedAt = new Date(apiResponse.updated_at)
}
static fromApiResponseArray(apiResponses: ApiUserResponse[]): User[] {
return apiResponses.map((res) => new User(res))
}
}
export interface ApiFullNoteResponse {
note_id: string
owner_id: string
title: string
content: string
version_number: number
version_created_at: string
note_expires_at: string
note_created_at: string
note_updated_at: string
}
export class FullNote {
id: string
owner: string
title: string
content: string
versionNumber: number
versionCreatedAt: Date
isActiveVersion: boolean
noteExpiresAt: Date | null
noteCreatedAt: Date
noteUpdatedAt: Date
constructor(apiResponse: ApiFullNoteResponse) {
this.id = apiResponse.note_id
this.owner = apiResponse.owner_id
this.title = apiResponse.title
this.content = apiResponse.content
this.versionNumber = apiResponse.version_number
this.versionCreatedAt = new Date(apiResponse.version_created_at)
this.isActiveVersion = true // this endpoint serves only the latest version
this.noteExpiresAt =
apiResponse.note_expires_at !== null ? new Date(apiResponse.note_expires_at) : null
this.noteCreatedAt = new Date(apiResponse.note_created_at)
this.noteUpdatedAt = new Date(apiResponse.note_updated_at)
}
public joinWithVersionResponse(apiResponse: ApiFullVersionResponse): FullNote {
const newNote = new FullNote({
note_id: this.id,
owner_id: this.owner,
title: apiResponse.title,
content: apiResponse.content,
version_number: apiResponse.version_number,
version_created_at: apiResponse.created_at,
note_expires_at: this.noteExpiresAt ? this.noteExpiresAt.toISOString() : "",
note_created_at: this.noteCreatedAt.toISOString(),
note_updated_at: this.noteUpdatedAt.toISOString()
})
// override the isActiveVersion property
newNote.isActiveVersion = this.versionNumber === apiResponse.version_number
return newNote
}
}
export interface ApiNoteMetadataResponse {
note_id: string
owner_id: string
title: string
expires_at: string
updated_at: string
}
export class NoteMetadata {
id: string
owner: string
title: string
expiresAt: Date | null
updatedAt: Date
constructor(apiResponse: ApiNoteMetadataResponse) {
this.id = apiResponse.note_id
this.owner = apiResponse.owner_id
this.title = apiResponse.title
this.expiresAt = apiResponse.expires_at !== null ? new Date(apiResponse.expires_at) : null
this.updatedAt = new Date(apiResponse.updated_at)
}
static fromApiResponseArray(apiResponses: ApiNoteMetadataResponse[]): NoteMetadata[] {
return apiResponses.map((res) => new NoteMetadata(res))
}
}
export interface NewNoteResponse {
title: string
content: string
}
export interface ApiVersionMetadataResponse {
version_id: string
title: string
version_number: number
created_at: string
}
export class VersionMetadata {
versionID: string
title: string
versionNumber: number
isActive: boolean
createdAt: Date
constructor(apiResponse: ApiVersionMetadataResponse, activeVersionNumber: number) {
this.versionID = apiResponse.version_id
this.title = apiResponse.title
this.versionNumber = apiResponse.version_number
this.isActive = activeVersionNumber === apiResponse.version_number
this.createdAt = new Date(apiResponse.created_at)
}
static fromApiResponseArray(
apiResponses: ApiVersionMetadataResponse[],
activeVersionNumber: number
): VersionMetadata[] {
return apiResponses.map((res) => new VersionMetadata(res, activeVersionNumber))
}
}
export interface ApiFullVersionResponse {
version_id: string
title: string
content: string
version_number: number
created_at: string
}