mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	refactored document rest api
This commit is contained in:
		| @@ -3,7 +3,8 @@ import { ActivatedRoute, Router } from '@angular/router'; | |||||||
| import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; | import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; | ||||||
| import { cloneFilterRules, FilterRule } from 'src/app/data/filter-rule'; | import { cloneFilterRules, FilterRule } from 'src/app/data/filter-rule'; | ||||||
| import { SavedViewConfig } from 'src/app/data/saved-view-config'; | import { SavedViewConfig } from 'src/app/data/saved-view-config'; | ||||||
| import { DocumentListViewService, SORT_FIELDS } from 'src/app/services/document-list-view.service'; | import { DocumentListViewService } from 'src/app/services/document-list-view.service'; | ||||||
|  | import { DOCUMENT_SORT_FIELDS } from 'src/app/services/rest/document.service'; | ||||||
| import { SavedViewConfigService } from 'src/app/services/saved-view-config.service'; | import { SavedViewConfigService } from 'src/app/services/saved-view-config.service'; | ||||||
| import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component'; | import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component'; | ||||||
|  |  | ||||||
| @@ -26,7 +27,7 @@ export class DocumentListComponent implements OnInit { | |||||||
|   showFilter = false |   showFilter = false | ||||||
|  |  | ||||||
|   getSortFields() { |   getSortFields() { | ||||||
|     return SORT_FIELDS |     return DOCUMENT_SORT_FIELDS | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   setSort(field: string) { |   setSort(field: string) { | ||||||
|   | |||||||
| @@ -1,20 +1,15 @@ | |||||||
| import { FilterRuleType } from './filter-rule-type'; | import { FilterRuleType } from './filter-rule-type'; | ||||||
|  |  | ||||||
|  |  | ||||||
| export function filterRulesToQueryParams(filterRules: FilterRule[]) { |  | ||||||
|   let params = {} |  | ||||||
|   for (let rule of filterRules) { |  | ||||||
|     params[rule.type.filtervar] = rule.value |  | ||||||
|   } |  | ||||||
|   return params |  | ||||||
| } |  | ||||||
|  |  | ||||||
| export function cloneFilterRules(filterRules: FilterRule[]): FilterRule[] { | export function cloneFilterRules(filterRules: FilterRule[]): FilterRule[] { | ||||||
|  |   if (filterRules) { | ||||||
|     let newRules: FilterRule[] = [] |     let newRules: FilterRule[] = [] | ||||||
|     for (let rule of filterRules) { |     for (let rule of filterRules) { | ||||||
|       newRules.push({type: rule.type, value: rule.value}) |       newRules.push({type: rule.type, value: rule.value}) | ||||||
|     } |     } | ||||||
|     return newRules       |     return newRules       | ||||||
|  |   } else { | ||||||
|  |     return null | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface FilterRule { | export interface FilterRule { | ||||||
|   | |||||||
| @@ -1,18 +1,10 @@ | |||||||
| import { Injectable } from '@angular/core'; | import { Injectable } from '@angular/core'; | ||||||
| import { Observable } from 'rxjs'; | import { Observable } from 'rxjs'; | ||||||
| import { cloneFilterRules, FilterRule, filterRulesToQueryParams } from '../data/filter-rule'; | import { cloneFilterRules, FilterRule } from '../data/filter-rule'; | ||||||
| import { PaperlessDocument } from '../data/paperless-document'; | import { PaperlessDocument } from '../data/paperless-document'; | ||||||
| import { SavedViewConfig } from '../data/saved-view-config'; | import { SavedViewConfig } from '../data/saved-view-config'; | ||||||
| import { DocumentService } from './rest/document.service'; | import { DocumentService, SORT_DIRECTION_DESCENDING } from './rest/document.service'; | ||||||
|  |  | ||||||
| export const SORT_FIELDS = [ |  | ||||||
|   {field: "correspondent__name", name: "Correspondent"}, |  | ||||||
|   {field: 'title', name: 'Title'}, |  | ||||||
|   {field: 'archive_serial_number', name: 'ASN'}, |  | ||||||
|   {field: 'created', name: 'Created'}, |  | ||||||
|   {field: 'added', name: 'Added'}, |  | ||||||
|   {field: 'modified', name: 'Modified'} |  | ||||||
| ] |  | ||||||
|  |  | ||||||
| @Injectable({ | @Injectable({ | ||||||
|   providedIn: 'root' |   providedIn: 'root' | ||||||
| @@ -26,27 +18,31 @@ export class DocumentListViewService { | |||||||
|   collectionSize: number |   collectionSize: number | ||||||
|  |  | ||||||
|   currentFilterRules: FilterRule[] = [] |   currentFilterRules: FilterRule[] = [] | ||||||
|   currentSortDirection = 'des' |   currentSortDirection = SORT_DIRECTION_DESCENDING | ||||||
|   currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD |   currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD | ||||||
|    |    | ||||||
|   viewConfig: SavedViewConfig |   viewConfig: SavedViewConfig | ||||||
|  |  | ||||||
|   reload(onFinish?) { |   reload(onFinish?) { | ||||||
|     let ordering: string |     let sortField: string | ||||||
|  |     let sortDirection: string | ||||||
|     let filterRules: FilterRule[] |     let filterRules: FilterRule[] | ||||||
|     if (this.viewConfig) { |     if (this.viewConfig) { | ||||||
|       ordering = this.getOrderingQueryParam(this.viewConfig.sortField, this.viewConfig.sortDirection) |       sortField = this.viewConfig.sortField | ||||||
|  |       sortDirection = this.viewConfig.sortDirection | ||||||
|       filterRules = this.viewConfig.filterRules |       filterRules = this.viewConfig.filterRules | ||||||
|     } else { |     } else { | ||||||
|       ordering = this.getOrderingQueryParam(this.currentSortField, this.currentSortDirection) |       sortField = this.currentSortField | ||||||
|  |       sortDirection = this.currentSortDirection | ||||||
|       filterRules = this.currentFilterRules |       filterRules = this.currentFilterRules | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     this.documentService.list( |     this.documentService.list( | ||||||
|       this.currentPage, |       this.currentPage, | ||||||
|       null, |       null, | ||||||
|       ordering, |       sortField, | ||||||
|       filterRulesToQueryParams(filterRules)).subscribe( |       sortDirection, | ||||||
|  |       filterRules).subscribe( | ||||||
|         result => { |         result => { | ||||||
|           this.collectionSize = result.count |           this.collectionSize = result.count | ||||||
|           this.documents = result.results |           this.documents = result.results | ||||||
| @@ -62,13 +58,6 @@ export class DocumentListViewService { | |||||||
|         }) |         }) | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   getOrderingQueryParam(sortField: string, sortDirection: string) { |  | ||||||
|     if (SORT_FIELDS.find(f => f.field == sortField)) { |  | ||||||
|       return (sortDirection == 'des' ? '-' : '') + sortField |  | ||||||
|     } else { |  | ||||||
|       return DocumentListViewService.DEFAULT_SORT_FIELD |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   setFilterRules(filterRules: FilterRule[]) { |   setFilterRules(filterRules: FilterRule[]) { | ||||||
|     this.currentFilterRules = cloneFilterRules(filterRules) |     this.currentFilterRules = cloneFilterRules(filterRules) | ||||||
|   | |||||||
| @@ -2,8 +2,24 @@ import { Injectable } from '@angular/core'; | |||||||
| import { PaperlessDocument } from 'src/app/data/paperless-document'; | import { PaperlessDocument } from 'src/app/data/paperless-document'; | ||||||
| import { AbstractPaperlessService } from './abstract-paperless-service'; | import { AbstractPaperlessService } from './abstract-paperless-service'; | ||||||
| import { HttpClient } from '@angular/common/http'; | import { HttpClient } from '@angular/common/http'; | ||||||
| import { Observable } from 'rxjs'; |  | ||||||
| import { AuthService } from '../auth.service'; | import { AuthService } from '../auth.service'; | ||||||
|  | import { Observable } from 'rxjs'; | ||||||
|  | import { Results } from 'src/app/data/results'; | ||||||
|  | import { FilterRule } from 'src/app/data/filter-rule'; | ||||||
|  |  | ||||||
|  |  | ||||||
|  | export const DOCUMENT_SORT_FIELDS = [ | ||||||
|  |   { field: "correspondent__name", name: "Correspondent" }, | ||||||
|  |   { field: 'title', name: 'Title' }, | ||||||
|  |   { field: 'archive_serial_number', name: 'ASN' }, | ||||||
|  |   { field: 'created', name: 'Created' }, | ||||||
|  |   { field: 'added', name: 'Added' }, | ||||||
|  |   { field: 'modified', name: 'Modified' } | ||||||
|  | ] | ||||||
|  |  | ||||||
|  | export const SORT_DIRECTION_ASCENDING = "asc" | ||||||
|  | export const SORT_DIRECTION_DESCENDING = "des" | ||||||
|  |  | ||||||
|  |  | ||||||
| @Injectable({ | @Injectable({ | ||||||
|   providedIn: 'root' |   providedIn: 'root' | ||||||
| @@ -14,6 +30,30 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument> | |||||||
|     super(http, 'documents') |     super(http, 'documents') | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   private filterRulesToQueryParams(filterRules: FilterRule[]) { | ||||||
|  |     if (filterRules) { | ||||||
|  |       let params = {} | ||||||
|  |       for (let rule of filterRules) { | ||||||
|  |         params[rule.type.filtervar] = rule.value | ||||||
|  |       } | ||||||
|  |       return params | ||||||
|  |     } else { | ||||||
|  |       return null | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private getOrderingQueryParam(sortField: string, sortDirection: string) { | ||||||
|  |     if (DOCUMENT_SORT_FIELDS.find(f => f.field == sortField)) { | ||||||
|  |       return (sortDirection == SORT_DIRECTION_DESCENDING ? '-' : '') + sortField | ||||||
|  |     } else { | ||||||
|  |       return null | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   list(page?: number, pageSize?: number, sortField?: string, sortDirection?: string, filterRules?: FilterRule[]): Observable<Results<PaperlessDocument>> { | ||||||
|  |     return super.list(page, pageSize, this.getOrderingQueryParam(sortField, sortDirection), this.filterRulesToQueryParams(filterRules)) | ||||||
|  |   } | ||||||
|  |  | ||||||
|   getPreviewUrl(id: number): string { |   getPreviewUrl(id: number): string { | ||||||
|     return this.getResourceUrl(id, 'preview') + `?auth_token=${this.auth.getToken()}` |     return this.getResourceUrl(id, 'preview') + `?auth_token=${this.auth.getToken()}` | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonas Winkler
					Jonas Winkler