refactored document rest api
This commit is contained in:
		
							parent
							
								
									20e93156bf
								
							
						
					
					
						commit
						a1c3645a4b
					
				@ -3,7 +3,8 @@ import { ActivatedRoute, Router } from '@angular/router';
 | 
			
		||||
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
 | 
			
		||||
import { cloneFilterRules, FilterRule } from 'src/app/data/filter-rule';
 | 
			
		||||
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 { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component';
 | 
			
		||||
 | 
			
		||||
@ -26,7 +27,7 @@ export class DocumentListComponent implements OnInit {
 | 
			
		||||
  showFilter = false
 | 
			
		||||
 | 
			
		||||
  getSortFields() {
 | 
			
		||||
    return SORT_FIELDS
 | 
			
		||||
    return DOCUMENT_SORT_FIELDS
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  setSort(field: string) {
 | 
			
		||||
 | 
			
		||||
@ -1,20 +1,15 @@
 | 
			
		||||
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[] {
 | 
			
		||||
  let newRules: FilterRule[] = []
 | 
			
		||||
  for (let rule of filterRules) {
 | 
			
		||||
    newRules.push({type: rule.type, value: rule.value})
 | 
			
		||||
  if (filterRules) {
 | 
			
		||||
    let newRules: FilterRule[] = []
 | 
			
		||||
    for (let rule of filterRules) {
 | 
			
		||||
      newRules.push({type: rule.type, value: rule.value})
 | 
			
		||||
    }
 | 
			
		||||
    return newRules      
 | 
			
		||||
  } else {
 | 
			
		||||
    return null
 | 
			
		||||
  }
 | 
			
		||||
  return newRules
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface FilterRule {
 | 
			
		||||
 | 
			
		||||
@ -1,18 +1,10 @@
 | 
			
		||||
import { Injectable } from '@angular/core';
 | 
			
		||||
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 { 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({
 | 
			
		||||
  providedIn: 'root'
 | 
			
		||||
@ -26,27 +18,31 @@ export class DocumentListViewService {
 | 
			
		||||
  collectionSize: number
 | 
			
		||||
 | 
			
		||||
  currentFilterRules: FilterRule[] = []
 | 
			
		||||
  currentSortDirection = 'des'
 | 
			
		||||
  currentSortDirection = SORT_DIRECTION_DESCENDING
 | 
			
		||||
  currentSortField = DocumentListViewService.DEFAULT_SORT_FIELD
 | 
			
		||||
  
 | 
			
		||||
  viewConfig: SavedViewConfig
 | 
			
		||||
 | 
			
		||||
  reload(onFinish?) {
 | 
			
		||||
    let ordering: string
 | 
			
		||||
    let sortField: string
 | 
			
		||||
    let sortDirection: string
 | 
			
		||||
    let filterRules: FilterRule[]
 | 
			
		||||
    if (this.viewConfig) {
 | 
			
		||||
      ordering = this.getOrderingQueryParam(this.viewConfig.sortField, this.viewConfig.sortDirection)
 | 
			
		||||
      sortField = this.viewConfig.sortField
 | 
			
		||||
      sortDirection = this.viewConfig.sortDirection
 | 
			
		||||
      filterRules = this.viewConfig.filterRules
 | 
			
		||||
    } else {
 | 
			
		||||
      ordering = this.getOrderingQueryParam(this.currentSortField, this.currentSortDirection)
 | 
			
		||||
      sortField = this.currentSortField
 | 
			
		||||
      sortDirection = this.currentSortDirection
 | 
			
		||||
      filterRules = this.currentFilterRules
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.documentService.list(
 | 
			
		||||
      this.currentPage,
 | 
			
		||||
      null,
 | 
			
		||||
      ordering,
 | 
			
		||||
      filterRulesToQueryParams(filterRules)).subscribe(
 | 
			
		||||
      sortField,
 | 
			
		||||
      sortDirection,
 | 
			
		||||
      filterRules).subscribe(
 | 
			
		||||
        result => {
 | 
			
		||||
          this.collectionSize = result.count
 | 
			
		||||
          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[]) {
 | 
			
		||||
    this.currentFilterRules = cloneFilterRules(filterRules)
 | 
			
		||||
 | 
			
		||||
@ -2,8 +2,24 @@ import { Injectable } from '@angular/core';
 | 
			
		||||
import { PaperlessDocument } from 'src/app/data/paperless-document';
 | 
			
		||||
import { AbstractPaperlessService } from './abstract-paperless-service';
 | 
			
		||||
import { HttpClient } from '@angular/common/http';
 | 
			
		||||
import { Observable } from 'rxjs';
 | 
			
		||||
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({
 | 
			
		||||
  providedIn: 'root'
 | 
			
		||||
@ -14,6 +30,30 @@ export class DocumentService extends AbstractPaperlessService<PaperlessDocument>
 | 
			
		||||
    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 {
 | 
			
		||||
    return this.getResourceUrl(id, 'preview') + `?auth_token=${this.auth.getToken()}`
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user