basic post and user services

This commit is contained in:
Benedek László 2024-04-09 00:29:58 +02:00
parent bc8c7a3a07
commit 154bcca1a3
7 changed files with 171 additions and 86 deletions

View File

@ -1,56 +1,20 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Post } from '../../model/Post'; import { Post } from '../../model/Post';
import { PostService } from '../services/data/post.service';
@Component({ @Component({
selector: 'app-feed', selector: 'app-feed',
templateUrl: './feed.component.html', templateUrl: './feed.component.html',
styleUrl: './feed.component.scss' styleUrl: './feed.component.scss'
}) })
export class FeedComponent { export class FeedComponent implements OnInit {
posts: Array<Post> = new Array<Post>( posts: Array<Post> | undefined;
{
user: { constructor(private postService: PostService) { }
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png", ngOnInit(): void {
followed: true this.postService.getFeed(10, 0)
}, .then(posts => { this.posts = posts; })
content: "Bukott diák vagyok én, nem bírom el a szenvedést.", .catch(()=> { alert('failed to fetch feed'); });
image: "assets/placeholder-profile-picture.png", }
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
);
} }

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { PostService } from './post.service';
describe('PostService', () => {
let service: PostService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(PostService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,80 @@
import { Injectable } from '@angular/core';
import { Post } from '../../../model/Post';
import { AuthService } from '../auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class PostService {
constructor(private auth: AuthService) { }
examplePosts: Array<Post> = new Array<Post>(
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
},
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
);
getFeed(length: Number, start: Number): Promise<Array<Post>> {
return new Promise<Array<Post>>((resolve, reject) => {
// TODO: fetch feed
this.auth.getToken()
.then( (token)=> {
resolve(this.examplePosts);
})
.catch( ()=> { reject() });
});
}
getPosts(username: String, lenght: Number, start: Number) {
return new Promise<Array<Post>>((resolve, reject) => {
// TODO: fetch feed
this.auth.getToken()
.then( (token)=> {
resolve(this.examplePosts);
})
.catch( ()=> { reject() });
});
}
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { UserService } from './user.service';
describe('UserService', () => {
let service: UserService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(UserService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { User } from '../../../model/User';
import { AuthService } from '../auth/auth.service';
@Injectable({
providedIn: 'root'
})
export class UserService {
exampleUser: User = {
username: "Zámbó Jimmy",
picture: "assets/placeholder-profile-picture.png",
followed: true
}
constructor(private auth: AuthService) { }
getUser(username: String): Promise<User> {
return new Promise<User>((resolve, reject) => {
// TODO: get user
this.auth.getToken()
.then((token) => {
resolve(this.exampleUser);
})
.catch(() => { reject() });
});
}
}

View File

@ -1,6 +1,6 @@
<app-top-menu></app-top-menu> <app-top-menu></app-top-menu>
<app-side-menu></app-side-menu> <app-side-menu></app-side-menu>
<div id="main"> <div id="main">
<app-large-user [user]="exampleUser"></app-large-user> <app-large-user [user]="user"></app-large-user>
<app-post *ngFor="let post of posts" [post]="post"></app-post> <app-post *ngFor="let post of posts" [post]="post"></app-post>
</div> </div>

View File

@ -1,47 +1,28 @@
import { Component } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Post } from '../../model/Post'; import { Post } from '../../model/Post';
import { User } from '../../model/User'; import { User } from '../../model/User';
import { PostService } from '../services/data/post.service';
import { ActivatedRoute } from '@angular/router';
import { UserService } from '../services/data/user.service';
@Component({ @Component({
selector: 'app-user', selector: 'app-user',
templateUrl: './user.component.html', templateUrl: './user.component.html',
styleUrl: './user.component.scss' styleUrl: './user.component.scss'
}) })
export class UserComponent { export class UserComponent implements OnInit{
exampleUser: User = { user: User | undefined;
username: "Zámbó Jimmy", posts: Array<Post> | undefined;
picture: "assets/placeholder-profile-picture.png",
followed: true
}
posts: Array<Post> = new Array<Post>( constructor(private postService: PostService, private userService: UserService, private route: ActivatedRoute) { }
{
user: this.exampleUser, ngOnInit(): void {
content: "Bukott diák vagyok én, nem bírom el a szenvedést.", this.userService.getUser(this.route.snapshot.params['username'])
image: "assets/placeholder-profile-picture.png", .then(user => { this.user = user })
likes: 1, .catch(() => alert('failed to get user'));
liked: true
}, this.postService.getPosts(this.route.snapshot.params['username'], 10, 0)
{ .then(posts => { this.posts = posts; })
user: this.exampleUser, .catch(()=> { alert('failed to fetch feed'); });
content: "Bukott diák vagyok én, nem bírom el a szenvedést.", }
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: this.exampleUser,
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
{
user: this.exampleUser,
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
image: "assets/placeholder-profile-picture.png",
likes: 1,
liked: true
},
);
} }