diff --git a/ccws_practical.pdf b/ccws_practical.pdf new file mode 100644 index 0000000..c4cbb47 Binary files /dev/null and b/ccws_practical.pdf differ diff --git a/flask.txt b/flask.txt new file mode 100644 index 0000000..c79ea49 --- /dev/null +++ b/flask.txt @@ -0,0 +1,51 @@ +from flask import Flask, request, jsonify + +app = Flask(__name__) + +# Sample data +books = [ + {"id": 1, "title": "Book 1", "author": "Author 1"}, + {"id": 2, "title": "Book 2", "author": "Author 2"}, + {"id": 3, "title": "Book 3", "author": "Author 3"} +] + +# Route to get all books +@app.route('/books', methods=['GET']) +def get_books(): + return jsonify(books) + +# Route to get a specific book by its ID +@app.route('/books/', methods=['GET']) +def get_book(book_id): + for book in books: + if book['id'] == book_id: + return jsonify(book) + return jsonify({'error': 'Book not found'}), 404 + +# Route to add a new book +@app.route('/books', methods=['POST']) +def add_book(): + new_book = request.json + books.append(new_book) + return jsonify(new_book), 201 + +# Route to update an existing book +@app.route('/books/', methods=['PUT']) +def update_book(book_id): + for book in books: + if book['id'] == book_id: + book.update(request.json) + return jsonify(book) + return jsonify({'error': 'Book not found'}), 404 + +# Route to delete a book +@app.route('/books/', methods=['DELETE']) +def delete_book(book_id): + for i, book in enumerate(books): + if book['id'] == book_id: + del books[i] + return jsonify({'message': 'Book deleted'}) + return jsonify({'error': 'Book not found'}), 404 + +if __name__ == '__main__': + app.run(debug=True)