diff --git a/backend/src/handler/db_handler.py b/backend/src/handler/db_handler.py index 27a9d5c77..46b1a8448 100644 --- a/backend/src/handler/db_handler.py +++ b/backend/src/handler/db_handler.py @@ -12,20 +12,28 @@ class DBHandler: self.session = Session() - def add_platform(self, **kargs): + def add_platform(self, **kargs) -> None: self.session.merge(Platform(**kargs)) - def get_platforms(self): + def get_platforms(self) -> list[Platform]: return self.session.query(Platform).all() - def add_rom(self, **kargs): + def add_rom(self, **kargs) -> None: self.session.merge(Rom(**kargs)) - def get_roms(self, p_slug): + def get_roms(self, p_slug: str) -> list[Rom]: return self.session.query(Rom).filter(Rom.p_slug == p_slug).all() + + + def update_rom(self, p_slug: str, filename: str, data: dict) -> None: + self.session.query(Rom).filter(Rom.p_slug==p_slug, Rom.filename==filename).update(data, synchronize_session='evaluate') + + + def delete_rom(self, p_slug: str, filename: str) -> None: + self.session.query(Rom).filter(Rom.p_slug==p_slug, Rom.filename==filename).delete(synchronize_session='evaluate') def commit(self): diff --git a/backend/src/handler/igdb_handler.py b/backend/src/handler/igdb_handler.py index dde18f001..d4e9f5fd6 100644 --- a/backend/src/handler/igdb_handler.py +++ b/backend/src/handler/igdb_handler.py @@ -105,7 +105,7 @@ class IGDBHandler(): url_cover: str = f"https:{res_details['url']}" except IndexError: log.warning(f"{name} cover not found in igdb") - if not name: name = filename + if not name: name = filename_no_ext return (igdb_id, filename_no_ext, slug, name, summary, url_cover) diff --git a/backend/src/main.py b/backend/src/main.py index 44d9f2215..f2b032aaa 100644 --- a/backend/src/main.py +++ b/backend/src/main.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI +from fastapi import FastAPI, Request import uvicorn from logger.logger import log @@ -17,69 +17,71 @@ sgdbh: SGDBHandler = SGDBHandler() dbh: DBHandler = DBHandler() -@app.patch("/platforms/{p_slug}/roms/{filename}") -async def editRom(p_slug: str, filename: str): - """Edits rom details""" - return {'msg': 'WIP'} - - @app.get("/platforms/{p_slug}/roms") -async def platforms(p_slug: str): +async def roms(p_slug: str): """Returns roms data of the desired platform""" return {'data': dbh.get_roms(p_slug)} +@app.patch("/platforms/{p_slug}/roms/{filename}") +async def updateRom(req: Request, p_slug: str, filename: str): + """Updates rom details""" + data: dict = await req.json() + if 'filename' in data: fs.rename_rom(p_slug, filename, data) + dbh.update_rom(p_slug, filename, data) + dbh.commit() + return {'msg': 'success'} + + +@app.delete("/platforms/{p_slug}/roms/{filename}") +async def delete_rom(p_slug: str, filename: str): + log.info("deleting rom...") + fs.delete_rom(p_slug, filename) + dbh.delete_rom(p_slug, filename) + dbh.commit() + return {'msg': 'success'} + + @app.get("/platforms") async def platforms(): """Returns platforms data""" return {'data': dbh.get_platforms()} +@app.get("/scan/rom") +async def scan_rom(req: Request, overwrite: bool=False): + """Scan single rom and write it in database.""" + + log.info("scaning rom...") + data: dict = await req.json() + fastapi.scan_rom(overwrite, data['filename'], data['p_igdb_id'], data['p_slug'], igdbh, dbh) + dbh.commit() + return {'msg': 'success'} + + +@app.get("/scan/platform") +async def scan_platform(req: Request, overwrite: bool=False): + """Scan single platform and write it in database.""" + + log.info("scaning platform roms...") + data: dict = await req.json() + for filename in fs.get_roms(data['p_slug']): + fastapi.scan_rom(overwrite, filename, data['p_igdb_id'], data['p_slug'], igdbh, dbh) + dbh.commit() + return {'msg': 'success'} + + @app.get("/scan") async def scan(overwrite: bool=False): """Scan platforms and roms and write them in database.""" - log.info("scaning...") - + log.info("complete scaning...") fs.store_default_resources(overwrite) - for p_slug in fs.get_platforms(): - platform: dict = {} - log.info(f"Getting {p_slug} details") - p_igdb_id, p_name, url_logo = igdbh.get_platform_details(p_slug) - platform['slug'] = p_slug - platform['igdb_id'] = p_igdb_id - platform['name'] = p_name - - #TODO: refactor logo details logic - if (overwrite or not fs.p_logo_exists(p_slug)) and url_logo: - fs.store_p_logo(p_slug, url_logo) - if fs.p_logo_exists(p_slug): - platform['path_logo']: str = fs.get_p_path_logo(p_slug) - - dbh.add_platform(**platform) - + p_igdb_id: str = fastapi.scan_platform(overwrite, p_slug, igdbh, dbh) for filename in fs.get_roms(p_slug): - rom: dict = {} - log.info(f"Getting {filename} details") - r_igdb_id, filename_no_ext, r_slug, r_name, summary, url_cover = igdbh.get_rom_details(filename, p_igdb_id) - path_cover_s, path_cover_l, has_cover = fs.get_cover_details(overwrite, p_slug, filename_no_ext, url_cover) - rom['filename'] = filename - rom['filename_no_ext'] = filename_no_ext - rom['r_igdb_id'] = r_igdb_id - rom['p_igdb_id'] = p_igdb_id - rom['name'] = r_name - rom['r_slug'] = r_slug - rom['p_slug'] = p_slug - rom['summary'] = summary - rom['path_cover_s'] = path_cover_s - rom['path_cover_l'] = path_cover_l - rom['has_cover'] = has_cover - - dbh.add_rom(**rom) - + fastapi.scan_rom(overwrite, filename, p_igdb_id, p_slug, igdbh, dbh) dbh.commit() - return {'msg': 'success'} diff --git a/backend/src/utils/fastapi.py b/backend/src/utils/fastapi.py index c55271968..7e784d6a4 100644 --- a/backend/src/utils/fastapi.py +++ b/backend/src/utils/fastapi.py @@ -1,6 +1,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware +from utils import fs from logger.logger import log @@ -13,3 +14,38 @@ def allow_cors(app: FastAPI) -> None: allow_headers=["*"], ) log.info("CORS enabled") + + +def scan_platform(overwrite: bool, p_slug: str, igdbh, dbh) -> str: + platform: dict = {} + log.info(f"Getting {p_slug} details") + p_igdb_id, p_name, url_logo = igdbh.get_platform_details(p_slug) + platform['slug'] = p_slug + platform['igdb_id'] = p_igdb_id + platform['name'] = p_name + #TODO: refactor logo details logic + if (overwrite or not fs.p_logo_exists(p_slug)) and url_logo: + fs.store_p_logo(p_slug, url_logo) + if fs.p_logo_exists(p_slug): + platform['path_logo']: str = fs.get_p_path_logo(p_slug) + dbh.add_platform(**platform) + return p_igdb_id + + +def scan_rom(overwrite: bool, filename: str, p_igdb_id: str, p_slug: str, igdbh, dbh) -> None: + rom: dict = {} + log.info(f"Getting {filename} details") + r_igdb_id, filename_no_ext, r_slug, r_name, summary, url_cover = igdbh.get_rom_details(filename, p_igdb_id) + path_cover_s, path_cover_l, has_cover = fs.get_cover_details(overwrite, p_slug, filename_no_ext, url_cover) + rom['filename'] = filename + rom['filename_no_ext'] = filename_no_ext + rom['r_igdb_id'] = r_igdb_id + rom['p_igdb_id'] = p_igdb_id + rom['name'] = r_name + rom['r_slug'] = r_slug + rom['p_slug'] = p_slug + rom['summary'] = summary + rom['path_cover_s'] = path_cover_s + rom['path_cover_l'] = path_cover_l + rom['has_cover'] = has_cover + dbh.add_rom(**rom) diff --git a/backend/src/utils/fs.py b/backend/src/utils/fs.py index bd5f5c1d7..161f1ecad 100644 --- a/backend/src/utils/fs.py +++ b/backend/src/utils/fs.py @@ -161,3 +161,15 @@ def get_roms(p_slug) -> list: log.warning(f"roms not found for {p_slug}") pass return roms + + +def rename_rom(p_slug: str, filename: str, data: dict) -> None: + os.rename(f"{EMULATION_BASE_PATH}/{p_slug}/roms/{filename}", + f"{EMULATION_BASE_PATH}/{p_slug}/roms/{data['filename']}") + + +def delete_rom(p_slug: str, filename: str) -> None: + try: + os.remove(f"{EMULATION_BASE_PATH}/{p_slug}/roms/{filename}") + except FileNotFoundError: + log.warning(f"Rom not found in filesystem: {p_slug}/{filename}") diff --git a/emulation/nds/roms/romm.nds b/emulation/gbc/roms/romm.gbc similarity index 100% rename from emulation/nds/roms/romm.nds rename to emulation/gbc/roms/romm.gbc diff --git a/emulation/nds/roms/pokemon black 2.nds b/emulation/nds/roms/pokemon black 2.nds new file mode 100644 index 000000000..e69de29bb diff --git a/emulation/nds/roms/pokemon black.nds b/emulation/nds/roms/pokemon black.nds new file mode 100644 index 000000000..e69de29bb diff --git a/emulation/nds/roms/pokemon white 2.nds b/emulation/nds/roms/pokemon white 2.nds new file mode 100644 index 000000000..e69de29bb diff --git a/emulation/nds/roms/pokemon white.nds b/emulation/nds/roms/pokemon white.nds new file mode 100644 index 000000000..e69de29bb diff --git a/emulation/resources/nds/pokemon black 2_l.png b/emulation/resources/nds/pokemon black 2_l.png new file mode 100644 index 000000000..cad70b5b1 Binary files /dev/null and b/emulation/resources/nds/pokemon black 2_l.png differ diff --git a/emulation/resources/nds/pokemon black 2_s.png b/emulation/resources/nds/pokemon black 2_s.png new file mode 100644 index 000000000..a2fcb73b1 Binary files /dev/null and b/emulation/resources/nds/pokemon black 2_s.png differ diff --git a/emulation/resources/nds/pokemon black_l.png b/emulation/resources/nds/pokemon black_l.png new file mode 100644 index 000000000..cd515711e Binary files /dev/null and b/emulation/resources/nds/pokemon black_l.png differ diff --git a/emulation/resources/nds/pokemon black_s.png b/emulation/resources/nds/pokemon black_s.png new file mode 100644 index 000000000..bebe72f4f Binary files /dev/null and b/emulation/resources/nds/pokemon black_s.png differ diff --git a/emulation/resources/nds/pokemon white 2_l.png b/emulation/resources/nds/pokemon white 2_l.png new file mode 100644 index 000000000..122231ebb Binary files /dev/null and b/emulation/resources/nds/pokemon white 2_l.png differ diff --git a/emulation/resources/nds/pokemon white 2_s.png b/emulation/resources/nds/pokemon white 2_s.png new file mode 100644 index 000000000..fe6863e3e Binary files /dev/null and b/emulation/resources/nds/pokemon white 2_s.png differ diff --git a/emulation/resources/nds/pokemon white_l.png b/emulation/resources/nds/pokemon white_l.png new file mode 100644 index 000000000..2a9867e71 Binary files /dev/null and b/emulation/resources/nds/pokemon white_l.png differ diff --git a/emulation/resources/nds/pokemon white_s.png b/emulation/resources/nds/pokemon white_s.png new file mode 100644 index 000000000..e5ea4e7c5 Binary files /dev/null and b/emulation/resources/nds/pokemon white_s.png differ diff --git a/frontend/src/components/RomsGallery.vue b/frontend/src/components/RomsGallery.vue index 225a94f90..0a1e422b1 100644 --- a/frontend/src/components/RomsGallery.vue +++ b/frontend/src/components/RomsGallery.vue @@ -77,7 +77,7 @@ onMounted(() => { if(currentPlatformSlug){ getRoms(currentPlatformSlug) } }) -
{{ rom.name }}
+
{{ rom.filename }}