Skip to content

Commit

Permalink
Refactor wordpress service into Abstract Base Class
Browse files Browse the repository at this point in the history
  • Loading branch information
joelj1995 committed Oct 6, 2022
1 parent 6a970b3 commit b0f61b4
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion blog-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { PostViewComponent } from './post-view/post-view.component';
import { PageViewComponent } from './page-view/page-view.component';
import { CacheService } from './services/cache.service';
import { SubscribeComponent } from './subscribe/subscribe.component';
import { IContentService } from './services/abc/content.service';

const routes: Routes = [
{ path: '', component: BlogRollComponent },
Expand All @@ -39,7 +40,7 @@ const routes: Routes = [
NgbModule,
RouterModule.forRoot(routes)
],
providers: [WordpressService, CacheService],
providers: [{provide: IContentService, useClass: WordpressService}, CacheService],
bootstrap: [AppComponent]
})
export class AppModule { }
6 changes: 3 additions & 3 deletions blog-app/src/app/blog-roll/blog-roll.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BlogError } from '../models/blog-error';
import { Posts } from '../models/posts';
import { WordpressService } from '../services/wordpress.service';
import { IContentService } from '../services/abc/content.service';

@Component({
selector: 'app-blog-roll',
Expand All @@ -12,7 +12,7 @@ import { WordpressService } from '../services/wordpress.service';
export class BlogRollComponent implements OnInit {

constructor(
private wordpressService: WordpressService,
private contentService: IContentService,
private route: ActivatedRoute
) { }

Expand All @@ -33,7 +33,7 @@ export class BlogRollComponent implements OnInit {
} else {
this.currentPage = 1;
}
this.wordpressService.getPosts(this.currentPage, this.perPage)
this.contentService.getPosts(this.currentPage, this.perPage)
.subscribe({
next: data => {
this.posts = <Posts>data;
Expand Down
Empty file added blog-app/src/app/models/page.ts
Empty file.
Empty file added blog-app/src/app/models/post.ts
Empty file.
6 changes: 3 additions & 3 deletions blog-app/src/app/page-view/page-view.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { WordpressService } from '../services/wordpress.service';
import { IContentService } from '../services/abc/content.service';
import { WpPage } from '../wp-model/wp-page';

@Component({
Expand All @@ -11,7 +11,7 @@ import { WpPage } from '../wp-model/wp-page';
export class PageViewComponent implements OnInit {

constructor(
private wordpressService: WordpressService,
private contentService: IContentService,
private route: ActivatedRoute
) { }

Expand All @@ -21,7 +21,7 @@ export class PageViewComponent implements OnInit {
ngOnInit(): void {
this.route.params.subscribe(params => {
this.slug = params['slug'];
this.wordpressService.getPage(this.slug).subscribe(data => {
this.contentService.getPage(this.slug).subscribe(data => {
if (data.length > 0) {
this.page = data[0];
}
Expand Down
6 changes: 3 additions & 3 deletions blog-app/src/app/post-view/post-view.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { WordpressService } from '../services/wordpress.service';
import { IContentService } from '../services/abc/content.service';
import { WpPost } from '../wp-model/wp-post';

@Component({
Expand All @@ -11,7 +11,7 @@ import { WpPost } from '../wp-model/wp-post';
export class PostViewComponent implements OnInit {

constructor(
private wordpressService: WordpressService,
private contentService: IContentService,
private route: ActivatedRoute
) { }

Expand All @@ -25,7 +25,7 @@ export class PostViewComponent implements OnInit {
this.post = null;
this.notFound = false;
this.slug = params['slug'];
this.wordpressService.getPost(this.slug).subscribe(data => {
this.contentService.getPost(this.slug).subscribe(data => {
if (data.length > 0) {
this.post = data[0];
} else {
Expand Down
13 changes: 13 additions & 0 deletions blog-app/src/app/services/abc/content.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { BlogError } from 'src/app/models/blog-error';
import { Posts } from 'src/app/models/posts';
import { WpPage } from 'src/app/wp-model/wp-page';
import { WpPost } from 'src/app/wp-model/wp-post';

@Injectable()
export abstract class IContentService {
abstract getPage(slug: string): Observable<WpPage[]>;
abstract getPosts(page: number, perPage: number): Observable<Posts | BlogError>;
abstract getPost(slug: string): Observable<WpPost[]>;
}
3 changes: 2 additions & 1 deletion blog-app/src/app/services/wordpress.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { BlogError } from '../models/blog-error';
import { Posts } from '../models/posts'
import { WpPage } from '../wp-model/wp-page'
import { WpPost } from '../wp-model/wp-post';
import { IContentService } from './abc/content.service';
import { CacheService } from './cache.service';

@Injectable({
providedIn: 'root'
})
export class WordpressService {
export class WordpressService implements IContentService {

BASE_URL: string = `${environment.wpBaseUrl}/wordpress/index.php/wp-json/wp/v2`

Expand Down

0 comments on commit b0f61b4

Please sign in to comment.