0

A produtividade para o desenvolvimento de um sistema muitas vezes acontece devido a sabedoria de reaproveitar códigos e/ou components

Analisando meu código percebi que meu crud se repete muitas vezes mudando apenas a url que chama o backend

http://localhost/api/v1/status
http://localhost/api/v1/cliente
http://localhost/api/v1/produto

E assim por diante

Ao criar um service no Angular v8

status.service

@Injectable({
  providedIn: 'root'
})
export class StatusService {
    private ws: string
    constructor(private http: HttpClient) {
        this.ws = `${environment.host}${environment.api}/status`
    }
    get(){
        return this.http.get(this.ws)
    }
    getById(id: number){
        return this.http.get(`${this.ws}/${id}`)
    }
    update(id: number,obj: Status){
        return this.http.put(`${this.ws}/${id}`, obj)
    }
    create(obj: Status){
        return this.http.post(this.ws, obj)
    }
    delete(id: number){
        return this.http.delete(`${this.ws}/${id}`)
    }
}

cliente.service

@Injectable({
  providedIn: 'root'
})
export class ClienteService {
    private ws: string
    constructor(private http: HttpClient) {
        this.ws = `${environment.host}${environment.api}/cliente`
    }
    get(){
        return this.http.get(this.ws)
    }
    getById(id: number){
        return this.http.get(`${this.ws}/${id}`)
    }
    update(id: number,obj: Status){
        return this.http.put(`${this.ws}/${id}`, obj)
    }
    create(obj: Status){
        return this.http.post(this.ws, obj)
    }
    delete(id: number){
        return this.http.delete(`${this.ws}/${id}`)
    }
}

produto.service

@Injectable({
  providedIn: 'root'
})
export class StatusService {
    private ws: string
    constructor(private http: HttpClient) {
        this.ws = `${environment.host}${environment.api}/produto`
    }
    get(){
        return this.http.get(this.ws)
    }
    getById(id: number){
        return this.http.get(`${this.ws}/${id}`)
    }
    update(id: number,obj: Status){
        return this.http.put(`${this.ws}/${id}`, obj)
    }
    create(obj: Status){
        return this.http.post(this.ws, obj)
    }
    delete(id: number){
        return this.http.delete(`${this.ws}/${id}`)
    }
}

Como eu poderia reaproveitar esse código chamando apenas uma vez e modificando a url?

Já ouvir falar no injector do angular, vi uns código e ainda não entendi como usar

Gostaria de ajuda nesse sentido, de entender como usar o injector

1 Resposta 1

0

Uma solução (se você deseja realmente utilizar um service "genérico") seria criar um serviço da seguinte maneira:

@Injectable({
  providedIn: 'root'
})
export class CRUDService {
    private ws: string
    constructor(private http: HttpClient, endpoint: string) {
        this.ws = `${environment.host}${environment.api}/${endpoint}`
    }
    get(){
        return this.http.get(this.ws)
    }
    getById(id: number){
        return this.http.get(`${this.ws}/${id}`)
    }
    update(id: number,obj: Status){
        return this.http.put(`${this.ws}/${id}`, obj)
    }
    create(obj: Status){
        return this.http.post(this.ws, obj)
    }
    delete(id: number){
        return this.http.delete(`${this.ws}/${id}`)
    }
}

PS: Todos os seus Creates e Updates, tem como parâmetro um objeto do tipo 'STATUS', se atente a isso!

1
  • A chamada seria instanciando? 9/03/2021 às 12:38

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .