Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	improve messages and error handling
Browse files- app.py +39 -23
- index.html +10 -4
    	
        app.py
    CHANGED
    
    | @@ -91,19 +91,31 @@ def rueckgabe(idBuch, grund='rueckgabe'): | |
| 91 | 
             
                - grund (str): The reason for the return (default: 'rueckgabe').
         | 
| 92 |  | 
| 93 | 
             
                Returns:
         | 
| 94 | 
            -
                -  | 
| 95 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 96 | 
             
                """
         | 
| 97 | 
            -
                sql = f"SELECT `idBuch`, `idKind`, `ausleihe`, `rueckgabe`, `rueckGrund` FROM `{AUSLEIHE_TABLE}` WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
         | 
| 98 | 
             
                result = execute_query(sql, (idBuch,))
         | 
| 99 | 
             
                if len(result) == 0:
         | 
| 100 | 
            -
                    return  | 
|  | |
| 101 | 
             
                # return the book
         | 
| 102 | 
             
                sql = f"UPDATE `{AUSLEIHE_TABLE}` SET `rueckgabe` = NOW(), `rueckGrund` = %s WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
         | 
| 103 | 
             
                execute_query(sql, (grund, idBuch))
         | 
| 104 | 
            -
                 | 
| 105 | 
            -
                 | 
| 106 | 
            -
                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 107 |  | 
| 108 | 
             
            @app.get("/borrow/{idBuch}/{idKind}")
         | 
| 109 | 
             
            def ausleihe(idBuch, idKind):
         | 
| @@ -117,22 +129,26 @@ def ausleihe(idBuch, idKind): | |
| 117 | 
             
                Returns:
         | 
| 118 | 
             
                - dict: A dictionary containing the result of the borrowing operation.
         | 
| 119 | 
             
                """
         | 
| 120 | 
            -
                 | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
                     | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 136 | 
             
            @app.get("/borrowed/{idKind}")
         | 
| 137 | 
             
            def ausgeliehen(idKind):
         | 
| 138 | 
             
                """
         | 
|  | |
| 91 | 
             
                - grund (str): The reason for the return (default: 'rueckgabe').
         | 
| 92 |  | 
| 93 | 
             
                Returns:
         | 
| 94 | 
            +
                - dict: Information about the return, including the students who had the book.
         | 
| 95 | 
            +
                """
         | 
| 96 | 
            +
                sql = f"""
         | 
| 97 | 
            +
                SELECT a.`idBuch`, a.`idKind`, a.`ausleihe`, a.`rueckgabe`, a.`rueckGrund`,
         | 
| 98 | 
            +
                       CONCAT(k.`Vorname`, ' ', k.`Nachnamen`) AS student_name
         | 
| 99 | 
            +
                FROM `{AUSLEIHE_TABLE}` a
         | 
| 100 | 
            +
                JOIN `kind` k ON a.`idKind` = k.`idKind`
         | 
| 101 | 
            +
                WHERE a.`idBuch` = %s AND a.`rueckgabe` is NULL;
         | 
| 102 | 
             
                """
         | 
|  | |
| 103 | 
             
                result = execute_query(sql, (idBuch,))
         | 
| 104 | 
             
                if len(result) == 0:
         | 
| 105 | 
            +
                    return {"success": False, "message": "Buch nicht gefunden oder bereits zurückgegeben"}
         | 
| 106 | 
            +
                
         | 
| 107 | 
             
                # return the book
         | 
| 108 | 
             
                sql = f"UPDATE `{AUSLEIHE_TABLE}` SET `rueckgabe` = NOW(), `rueckGrund` = %s WHERE `idBuch` = %s AND `rueckgabe` is NULL;"
         | 
| 109 | 
             
                execute_query(sql, (grund, idBuch))
         | 
| 110 | 
            +
                
         | 
| 111 | 
            +
                student_names = [row['student_name'] for row in result]
         | 
| 112 | 
            +
                students_str = ", ".join(student_names)
         | 
| 113 | 
            +
                
         | 
| 114 | 
            +
                return {
         | 
| 115 | 
            +
                    "success": True,
         | 
| 116 | 
            +
                    "message": f"Buch zurückgegeben von: {students_str}",
         | 
| 117 | 
            +
                    "student_names": student_names
         | 
| 118 | 
            +
                }
         | 
