From 13318ce5f090e8371335db5ef104c8d37f644a4b Mon Sep 17 00:00:00 2001 From: Furkan Kalkan Date: Tue, 21 Jun 2022 15:09:05 +0300 Subject: [PATCH] add note search count endpoint --- src/note_service/modules/rest/business.py | 22 +++++++++++++++++----- src/note_service/modules/rest/views.py | 13 +++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/note_service/modules/rest/business.py b/src/note_service/modules/rest/business.py index b5fa76a..b2d5d53 100644 --- a/src/note_service/modules/rest/business.py +++ b/src/note_service/modules/rest/business.py @@ -6,13 +6,11 @@ import requests as requests from flask import current_app from flask_smorest import abort -from sqlalchemy import func, column -from sqlalchemy.orm.exc import NoResultFound - - from note_service.database import db from note_service.modules.rest.models import Notes from note_service.modules.rest.utils import ResponseObject, PaginationObject +from sqlalchemy import func, column +from sqlalchemy.orm.exc import NoResultFound def create_note(files, args, username): @@ -46,7 +44,8 @@ def search_notes(args, pagination_parameters): _response = _response.select_from(func.unnest(func.string_to_array(Notes.tag, ',')).alias("tags")).filter(column("tags").like(tag)) if note_info: _response = _response.filter(Notes.note_info.ilike("%" + note_info + "%")) - _response = _response.distinct().order_by(Notes.created_date.desc()).paginate(pagination_parameters.page, pagination_parameters.page_size) + _response = _response.distinct().order_by(Notes.created_date.desc()).paginate(pagination_parameters.page, + pagination_parameters.page_size) pagination_parameters.item_count = _response.total return ResponseObject(data=_response.items, page=PaginationObject(page=_response.page, total_pages=_response.pages, @@ -54,6 +53,19 @@ def search_notes(args, pagination_parameters): status=HTTPStatus.OK) +def search_item_count(args): + tag = args.get("tag") + note_info = args.get("note_info") + _response = Notes.query + _response = _response.select_from(func.unnest(func.string_to_array(Notes.tag, ',')).alias("tags")).filter( + column("tags").like(tag)) + if note_info: + _response = _response.filter(Notes.note_info.ilike("%" + note_info + "%")) + _response = _response.distinct() + + return ResponseObject(data={"count": str(_response.count())}, status=HTTPStatus.OK) + + def fetch_note(note_id): try: _response = Notes.query.filter(Notes.id == note_id).one() diff --git a/src/note_service/modules/rest/views.py b/src/note_service/modules/rest/views.py index 0221e7f..1388b0d 100644 --- a/src/note_service/modules/rest/views.py +++ b/src/note_service/modules/rest/views.py @@ -4,7 +4,7 @@ from flask_smorest import Blueprint from note_service.modules.rest.business import search_notes, create_note, fetch_note, update_note, delete_note, \ - get_pdf_key + get_pdf_key, search_item_count from note_service.modules.rest.decorators import token_required from note_service.modules.rest.schemas import Note, NoteFile, NotesResponse, NoteResponse, BaseResponse, NoteSearch @@ -35,6 +35,16 @@ def get(self, args, pagination_parameters, **kwargs): return search_notes(args, pagination_parameters) +@notes.route("/search/count") +class NoteSearchCountCollection(MethodView): + @token_required + @notes.arguments(NoteSearch, location="query") + @notes.response(HTTPStatus.OK, BaseResponse) + def get(self, args, **kwargs): + """Verilen tag bilgisine göre not aramak için kullanılır""" + return search_item_count(args) + + @notes.route("/") class NoteItemCollection(MethodView): @token_required @@ -69,4 +79,3 @@ class NoteItemCollection(MethodView): def get(self, note_id, **kwargs): """Not'un ekini görüntülenmek için pdf anahtarı oluşturur """ return get_pdf_key(note_id) -