import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';

import { environment } from '../../../environments/environment';
import { Post, Category } from '../../core/entities';

const CATEGORIES_URL = environment.apiUrl + '/api/categories';
const POSTS_URL = environment.apiUrl + '/api/posts';

@Injectable()
export class ByCategoryService {

	constructor(private http: HttpClient) {}

	getCategoryById(categoryId: number): Observable<Category> {
		return this.http.get<Category>(`${CATEGORIES_URL}/${categoryId}`);
	}

	getPostsByCategory(category: Category): Observable<Array<Post>> {
		return this.http.get<Array<Post>>(`${POSTS_URL}/byCategory/${category.id}`);
	}
}