| 119 |  | 
| 120 | 
             
            @app.get("/borrow/{idBuch}/{idKind}")
         | 
| 121 | 
             
            def ausleihe(idBuch, idKind):
         | 
|  | |
| 129 | 
             
                Returns:
         | 
| 130 | 
             
                - dict: A dictionary containing the result of the borrowing operation.
         | 
| 131 | 
             
                """
         | 
| 132 | 
            +
                try:
         | 
| 133 | 
            +
                    rueckgabe_result = rueckgabe(idBuch, grund="neu-ausleihe")
         | 
| 134 | 
            +
                    
         | 
| 135 | 
            +
                    message = "Buch erfolgreich ausgeliehen"
         | 
| 136 | 
            +
                    if rueckgabe_result.get("success", False):
         | 
| 137 | 
            +
                        # Get the name of the previous borrower
         | 
| 138 | 
            +
                        prev_borrower_id = rueckgabe_result["student_names"][0]
         | 
| 139 | 
            +
                        sql = "SELECT CONCAT(Vorname, ' ', Nachnamen) AS full_name FROM kind WHERE idKind = %s;"
         | 
| 140 | 
            +
                        prev_borrower_name = execute_query(sql, (prev_borrower_id,))[0]['full_name']
         | 
| 141 | 
            +
                        message += f". Zuvor ausgeliehen von {prev_borrower_name}"
         | 
| 142 | 
            +
                    
         | 
| 143 | 
            +
                    # Insert new borrowing record
         | 
| 144 | 
            +
                    sql = f"INSERT INTO `{AUSLEIHE_TABLE}` (`idBuch`, `idKind`, `ausleihe`) VALUES (%s, %s, NOW());"
         | 
| 145 | 
            +
                    execute_query(sql, (idBuch, idKind))
         | 
| 146 | 
            +
                    
         | 
| 147 | 
            +
                    return {"success": True, "message": message}
         | 
| 148 | 
            +
                except Exception as e:
         | 
| 149 | 
            +
                    LOG.error(f"Error in ausleihe: {str(e)}")
         | 
| 150 | 
            +
                    return {"success": False, "message": f"Fehler beim Ausleihen des Buches: {str(e)}"}
         | 
| 151 | 
            +
             | 
| 152 | 
             
            @app.get("/borrowed/{idKind}")
         | 
| 153 | 
             
            def ausgeliehen(idKind):
         | 
| 154 | 
             
                """
         | 
    	
        index.html
    CHANGED
    
    | @@ -125,11 +125,17 @@ | |
| 125 | 
             
                        fetch(`/return/${bookId}`)
         | 
| 126 | 
             
                            .then(response => response.json())
         | 
| 127 | 
             
                            .then(result => {
         | 
| 128 | 
            -
                                if (result | 
| 129 | 
            -
                                     | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 132 | 
             
                                    document.getElementById('returnBookId').value = ''; // Clear input field
         | 
|  | |
|  | |
| 133 | 
             
                                }
         | 
| 134 | 
             
                            });
         | 
| 135 | 
             
                    }
         | 
|  | |
| 125 | 
             
                        fetch(`/return/${bookId}`)
         | 
| 126 | 
             
                            .then(response => response.json())
         | 
| 127 | 
             
                            .then(result => {
         | 
| 128 | 
            +
                                if (result.success) {
         | 
| 129 | 
            +
                                    let message = `Buch (Nummer: ${bookId}) erfolgreich zurückgegeben.`;
         | 
| 130 | 
            +
                                    if (result.student_names.length === 1) {
         | 
| 131 | 
            +
                                        message += ` Ausgeliehen von: ${result.student_names[0]}`;
         | 
| 132 | 
            +
                                    } else if (result.student_names.length > 1) {
         | 
| 133 | 
            +
                                        message += ` Ausgeliehen von: ${result.student_names.join(', ')}`;
         | 
| 134 | 
            +
                                    }
         | 
| 135 | 
            +
                                    setMessage(message);
         | 
| 136 | 
             
                                    document.getElementById('returnBookId').value = ''; // Clear input field
         | 
| 137 | 
            +
                                } else {
         | 
| 138 | 
            +
                                    setMessage(result.message);
         | 
| 139 | 
             
                                }
         | 
| 140 | 
             
                            });
         | 
| 141 | 
             
                    }
         |