def get_by_alias(alias: str, user_service: UserService): try: user = user_service.find_by_alias(alias) return jsonify({"user": UserVo(user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def signin(auth_service: AuthService, user_serivce: UserService): try: auth = auth_service.validate(request.headers["Authorization"]) if not auth: return jsonify({"message": "Not authorized"}), 401 return jsonify({"auth": AuthVo(auth).to_dict(), "user": UserVo(auth.user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def delete(id: str, user_service: UserService, auth_service: AuthService): try: auth = auth_service.validate(request.headers["Authorization"]) if not auth: return jsonify({"message": "Not authorized"}), 401 user = user_service.delete(id) return jsonify({"user": UserVo(user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def to_dict(self) -> Dict: return { "id": self.note.id, "user": UserVo(self.note.user).to_dict(), "title": self.note.title, "tags": [TagVo(tag).to_dict() for tag in self.note.tags], "description": self.note.description, "status": self.note.status, "createdAt": self.note.created_at, "updatedAt": self.note.updated_at }
def signup(auth_service: AuthService, user_service: UserService): try: form = json.loads(request.data.decode('utf-8')) user = user_service.create(copy.deepcopy(form)) auth = auth_service.login(copy.deepcopy(form), user.password) return jsonify({"auth": AuthVo(auth).to_dict(), "user": UserVo(user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def index(user_service: UserService): try: offset = request.args.get('offset', default=0, type=int) limit = request.args.get('limit', default=10, type=int) user_models = user_service.get(offset, limit) users = [UserVo(user_model).to_dict() for user_model in user_models] return jsonify({"users": users}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def login(auth_service: AuthService, user_service: UserService): try: form = json.loads(request.data.decode('utf-8')) user = user_service.find_by_email(form["email"]) if not user: return jsonify({"message": "User is not found."}), 400 auth = auth_service.login(form, user.password) return jsonify({"auth": AuthVo(auth).to_dict(), "user": UserVo(user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500
def update(user_service: UserService, file_service: FileService, 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"]) if request.files: file_ = request.files.get('file') file_model = file_service.create(fields["id"], file_) fields["avatar"] = file_model.path user = user_service.update(fields) return jsonify({"user": UserVo(user).to_dict()}), 200 except Exception as e: logger.error(e) return jsonify({'message': 'Failed ...'}), 500