minor refactoring

This commit is contained in:
Jörn-Michael Miehe 2023-09-07 19:34:11 +00:00
parent 7821aebd68
commit 83098d3dc4
4 changed files with 24 additions and 22 deletions

View file

@ -13,13 +13,7 @@ class User(BaseModel):
password: str password: str
class Puzzle(BaseModel): class DoorSaved(BaseModel):
background: str
font: str
solution: str
class Door(BaseModel):
day: int day: int
x1: int x1: int
y1: int y1: int
@ -27,13 +21,19 @@ class Door(BaseModel):
y2: int y2: int
Doors: TypeAlias = list[Door] | None DoorsSaved: TypeAlias = list[DoorSaved]
class Puzzle(BaseModel):
background: str
doors: DoorsSaved = []
font: str
solution: str
class Config(BaseModel): class Config(BaseModel):
admin: User admin: User
puzzle: Puzzle puzzle: Puzzle
doors: Doors = []
async def get_config() -> Config: async def get_config() -> Config:

View file

@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from PIL import Image from PIL import Image
from ..config import Config, Doors, get_config, set_config from ..config import Config, DoorsSaved, get_config, set_config
from ..dav_common import dav_get_file from ..dav_common import dav_get_file
from ._misc import api_return_image from ._misc import api_return_image
@ -28,22 +28,22 @@ async def get_image_for_day(
@router.get("/doors") @router.get("/doors")
async def get_doors( async def get_doors(
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
) -> Doors: ) -> DoorsSaved:
""" """
Türchen lesen Türchen lesen
""" """
return cfg.doors return cfg.puzzle.doors
@router.put("/doors") @router.put("/doors")
async def put_doors( async def put_doors(
doors: Doors, doors: DoorsSaved,
cfg: Config = Depends(get_config), cfg: Config = Depends(get_config),
) -> None: ) -> None:
""" """
Türchen setzen Türchen setzen
""" """
cfg.doors = doors cfg.puzzle.doors = doors
await set_config(cfg) await set_config(cfg)

View file

@ -28,7 +28,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { Door, DoorSerialized } from "@/lib/door"; import { Door, DoorsSaved } from "@/lib/door";
import { Options, Vue } from "vue-class-component"; import { Options, Vue } from "vue-class-component";
import BulmaBreadcrumbs, { Step } from "./BulmaBreadcrumbs.vue"; import BulmaBreadcrumbs, { Step } from "./BulmaBreadcrumbs.vue";
@ -56,24 +56,24 @@ export default class extends Vue {
public doors: Door[] = []; public doors: Door[] = [];
public load_doors() { public load_doors() {
this.$advent22.api_get<DoorSerialized[]>("/general/doors").then((data) => { this.$advent22.api_get<DoorsSaved>("/general/doors").then((data) => {
this.doors.length = 0; this.doors.length = 0;
for (const value of data) { for (const value of data) {
this.doors.push(Door.deserialize(value)); this.doors.push(Door.load(value));
} }
}); });
} }
public save_doors() { public save_doors() {
const data: DoorSerialized[] = []; const data: DoorsSaved = [];
for (const door of this.doors) { for (const door of this.doors) {
if (door.day === -1) { if (door.day === -1) {
continue; continue;
} }
data.push(door.serialized); data.push(door.save());
} }
this.$advent22.api_put("/general/doors", data); this.$advent22.api_put("/general/doors", data);

View file

@ -1,7 +1,7 @@
import { Rectangle } from "./rectangle"; import { Rectangle } from "./rectangle";
import { Vector2D } from "./vector2d"; import { Vector2D } from "./vector2d";
export interface DoorSerialized { export interface DoorSaved {
day: number; day: number;
x1: number; x1: number;
y1: number; y1: number;
@ -9,6 +9,8 @@ export interface DoorSerialized {
y2: number; y2: number;
} }
export type DoorsSaved = DoorSaved[];
export class Door { export class Door {
private _day = -1; private _day = -1;
public position: Rectangle; public position: Rectangle;
@ -35,7 +37,7 @@ export class Door {
} }
} }
public static deserialize(serialized: DoorSerialized): Door { public static load(serialized: DoorSaved): Door {
return new Door( return new Door(
new Rectangle( new Rectangle(
new Vector2D(serialized.x1, serialized.y1), new Vector2D(serialized.x1, serialized.y1),
@ -45,7 +47,7 @@ export class Door {
); );
} }
public get serialized(): DoorSerialized { public save(): DoorSaved {
return { return {
day: this.day, day: this.day,
x1: this.position.origin.x, x1: this.position.origin.x,