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];
      }, [])
    );
  }
复制代码

 

posted @   starof  阅读(68)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2017-02-24 处理跨域方式
2017-02-24 用jQuery实现Ajax
2016-02-24 css3常用动画+动画库
点击右上角即可分享
微信分享提示