Skip to content
This repository was archived by the owner on Feb 21, 2025. It is now read-only.

Commit

Permalink
lecture bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluerberry committed Nov 19, 2024
1 parent edbfe07 commit d798daf
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/scripts/controllers/DomainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class DomainController extends NodeController<DomainController> {

// Fix order of remaining domains
if (reorder_graph) {
await this.graph.reorder()
await this.graph.reorderDomains()
}

// Call the API to delete the domain
Expand Down
38 changes: 34 additions & 4 deletions src/lib/scripts/controllers/GraphController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class GraphController {

// Fetch lectures from the cache
this._lectures = this._lecture_ids.map(id => this.cache.findOrThrow(LectureController, id))
this._lectures.sort((a, b) => a.order - b.order)

return Array.from(this._lectures)
}

Expand Down Expand Up @@ -577,7 +579,7 @@ class GraphController {
if (this._subject_ids !== undefined)
promises.push(...this.subjects.map(async subject => await subject.delete()))
if (this._lecture_ids !== undefined)
promises.push(...this.lectures.map(async lecture => await lecture.delete()))
promises.push(...this.lectures.map(async lecture => await lecture.delete(false)))
await Promise.all(promises)

// Call the API to delete the graph
Expand All @@ -592,7 +594,7 @@ class GraphController {
this.cache.remove(this)
}

async reorder(domains?: DomainController[]) {
async reorderDomains(domains?: DomainController[]) {

// Update the graph
if (domains !== undefined) {
Expand All @@ -608,15 +610,43 @@ class GraphController {
}

// Call the API to reorder the graph
const response = await fetch(`/api/graph/${this.id}/reorder`, {
const response = await fetch(`/api/graph/${this.id}/reorder/domains`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ domain_ids: this.domain_ids })
})

// Throw an error if the API request fails
if (!response.ok) {
throw new Error(`APIError (/api/graph/${this.id}/reorder PUT): ${response.status} ${response.statusText}`)
throw new Error(`APIError (/api/graph/${this.id}/reorder/domains PUT): ${response.status} ${response.statusText}`)
}
}

async reorderLectures(lectures?: LectureController[]) {

// Update the graph
if (lectures !== undefined) {
this._lecture_ids = lectures.map(lecture => lecture.id)
this._lectures = lectures
this._unchanged = false
this._unsaved = true
}

// Update lectures
for (const [index, lecture] of this.lectures.entries()) {
lecture.order = index
}

// Call the API to reorder the graph
const response = await fetch(`/api/graph/${this.id}/reorder/lectures`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ lecture_ids: this.lecture_ids })
})

// Throw an error if the API request fails
if (!response.ok) {
throw new Error(`APIError (/api/graph/${this.id}/reorder/lectures PUT): ${response.status} ${response.statusText}`)
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/lib/scripts/controllers/LectureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,19 @@ class LectureController {
this._unsaved = false
}

async delete() {
async delete(reorder_graph: boolean = true) {

// Unassign graph and subjects
if (this._graph_id !== undefined)
this.graph.unassignLecture(this)
if (this._present_subject_ids !== undefined)
for (const subject of this.present_subjects)
subject.unassignFromLecture(this, false)

// Fix order of remaining lectures
if (reorder_graph) {
await this.graph.reorderLectures()
}

// Call the API to delete the lecture
const response = await fetch(`/api/lecture/${this.id}`, { method: 'DELETE' })
Expand Down
21 changes: 20 additions & 1 deletion src/lib/scripts/helpers/GraphHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export async function update(data: SerializedGraph) {
}
}

export async function reorder(domain_ids: number[]) {
export async function reorderDomains(domain_ids: number[]) {
return await Promise.all(
domain_ids.map(async (domain_id, order) => {
try {
Expand All @@ -168,6 +168,25 @@ export async function reorder(domain_ids: number[]) {
)
}

export async function reorderLectures(lecture_ids: number[]) {
return await Promise.all(
lecture_ids.map(async (lecture_id, order) => {
try {
return prisma.lecture.update({
where: {
id: lecture_id
},
data: {
order: order
}
})
} catch (error) {
return Promise.reject(error)
}
})
)
}

export async function remove(id: number) {
try {
await prisma.graph.delete({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function PUT({ request }) {
}

// Reorder data
return await GraphHelper.reorder(domain_ids)
return await GraphHelper.reorderDomains(domain_ids)
.then(
() => new Response(null, { status: 200 }),
error => new Response(error, { status: 400 })
Expand Down
25 changes: 25 additions & 0 deletions src/routes/api/graph/[id]/reorder/lectures/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

// Internal dependencies
import { GraphHelper } from '$scripts/helpers'

// Exports
export { PUT }


// --------------------> API Endpoints

async function PUT({ request }) {

// Retrieve data
const { lecture_ids } = await request.json()
if (!lecture_ids) {
return new Response('Missing data', { status: 400 })
}

// Reorder data
return await GraphHelper.reorderLectures(lecture_ids)
.then(
() => new Response(null, { status: 200 }),
error => new Response(error, { status: 400 })
)
}
2 changes: 1 addition & 1 deletion src/routes/app/graph/[graph]/editor/DomainTab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{#if filtered_domains.length === 0}
<p class="grayed"> There's nothing here </p>
{:else}
<SortableList let:item list={filtered_domains} on:rearrange={async event => await $graph.reorder(event.detail)}>
<SortableList let:item list={filtered_domains} on:rearrange={async event => await $graph.reorderDomains(event.detail)}>
<DomainRow domain={item} />
</SortableList>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/app/graph/[graph]/editor/LectureTab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{#if filtered_lectures.length == 0}
<p class="grayed"> There's nothing here </p>
{:else}
<SortableList let:item list={filtered_lectures} >
<SortableList let:item list={filtered_lectures} on:rearrange={async event => await $graph.reorderLectures(event.detail)} >
<LectureRow lecture={item} />
</SortableList>
{/if}
Expand Down

0 comments on commit d798daf

Please sign in to comment.