Edit poljubne in ustvari novo. #11
This commit is contained in:
@@ -156,15 +156,25 @@ def search_songs():
|
||||
|
||||
@app.route('/api/get_song_details', methods=['GET'])
|
||||
def get_song_details():
|
||||
"""Vrne podrobnosti trenutno naložene pesmi"""
|
||||
"""Vrne podrobnosti trenutno naložene pesmi ali pesmi po ID-ju"""
|
||||
if _projector_app is None:
|
||||
return jsonify({'status': 'error', 'message': 'Aplikacija ni inicijalizirana'})
|
||||
|
||||
if not _projector_app.song_number_last:
|
||||
song_id_param = request.args.get('id')
|
||||
if song_id_param:
|
||||
try:
|
||||
song_id = int(song_id_param)
|
||||
except ValueError:
|
||||
return jsonify({'status': 'error', 'message': 'Neveljaven ID pesmi'})
|
||||
elif _projector_app.song_number_last:
|
||||
try:
|
||||
song_id = int(_projector_app.song_number_last)
|
||||
except ValueError:
|
||||
return jsonify({'status': 'error', 'message': 'Neveljavna številka naložene pesmi'})
|
||||
else:
|
||||
return jsonify({'status': 'error', 'message': 'Nobena pesem ni naložena'})
|
||||
|
||||
try:
|
||||
song_id = int(_projector_app.song_number_last)
|
||||
_projector_app.cursor.execute("SELECT id, title, lyrics FROM songs WHERE id=?", (song_id,))
|
||||
result = _projector_app.cursor.fetchone()
|
||||
if result:
|
||||
@@ -177,14 +187,14 @@ def get_song_details():
|
||||
}
|
||||
})
|
||||
else:
|
||||
return jsonify({'status': 'error', 'message': 'Pesem ni najdena v bazi'})
|
||||
return jsonify({'status': 'error', 'message': f'Pesem s številko {song_id} ni najdena v bazi'})
|
||||
except Exception as e:
|
||||
return jsonify({'status': 'error', 'message': str(e)})
|
||||
|
||||
|
||||
@app.route('/api/update_song', methods=['POST'])
|
||||
def update_song():
|
||||
"""Posodobi naslov in besedilo pesmi"""
|
||||
"""Posodobi naslov in besedilo pesmi ali ustvari novo"""
|
||||
if _projector_app is None:
|
||||
return jsonify({'status': 'error', 'message': 'Aplikacija ni inicijalizirana'})
|
||||
|
||||
@@ -197,6 +207,25 @@ def update_song():
|
||||
return jsonify({'status': 'error', 'message': 'Manjkajoči podatki'})
|
||||
|
||||
try:
|
||||
if song_id == 'new':
|
||||
# Pridobi prvo naslednjo prosto številko
|
||||
_projector_app.cursor.execute("SELECT MAX(id) FROM songs")
|
||||
max_id = _projector_app.cursor.fetchone()[0]
|
||||
new_id = (max_id or 0) + 1
|
||||
|
||||
_projector_app.cursor.execute(
|
||||
"INSERT INTO songs (id, title, lyrics) VALUES (?, ?, ?)",
|
||||
(new_id, title, lyrics)
|
||||
)
|
||||
_projector_app.conn.commit()
|
||||
|
||||
# Naloži novo pesem
|
||||
_projector_app.song_number = str(new_id)
|
||||
_projector_app.load_song()
|
||||
|
||||
return jsonify({'status': 'ok', 'new_id': new_id})
|
||||
|
||||
# Obstoječa pesem
|
||||
_projector_app.cursor.execute("UPDATE songs SET title=?, lyrics=? WHERE id=?", (title, lyrics, song_id))
|
||||
_projector_app.conn.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user