|
14 | 14 | api = Api(blueprint) |
15 | 15 |
|
16 | 16 |
|
| 17 | +@api.route('/books/', methods=['POST', 'GET', 'DELETE', 'PUT']) |
| 18 | +@api.route('/books/<int:model_id>/', methods=['GET', 'DELETE', 'PUT']) |
| 19 | +class BookRoute(Resource): |
| 20 | + def get(self, model_id: int = None): |
| 21 | + if model_id is None: |
| 22 | + all_objects = Book.query.all() |
| 23 | + output = [{'id': obj.id, **BookForm(obj=obj).data} for obj in all_objects] |
| 24 | + else: |
| 25 | + obj = Book.query.get(model_id) |
| 26 | + if obj is None: |
| 27 | + return { |
| 28 | + 'message': 'matching record not found', |
| 29 | + 'success': False |
| 30 | + }, 404 |
| 31 | + output = {'id': obj.id, **BookForm(obj=obj).data} |
| 32 | + return { |
| 33 | + 'data': output, |
| 34 | + 'success': True |
| 35 | + }, 200 |
| 36 | + |
| 37 | + @token_required |
| 38 | + def post(self): |
| 39 | + try: |
| 40 | + body_of_req = request.form |
| 41 | + if not body_of_req: |
| 42 | + raise Exception() |
| 43 | + except Exception: |
| 44 | + if len(request.data) > 0: |
| 45 | + body_of_req = json.loads(request.data) |
| 46 | + else: |
| 47 | + body_of_req = {} |
| 48 | + form = BookForm(MultiDict(body_of_req)) |
| 49 | + if form.validate(): |
| 50 | + try: |
| 51 | + obj = Book(**body_of_req) |
| 52 | + Book.query.session.add(obj) |
| 53 | + Book.query.session.commit() |
| 54 | + except Exception as e: |
| 55 | + return { |
| 56 | + 'message': str(e), |
| 57 | + 'success': False |
| 58 | + }, 400 |
| 59 | + else: |
| 60 | + return { |
| 61 | + 'message': form.errors, |
| 62 | + 'success': False |
| 63 | + }, 400 |
| 64 | + return { |
| 65 | + 'message': 'record saved!', |
| 66 | + 'success': True |
| 67 | + }, 200 |
| 68 | + |
| 69 | + @token_required |
| 70 | + def put(self, model_id: int): |
| 71 | + try: |
| 72 | + body_of_req = request.form |
| 73 | + if not body_of_req: |
| 74 | + raise Exception() |
| 75 | + except Exception: |
| 76 | + if len(request.data) > 0: |
| 77 | + body_of_req = json.loads(request.data) |
| 78 | + else: |
| 79 | + body_of_req = {} |
| 80 | + |
| 81 | + to_edit_row = Book.query.filter_by(id=model_id) |
| 82 | + |
| 83 | + if not to_edit_row: |
| 84 | + return { |
| 85 | + 'message': 'matching record not found', |
| 86 | + 'success': False |
| 87 | + }, 404 |
| 88 | + |
| 89 | + obj = to_edit_row.first() |
| 90 | + |
| 91 | + if not obj: |
| 92 | + return { |
| 93 | + 'message': 'matching record not found', |
| 94 | + 'success': False |
| 95 | + }, 404 |
| 96 | + |
| 97 | + form = BookForm(MultiDict(body_of_req), obj=obj) |
| 98 | + if not form.validate(): |
| 99 | + return { |
| 100 | + 'message': form.errors, |
| 101 | + 'success': False |
| 102 | + }, 404 |
| 103 | + |
| 104 | + table_cols = [attr.name for attr in to_edit_row.__dict__['_raw_columns'][0].columns._all_columns] |
| 105 | + |
| 106 | + for col in table_cols: |
| 107 | + value = body_of_req.get(col, None) |
| 108 | + if value: |
| 109 | + setattr(obj, col, value) |
| 110 | + Book.query.session.add(obj) |
| 111 | + Book.query.session.commit() |
| 112 | + return { |
| 113 | + 'message': 'record updated', |
| 114 | + 'success': True |
| 115 | + } |
| 116 | + |
| 117 | + @token_required |
| 118 | + def delete(self, model_id: int): |
| 119 | + to_delete = Book.query.filter_by(id=model_id) |
| 120 | + if to_delete.count() == 0: |
| 121 | + return { |
| 122 | + 'message': 'matching record not found', |
| 123 | + 'success': False |
| 124 | + }, 404 |
| 125 | + to_delete.delete() |
| 126 | + Book.query.session.commit() |
| 127 | + return { |
| 128 | + 'message': 'record deleted!', |
| 129 | + 'success': True |
| 130 | + }, 200 |
17 | 131 |
|
0 commit comments