Пример #1
0
    def get(self, search_term):
        result = []
        notes = Note.query.filter(Note.title.like('%' + search_term +
                                                  '%')).all()
        for note in notes:
            result.append(Note.serialize(note))

        if len(result) > 0:
            return {"status": "succes", "data": result}, 201
        else:
            return {"message": "No Notes Found"}, 201
Пример #2
0
    def get(self):
        # print('hello')
        result = []
        # json_data = request.get_json(force=True)
        header = request.headers['Authorization']

        if not header:
            return {"Messege" : "No api key!"}, 400
        else:
            user = User.query.filter_by(api_key=header).first()
            if user:
                notes = Note.query.filter_by(user_id=user.id).all()
                for note in notes:
                    result.append(Note.serialize(note))


            return {"status": 'success', 'data': result}, 201
Пример #3
0
    def post(self):
        header = request.headers['Authorization']
        json_data = request.get_json(force=True)

        if not header:
            return {"Messege" : "No api key!"}, 400
        else:
            user = User.query.filter_by(api_key=header).first()
            if user:
                note = Note( 
                    title = json_data['title'],
                    user_id = user.id,
                    note = json_data['note'],
                )
                db.session.add(note) 
                db.session.commit()

                result = Note.serialize(note)
                return {"status": 'success', 'data': result}, 201
            else:
                return {"Messege" : "No user with that api key"}, 402
Пример #4
0
def notes(agenda_id=None, id=None):
    if request.method == 'GET':
        if id is not None and agenda_id is not None:
            note = Note.query.filter_by(agenda_id=agenda_id, id=id).first()
            if note:
                return jsonify(note.serialize()), 200
            else:
                return jsonify({"note":"Not found"}), 404
        elif id is not None:
            note = Note.query.get(id)
            if note:
                return jsonify(note.serialize()), 200
            else:     
                return jsonify({"note":"Not found"}), 404
        elif agenda_id is not None:
            notes = Note.query.filter_by(agenda_id = agenda_id).all()
            notes = list(map(lambda note: note.serialize(),notes))
            return jsonify(notes), 200
        else:
            notes = Note.query.all()
            notes = list(map(lambda note: note.serialize(), notes))
            return jsonify(notes), 200
    
    if request.method == 'POST':
        title=request.json.get('title')
        date=request.json.get('date')
        agenda_id=request.json.get('agenda_id')
        if not title:
            return jsonify({"msg": "Título es obligatorio"}), 422  
        if not date:
            return jsonify({"msg": "Fecha es obligatorio"}), 422  
        if not agenda_id:
            return jsonify({"msg": "agenda_id es obligatorio"}), 422  
           
        note = Note()
        note.title = title
        note.date = date
        note.agenda_id = agenda_id

        db.session.add(note)
        db.session.commit()

        return jsonify(note.serialize()), 201
    
    if request.method == 'PUT':
        
        note = Note.query.get(id)
        note.title = request.json.get('title')
        note.agenda_id = request.json.get('agenda_id')

        db.session.commit()

        return jsonify(note.serialize()), 200

    if request.method == 'DELETE':
        
        note = Note.query.get(id)
        db.session.delete(note)
        db.session.commit()

        return jsonify({'note':'Deleted'}), 200