def get(id: str, note_service: NoteService): try: note = note_service.find(id) return jsonify({"note": NoteVo(note).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def _bind(binder): # Auth auth_repository = AuthRepository(db) auth_service = AuthService(auth_repository) binder.bind(AuthRepository, to=auth_repository, scope=request) binder.bind(AuthService, to=auth_service, scope=request) # File file_repository = FileRepository(db) file_service = FileService(file_repository) binder.bind(FileRepository, to=file_repository, scope=request) binder.bind(FileService, to=file_service, scope=request) # User user_repository = UserRepository(db) user_service = UserService(user_repository) binder.bind(UserRepository, to=user_repository, scope=request) binder.bind(UserService, to=user_service, scope=request) # Tag tag_repository = TagRepository(db) binder.bind(TagRepository, to=tag_repository, scope=request) # Note note_repository = NoteRepository(db) note_service = NoteService(note_repository, tag_repository) binder.bind(NoteRepository, to=note_repository, scope=request) binder.bind(NoteService, to=note_service, scope=request)
def post(self, request, *args, **kwargs): data = request.POST data = dict( title=data['title'], body=data['body'], ) note_service = NoteService() note = note_service.create_note(self.kwargs['email'], data) if note: messages.success(request, 'Note created successfully') return redirect( reverse('detail_user', kwargs={'email': self.kwargs['email']})) else: return HttpResponseBadRequest()
def delete(id: str, note_service: NoteService, auth_service: AuthService): try: auth = auth_service.validate(request.headers["Authorization"]) if not auth: return jsonify({"message": "Not authorized"}), 401 note_model = note_service.delete(id) note = NoteVo(note_model).to_dict() if note_model else {} return jsonify({"note": note}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def count(note_service: NoteService): try: status = request.args.get('status', default=Note.STATUS_PUBLISHED, type=str) user_id = request.args.get('userId', default=None, type=str) keyword = request.args.get('keyword', default=None, type=str) count = note_service.count(user_id=user_id, status=status, keyword=keyword) return jsonify({"count": count}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def update(note_service: NoteService, auth_service: AuthService): try: auth = auth_service.validate(request.headers["Authorization"]) if not auth: return jsonify({"message": "Not authorized"}), 401 fields = json.loads(request.form["fields"]) logger.error(fields["tags"]) logger.error(type(fields["tags"])) fields = _convert_to_snake_case(fields) note = note_service.update(fields) return jsonify({"note": NoteVo(note).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def index(note_service: NoteService): try: offset = request.args.get('offset', default=0, type=int) limit = request.args.get('limit', default=10, type=int) status = request.args.get('status', default=Note.STATUS_PUBLISHED, type=str) user_id = request.args.get('userId', default=None, type=str) keyword = request.args.get('keyword', default=None, type=str) note_models = note_service.get(offset=offset, limit=limit, user_id=user_id, status=status, keyword=keyword) notes = [NoteVo(note_model).to_dict() for note_model in note_models] return jsonify({"notes": notes}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500