diff --git a/src/api.ts b/src/api.ts index 4682d3b..0176120 100644 --- a/src/api.ts +++ b/src/api.ts @@ -42,6 +42,13 @@ import { UpdateGroupData } from "./types/UpdateGroupData"; import { UpdateProjectData } from "./types/UpdateProjectData"; import { UpdateRoleData } from "./types/UpdateRoleData"; import { User } from "./types/User"; +import { AssignmentId } from "./types/AssignmentId"; +import { CreateAssignmentData } from "./types/CreateAssignmentData"; +import { Assignment } from "./types/Assignment"; +import { SubmissionId } from "./types/SubmissionId"; +import { CreateSubmissionData } from "./types/CreateSubmissionData"; +import { UpdateAssignmentData } from "./types/UpdateAssignmentData"; +import { Submission } from "./types/Submission"; export default class NetsBloxApi { private baseUrl: string; @@ -237,6 +244,115 @@ export default class NetsBloxApi { return await this.fetchJson(`/groups/id/${encodeURIComponent(id)}/members`); } + async createAssignment( + id: GroupId, + data: CreateAssignmentData, + ): Promise { + return await this.post( + `/groups/id/${encodeURIComponent(id)}/assignments/`, + data, + ); + } + + async listGroupAssignments(id: GroupId): Promise { + return await this.fetchJson( + `/groups/id/${encodeURIComponent(id)}/assignments/`, + ); + } + + async viewAssignment( + group_id: GroupId, + id: AssignmentId, + ): Promise { + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${id}/`, + ); + } + + async editAssignment( + group_id: GroupId, + id: AssignmentId, + data: UpdateAssignmentData, + ): Promise { + const opts = { + method: "patch", + body: JSON.stringify(data), + }; + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${id}/`, + opts, + ); + } + + async deleteAssignment( + group_id: GroupId, + id: AssignmentId, + ): Promise { + const opts = { method: "delete" }; + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(id)}/`, + opts, + ); + } + + async createSubmission( + group_id: GroupId, + id: AssignmentId, + data: CreateSubmissionData, + ): Promise { + return await this.post( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(id)}/submissions/`, + data, + ); + } + + async viewSubmission(group_id: GroupId, assignment_id: AssignmentId, id: SubmissionId): Promise { + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/`, + ); + } + + async viewAssignmentSubmissions( + group_id: GroupId, + assignment_id: AssignmentId, + ): Promise { + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/`, + ); + } + + async viewUserSubmissions( + group_id: GroupId, + assignment_id: AssignmentId, + username: string, + ): Promise { + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/user/${encodeURIComponent(username)}/`, + ); + } + + async viewSubmissionXml( + group_id: GroupId, + assignment_id: AssignmentId, + id: SubmissionId, + ): Promise { + return await this.fetchText( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/xml/`, + ); + } + + async deleteSubmission( + group_id: GroupId, + assignment_id: AssignmentId, + id: SubmissionId, + ): Promise { + const opts = { method: "delete" }; + return await this.fetchJson( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/`, + opts, + ); + } + ////////////////////////////// Projects ////////////////////////////// async createProject(data: CreateProjectData): Promise { const opts = { diff --git a/src/client.ts b/src/client.ts index 14eced3..7fcbb2b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -49,24 +49,27 @@ export default class Cloud { username: string; projectId: string | null; roleId: string | null; + groupId: string | null; newProjectRequest: Promise | undefined; localize: (text: string) => string; token: string | null; api: NetsBloxApi; - constructor(url, clientId, username, localize = defaultLocalizer) { + constructor(url, clientId, username, groupId, localize = defaultLocalizer) { this.clientId = clientId; this.username = username; this.projectId = null; this.roleId = null; + this.groupId = groupId; this.url = url; this.token = null; // only needed in NodeJs this.localize = localize; this.api = new NetsBloxApi(this.url); - } +} clear() { this.username = null; + this.groupId = null; this.token = null; } @@ -96,6 +99,7 @@ export default class Cloud { const response = await this.post("/users/login", body); const user = await response.json(); this.username = user.username; + this.groupId = user.groupId; if (isNodeJs) { const cookie = response.headers.get("set-cookie"); if (!cookie) throw new CloudError("No cookie received"); @@ -677,6 +681,27 @@ export default class Cloud { await this.post(`/libraries/user/${this.username}/${name}/unpublish`); } + async listGroupAssignments() { + const response = await this.fetch( `/groups/id/${encodeURIComponent(this.groupId)}/assignments/`, ); + return await response.json(); + } + + async saveSubmission(assignmentId, xml) { + const body = { owner: this.username, xml: xml }; + const response = await this.post( + `/groups/id/${encodeURIComponent(this.groupId)}/assignments/id/${encodeURIComponent(assignmentId)}/submissions/`, + body, + ); + return await response.json(); + } + + async viewSubmissionXml( group_id, assignment_id, id) { + const response = await this.fetch( + `/groups/id/${encodeURIComponent(group_id)}/assignments/id/${encodeURIComponent(assignment_id)}/submissions/id/${encodeURIComponent(id)}/xml/`, + ); + return await response.text(); + } + // Cloud: user messages (to be overridden) message(string) { diff --git a/src/types/Assignment.ts b/src/types/Assignment.ts new file mode 100644 index 0000000..05751aa --- /dev/null +++ b/src/types/Assignment.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { AssignmentId } from "./AssignmentId"; +import type { GroupId } from "./GroupId"; + +export interface Assignment { id: AssignmentId, groupId: GroupId, name: string, originTime: any, dueDate: any, } \ No newline at end of file diff --git a/src/types/AssignmentId.ts b/src/types/AssignmentId.ts new file mode 100644 index 0000000..e517cce --- /dev/null +++ b/src/types/AssignmentId.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type AssignmentId = string; \ No newline at end of file diff --git a/src/types/CreateAssignmentData.ts b/src/types/CreateAssignmentData.ts new file mode 100644 index 0000000..f84e69c --- /dev/null +++ b/src/types/CreateAssignmentData.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { GroupId } from "./GroupId"; + +export interface CreateAssignmentData { name: string, dueDate: any, } diff --git a/src/types/CreateSubmissionData.ts b/src/types/CreateSubmissionData.ts new file mode 100644 index 0000000..08451b8 --- /dev/null +++ b/src/types/CreateSubmissionData.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface CreateSubmissionData { owner: string, xml: string, } \ No newline at end of file diff --git a/src/types/Submission.ts b/src/types/Submission.ts new file mode 100644 index 0000000..558da7f --- /dev/null +++ b/src/types/Submission.ts @@ -0,0 +1,5 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { AssignmentId } from "./AssignmentId"; +import type { SubmissionId } from "./SubmissionId"; + +export interface Submission { id: SubmissionId, assignmentId: AssignmentId, owner: string, originTime: any, } \ No newline at end of file diff --git a/src/types/SubmissionId.ts b/src/types/SubmissionId.ts new file mode 100644 index 0000000..82d39aa --- /dev/null +++ b/src/types/SubmissionId.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type SubmissionId = string; \ No newline at end of file diff --git a/src/types/UpdateAssignmentData.ts b/src/types/UpdateAssignmentData.ts new file mode 100644 index 0000000..0eab52d --- /dev/null +++ b/src/types/UpdateAssignmentData.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export interface UpdateAssignmentData { name: string | null, dueDate?: any, } \ No newline at end of file