You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.9 KiB
85 lines
2.9 KiB
import { TacticService, ServiceError, TacticContext } from "./TacticService.ts"
|
|
import { StepContent, StepInfoNode } from "../model/tactic/Tactic.ts"
|
|
import { fetchAPI, fetchAPIGet } from "../Fetcher.ts"
|
|
|
|
export class APITacticService implements TacticService {
|
|
private readonly tacticId: number
|
|
|
|
constructor(tacticId: number) {
|
|
this.tacticId = tacticId
|
|
}
|
|
|
|
async getContext(): Promise<TacticContext | ServiceError> {
|
|
const infoResponsePromise = fetchAPIGet(`tactics/${this.tacticId}`)
|
|
const treeResponsePromise = fetchAPIGet(`tactics/${this.tacticId}/tree`)
|
|
|
|
const infoResponse = await infoResponsePromise
|
|
const treeResponse = await treeResponsePromise
|
|
|
|
if (infoResponse.status == 401 || treeResponse.status == 401) {
|
|
return ServiceError.UNAUTHORIZED
|
|
}
|
|
const { name, courtType } = await infoResponse.json()
|
|
const { root } = await treeResponse.json()
|
|
|
|
return { courtType, name, stepsTree: root }
|
|
}
|
|
|
|
async addStep(
|
|
parent: StepInfoNode,
|
|
content: StepContent,
|
|
): Promise<StepInfoNode | ServiceError> {
|
|
const response = await fetchAPI(`tactics/${this.tacticId}/steps`, {
|
|
parentId: parent.id,
|
|
content,
|
|
})
|
|
if (response.status == 404) return ServiceError.NOT_FOUND
|
|
if (response.status == 401) return ServiceError.UNAUTHORIZED
|
|
|
|
const { stepId } = await response.json()
|
|
return { id: stepId, children: [] }
|
|
}
|
|
|
|
async removeStep(id: number): Promise<void | ServiceError> {
|
|
const response = await fetchAPI(
|
|
`tactics/${this.tacticId}/steps/${id}`,
|
|
{},
|
|
"DELETE",
|
|
)
|
|
if (response.status == 404) return ServiceError.NOT_FOUND
|
|
if (response.status == 401) return ServiceError.UNAUTHORIZED
|
|
}
|
|
|
|
async setName(name: string): Promise<void | ServiceError> {
|
|
const response = await fetchAPI(
|
|
`tactics/${this.tacticId}/name`,
|
|
{ name },
|
|
"PUT",
|
|
)
|
|
if (response.status == 404) return ServiceError.NOT_FOUND
|
|
if (response.status == 401) return ServiceError.UNAUTHORIZED
|
|
}
|
|
|
|
async saveContent(
|
|
step: number,
|
|
content: StepContent,
|
|
): Promise<void | ServiceError> {
|
|
const response = await fetchAPI(
|
|
`tactics/${this.tacticId}/steps/${step}`,
|
|
{ content },
|
|
"PUT",
|
|
)
|
|
if (response.status == 404) return ServiceError.NOT_FOUND
|
|
if (response.status == 401) return ServiceError.UNAUTHORIZED
|
|
}
|
|
|
|
async getContent(step: number): Promise<StepContent | ServiceError> {
|
|
const response = await fetchAPIGet(
|
|
`tactics/${this.tacticId}/steps/${step}`,
|
|
)
|
|
if (response.status == 404) return ServiceError.NOT_FOUND
|
|
if (response.status == 401) return ServiceError.UNAUTHORIZED
|
|
return await response.json()
|
|
}
|
|
}
|