Angular 项目TaskService

增删改查嗯ProjectService差不多

import { Task } from './../domain';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { count, map, mapTo, mergeMap, switchMap } from 'rxjs/operators';
import { from, Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class TaskService {
  private readonly domain = 'tasks';
  private headers = new Headers({
    'Content-type': 'application/json'
  });
  constructor(private httpClient: HttpClient, @Inject('BASE_CONFIG') private config: any) { }

  //POST
  add(task: Task): Observable<Task> {
    task.id = undefined;
    const uri = `${this.config.uri}/${this.domain}`;
    return this.httpClient.post(uri, JSON.stringify(task)).pipe(
      map(res => res as Task)
    )
  }

  //PUT/patch
  update(task: Task): Observable<Task> {
    const uri = `${this.config.uri}/${this.domain}/${task.id}`;
    const toUpdate = {
      desc: task.desc,
      ownerId: task.ownerId,
      participantIds: task.participantIds,
      dueDate: task.dueDate,
      reminder: task.reminder,
      priority: task.priority,
      remark: task.remark
    };
    return this.httpClient.patch(uri, JSON.stringify(toUpdate)).pipe(
      map(res => res as Task)
    )
  }

  //DELETE
  delete(task: Task): Observable<Task> {
    const uri = `${this.config.uri}/${this.domain}/${task.id}`;
    return this.httpClient.delete(uri).pipe(
      mapTo(task)
    )
  }

  //GET
  get(taskId: string): Observable<Task[]> {
    const uri = `${this.config.uri}/${this.domain}`;
    return this.httpClient.get(uri, { params: { 'taskId': taskId } }).pipe(
      map(res => res as Task[])
    )
  }

}

把所有Project相关的列表,以及相关任务全部load出来

  getByLists(lists: TaskList[]): Observable<Task[]> {
    return from(lists)
      .pipe(
        mergeMap((list: TaskList) => this.get(<string>list.id)),
        reduce((tasks: Task[], t: Task[]) => [...tasks, ...t], [])
      )
  }

完成任务,取消完成任务

complete(task: Task): Observable<Task> {
    const uri = `${this.config.uri}/${this.domain}/${task.id}`;
    return this.httpClient.patch<Task>(
      uri,
      JSON.stringify({ completed: !task.completed }),
      { headers: this.headers }
    ).pipe(map(res => res as Task));
  }

移动Task

  move(taskId: string, taskListId: string): Observable<Task> {
    const uri = `${this.config.uri}/${this.domain}/${taskId}`;
    return this.httpClient.patch<Task>(
      uri,
      JSON.stringify({ taskListId: taskListId }),
      { headers: this.headers }
    );
  }

移动全部task: 移动srcList中所有任务到target列表

  moveAll(srcListId: string, targetListId: string): Observable<Task[]> {
    return this.get(srcListId).pipe(
      mergeMap((tasks: Task[]) => from(tasks)),
      mergeMap((task: Task) => this.move(<string>task.id, targetListId)),
      reduce((arrTasks: Task[], t: Task) => {
        return [...arrTasks, t];
      }, [])
    );
  }

 

上一篇:CatBoost原理及其工具库实际使用


下一篇:臭氧果蔬消毒器市场现状研究分析与发展前景预测报告