Skip to content

Commit

Permalink
feat: ajax comments
Browse files Browse the repository at this point in the history
  • Loading branch information
franchescoURJC committed Apr 22, 2023
1 parent a03776d commit b783406
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.daw.alist.services.PostService;
import net.daw.alist.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
Expand Down Expand Up @@ -76,6 +77,20 @@ public ResponseEntity<List<Comment>> getPostComments(@PathVariable long postId)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@GetMapping("/posts")
public ResponseEntity<List<Comment>> getNewComments(@RequestParam int id, @RequestParam int page) {
Optional<Post> optionalPost = postService.findByID(Long.valueOf(id));
if (optionalPost.isPresent()){
boolean validPage = page <= (int) Math.ceil(optionalPost.get().getComments().size()/2);
if (validPage) {
Post post = optionalPost.get();
postService.getNewComments(post.getId(), page);
return new ResponseEntity<>(postService.getNewComments(post.getId(), page), HttpStatus.OK);
}
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@Operation(summary = "Get all the comments of an user")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found", content = {
Expand Down
2 changes: 2 additions & 0 deletions back/src/main/java/net/daw/alist/models/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ public void init() throws IOException, SQLException {
firstPost.addComment(firstComment);
Comment secondComment = new Comment(shanks, "Where is LeganesCF in this Top?!?", null);
thirdPost.addComment(secondComment);
Comment fourthComment = new Comment(manolo, "HALA MADRID!", null);
thirdPost.addComment(fourthComment);
Comment fifthComment = new Comment(admin, "Manolo is looking to get banned...", null);
thirdPost.addComment(fifthComment);
Comment sixthComment = new Comment(admin, "FORÇA BARÇA!!!", null);
thirdPost.addComment(sixthComment);
Comment seventhComment = new Comment(peepo, "GetafeCF forever...", null);
thirdPost.addComment(seventhComment);
Comment thirdComment = new Comment(peepo, "Lebron is the GOAT", null);
fourthPost.addComment(thirdComment);
Comment goatedComment = new Comment(peepo, "Hakiman is goated fr fr no cap top 1 ez", null);
Expand Down
14 changes: 11 additions & 3 deletions back/src/main/java/net/daw/alist/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import net.daw.alist.models.Post;
import net.daw.alist.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -26,6 +24,16 @@ public Page<Post> getPosts(int pageNumber) {
return postRepository.findAll(PageRequest.of(pageNumber, pageSize,Sort.by("votes").descending()));
}

public List<Comment> getNewComments(Long id, int pageNumber) {
List<Comment> comments = postRepository.getReferenceById(id).getComments();
int start = pageSize * pageNumber;
int end = start + pageSize;
if (end > comments.size()){
end = comments.size();
}
return comments.subList(start, end);
}

public Page<Post> getUserPosts(int pageNumber, int user_id) {
return postRepository.findPostsByUser(user_id, PageRequest.of(pageNumber, pageSize));
}
Expand Down
3 changes: 1 addition & 2 deletions front/src/app/post/pages/top/top.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ img {
.commentSection{
background-color: white;
border-radius: var(--bs-border-radius);
padding-top: 1rem;
padding-bottom: 1rem;

}

.post,
Expand Down
12 changes: 10 additions & 2 deletions front/src/app/post/pages/top/top.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<app-post [post]="post"></app-post>
<div class="commentSection mb-3">
<p class="commentTitle text-muted pb-3">Comments</p>
<p class="commentTitle text-muted p-3">Comments</p>
<!--TODO: fix forum-->
<form action="#" *ngIf="isLoggedIn">
<div class="writeComment p-3">
Expand All @@ -17,4 +17,12 @@
<div class="commentBox" *ngFor="let comment of comments">
<app-comment [comment]="comment"></app-comment>
</div>
</div>
<div class="d-flex justify-content-center flex-column p-3" *ngIf="!endOfComments">
<mat-progress-spinner class="m-auto" mode="indeterminate" color="white"
[style.display]="loading ? 'block' : 'none'"></mat-progress-spinner>
<a class="btn btn-primary btn-sm shadow-none m-auto" (click)="getComments()">Load more...</a>
</div>
<div class="d-flex justify-content-center p-3" *ngIf="endOfComments">
<p class="text-muted m-auto">No more comments :(</p>
</div>
</div>
20 changes: 17 additions & 3 deletions front/src/app/post/pages/top/top.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import { ActivatedRoute } from '@angular/router';
export class TopComponent implements OnInit {
isLoggedIn: boolean = false;
comments: Comment[] = [];
page: number = 0;
post!: Post;
postId!: number;
endOfComments: boolean = false;
loading: boolean = false;

constructor(
private postsService: PostsService,
Expand All @@ -31,8 +34,19 @@ export class TopComponent implements OnInit {
this.postsService
.getPostById(this.postId)
.subscribe(response => (this.post = response as Post));
this.commentsService
.getCommentsByPostId(this.postId)
.subscribe(response => (this.comments = response as Comment[]));
this.getComments();
}

getComments() {
this.loading = true;
if (!this.endOfComments) {
this.commentsService.getComments(this.postId, this.page).subscribe(
(fetchedComments: Comment[]) =>
(this.comments = [...this.comments, ...fetchedComments]),
error => (this.endOfComments = true)
);
this.page++;
}
this.loading = false;
}
}
11 changes: 10 additions & 1 deletion front/src/app/post/services/comments.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map } from 'rxjs';
import { Comment } from 'src/app/models/comment.model';

const BASE_URL = 'api/comments';

Expand All @@ -12,4 +14,11 @@ export class CommentsService {
getCommentsByPostId(postId: number) {
return this.http.get(BASE_URL + '/posts/' + postId);
}

getComments(id: number, page: number) {
const params = new HttpParams().set('id', id).set('page', page);
return this.http
.get<Comment[]>(BASE_URL + '/posts', { params })
.pipe(map(res => res));
}
}
1 change: 1 addition & 0 deletions front/src/app/post/services/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';


const BASE_URL = 'api/posts';

@Injectable({
Expand Down

0 comments on commit b783406

Please sign in to comment.