@@ -103,61 +103,61 @@ def searchBooks(db: Session, title, author, skip: int, onlyEbooks: bool, noEbook
103103 models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == True ))).with_entities (func .count ()).scalar ()
104104 result = db .query (models .Book ).filter (
105105 (or_ (models .Book .title .icontains (title ),
106- models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == True ))).limit (limit ).offset (skip ).all ()
106+ models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == True ))).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
107107 return result , resultCount
108108 elif (onlyEbooks == False ) and (noEbooks == True ):
109109 resultCount = db .query (models .Book ).filter (
110110 (or_ (models .Book .title .icontains (title ),
111111 models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == False ))).with_entities (func .count ()).scalar ()
112112 result = db .query (models .Book ).filter (
113113 (or_ (models .Book .title .icontains (title ),
114- models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == False ))).limit (limit ).offset (skip ).all ()
114+ models .Book .author .icontains (author )) & (models .Book .owned == True ) & (models .Book .ebook == False ))).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
115115 return result , resultCount
116116 else :
117117 resultCount = db .query (models .Book ).filter (
118118 or_ (models .Book .title .icontains (title ),
119119 models .Book .author .icontains (author )) & (models .Book .owned == True )).with_entities (func .count ()).scalar ()
120120 result = db .query (models .Book ).filter (
121121 or_ (models .Book .title .icontains (title ),
122- models .Book .author .icontains (author )) & (models .Book .owned == True )).limit (limit ).offset (skip ).all ()
122+ models .Book .author .icontains (author )) & (models .Book .owned == True )).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
123123 return result , resultCount
124124
125125def searchBooksbyAuthor (db : Session , author , skip : int , onlyEbooks : bool , noEbooks :bool , limit : int = 50 ):
126126 if (onlyEbooks == True ) and (noEbooks == False ):
127127 resultCount = db .query (models .Book ).filter (
128128 models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == True )).with_entities (func .count ()).scalar ()
129129 result = db .query (models .Book ).filter (
130- models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == True )).limit (limit ).offset (skip ).all ()
130+ models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == True )).order_by ( models . Book . author . asc ()). limit (limit ).offset (skip ).all ()
131131 return result , resultCount
132132 elif (onlyEbooks == False ) and (noEbooks == True ):
133133 result = db .query (models .Book ).filter (
134- models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == False )).limit (limit ).offset (skip ).all ()
134+ models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == False )).order_by ( models . Book . author . asc ()). limit (limit ).offset (skip ).all ()
135135 resultCount = db .query (models .Book ).filter (
136136 models .Book .author .icontains (author ) & (models .Book .owned == True ) & (models .Book .ebook == False )).with_entities (func .count ()).scalar ()
137137 return result , resultCount
138138 else :
139139 result = db .query (models .Book ).filter (
140- models .Book .author .icontains (author ) & (models .Book .owned == True )).limit (limit ).offset (skip ).all ()
140+ models .Book .author .icontains (author ) & (models .Book .owned == True )).order_by ( models . Book . author . asc ()). limit (limit ).offset (skip ).all ()
141141 resultCount = db .query (models .Book ).filter (
142142 models .Book .author .icontains (author ) & (models .Book .owned == True )).with_entities (func .count ()).scalar ()
143143 return result , resultCount
144144
145145def searchBooksbyTitle (db : Session , title , skip : int , onlyEbooks : bool , noEbooks :bool , limit : int = 50 ):
146146 if (onlyEbooks == True ) and (noEbooks == False ):
147147 result = db .query (models .Book ).filter (
148- models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == True )).limit (limit ).offset (skip ).all ()
148+ models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == True )).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
149149 resultCount = db .query (models .Book ).filter (
150150 models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == True )).with_entities (func .count ()).scalar ()
151151 return result , resultCount
152152 elif (onlyEbooks == False ) and (noEbooks == True ):
153153 result = db .query (models .Book ).filter (
154- models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == False )).limit (limit ).offset (skip ).all ()
154+ models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == False )).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
155155 resultCount = db .query (models .Book ).filter (
156156 models .Book .title .icontains (title ) & (models .Book .owned == True ) & (models .Book .ebook == False )).with_entities (func .count ()).scalar ()
157157 return result , resultCount
158158 else :
159159 result = db .query (models .Book ).filter (
160- models .Book .title .icontains (title ) & (models .Book .owned == True )).limit (limit ).offset (skip ).all ()
160+ models .Book .title .icontains (title ) & (models .Book .owned == True )).order_by ( models . Book . title . asc ()). limit (limit ).offset (skip ).all ()
161161 resultCount = db .query (models .Book ).filter (
162162 models .Book .title .icontains (title ) & (models .Book .owned == True )).with_entities (func .count ()).scalar ()
163163 return result , resultCount
@@ -610,3 +610,25 @@ def updateVkey(db: Session, newKey: schemas.vkey):
610610 except Exception as e :
611611 print (e )
612612 return False
613+
614+
615+ def stats (db : Session , isSum , group , target ):
616+ if (isSum == True ) and (len (group )) == 0 :
617+ results = db .query (getattr (models .Book , target ), func .sum (getattr (models .Book , target ))).all ()
618+ return dict (results )
619+ elif (isSum == False ) and (len (group )) == 0 :
620+ results = db .query (getattr (models .Book , target ), func .count ()).all ()
621+ return dict (results )
622+ elif (isSum == True ) and (len (group )) != 0 :
623+ results = db .query (getattr (models .Book , group ), func .sum (getattr (models .Book , target ))).group_by (getattr (models .Book , group )).all ()
624+ return dict (results )
625+ else :
626+ results = db .query (getattr (models .Book , target ), func .count ()).group_by (getattr (models .Book , group )).all ()
627+ return dict (results )
628+
629+ #def advancedSearch(db: Session, isSum, group, target):
630+ #author,title,location, genre
631+ #ebook / not ebook
632+ #ISBN
633+ #library, shelf
634+ #2 custom fields
0 commit comments