Skip to content

Commit

Permalink
feat: Search bar full functional merged with the main
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr4ll committed May 6, 2023
1 parent b267d06 commit 5922745
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class PostRestController {
@ApiResponse(responseCode = "400", description = "Bad formatting", content = @Content)
})
@PostMapping("/")
public ResponseEntity<Post> createPost(Authentication auth, @RequestBody Data content) throws SQLException, IOException {
public ResponseEntity<Post> createPost(Authentication auth, @RequestBody Data content)
throws SQLException, IOException {
if (content.getTitle() == null || content.getTopicStrings() == null || content.getItems() == null)
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

Expand All @@ -88,6 +89,13 @@ public ResponseEntity<Post> createPost(Authentication auth, @RequestBody Data co
postService.save(post);
return new ResponseEntity<>(post, HttpStatus.CREATED);
}

@GetMapping("/names/{prefix}")
public ResponseEntity<List<Post>> getUserByPrefix(@PathVariable String prefix){
List<Post> firstElementsList = postService.findPostPrefix(prefix).stream().limit(5).collect(Collectors.toList());

return new ResponseEntity<>(firstElementsList, HttpStatus.OK);
}

@Operation(summary = "Get specific post")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.daw.alist.models.Post;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -16,4 +17,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
@Query(value="select * from post where post.author_id=?1 order by post.date desc", nativeQuery=true)
Page<Post> findPostsByUser(int user_id, Pageable page);

List<Post> findByTitleStartingWith(String prefix);

}
19 changes: 11 additions & 8 deletions back/src/main/java/net/daw/alist/services/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ public long count() {
return postRepository.count();
}

public Optional<Comment> getCommentByID(Post post, long commentId) {
List<Comment> comments = post.getComments();
for (Comment comment : comments) {
if (comment.getId() == commentId) {
return Optional.of(comment);
}
public Optional<Comment> getCommentByID(Post post, long commentId) {
List<Comment> comments = post.getComments();
for (Comment comment : comments) {
if (comment.getId() == commentId) {
return Optional.of(comment);
}
}
return Optional.empty();
}
return Optional.empty();
}

public List<Post> findPostPrefix(String prefix){return postRepository.findByTitleStartingWith(prefix);}

}
54 changes: 30 additions & 24 deletions front/src/app/shared/components/search/search.component.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<div class="body">
<!-- TODO: hideContainer() -->
<div class="searchWrapper bg-white" onmouseleave="hideContainer()">
<div class="searchWrapper bg-white" (mouseleave)="hideContainer()">
<div class="d-flex w-100" role="search">
<!-- TODO: showContainer() -->
<input class="searchBar form-control border-0 shadow-none w-100" id="searchBar" type="search" placeholder="Search"
aria-label="Search" onfocus="showContainer()" onmouseover="showContainer()" data-search>
<input class="searchBar form-control border-0 shadow-none w-100" #userPrefix id="searchBar"
(change)="searcher(userPrefix.value)" type="search" placeholder="Search" aria-label="Search"
(focus)="showContainer()" (mouseover)="showContainer()" data-search>
<div class="d-flex justify-content-center">
<i class="searchIcon fa-solid fa-magnifying-glass"></i>
</div>
Expand All @@ -13,32 +14,37 @@
<div class="separator text-center">Users</div>
<!-- TODO: {{#searchSuggestedUsers}} -->
<!-- TODO: username -->
<a class="card w-100 text-decoration-none" [routerLink]="['/user/username']">
<div class="cardLink text-left d-flex justify-content-center ms-0 me-auto">
<!-- TODO: imagePath -->
<img src="imagePath" alt="profilePic" class="profile-pic-search rounded-circle me-2">
<!-- TODO: username -->
<div class="usernameSug header">username</div>
<div class="body"></div>
</div>
</a>
<div *ngFor="let user of userResults">
<a class="card w-100 text-decoration-none" href="/user/{{user.username}}">
<div class="cardLink text-left d-flex justify-content-center ms-0 me-auto">
<!-- TODO: imagePath -->
<img [src]="fetchImage(user.id)" alt="Profile picture" width="35" height="35"
class="profileImg rounded-circle flex-shrink-0">
<!-- TODO: username -->
<div class="usernameSug header m-auto">{{user?.username}}</div>
<div class="body"></div>
</div>
</a>
</div>
<!-- TODO: {{/searchSuggestedUsers}} -->
<div class="separator text-center">Posts</div>
<!-- TODO: {{#searchSuggestedPosts}} -->
<!-- TODO: id -->
<a class="card w-100 text-decoration-none" [routerLink]="['/top-list/id']">
<div class="cardLink text-left d-flex flex-column justify-content-center ms-0 me-auto">
<div class="d-flex flex-row">
<!-- TODO: author.imagePath -->
<img src="author.imagePath" alt="profilePic" class="profile-pic-search rounded-circle me-2">
<!-- TODO: author.username -->
<div class="usernameSug header">author.username</div>
<div *ngFor="let post of postResults">
<a class="card w-100 text-decoration-none" href="/top-list/{{post.id}}">
<div class="cardLink text-left d-flex flex-column justify-content-center ms-0 me-auto">
<div class="d-flex flex-row">
<!-- TODO: author.imagePath -->
<img [src]="fetchImage(post.authorId)" alt="profilePic" width="35" height="35"
class="profileImg rounded-circle flex-shrink-0">
<!-- TODO: author.username -->
<div class="usernameSug header">{{post.authorName}}</div>
</div>
<div class="d-flex flex-row "> {{post.title}}</div>
</div>
<!-- TODO: title -->
<div class="body">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp &#9900; title</div>
</div>
</a>
</a>
</div>
<!-- TODO: {{/searchSuggestedPosts}} -->
</div>
</div>
</div>
</div>
63 changes: 62 additions & 1 deletion front/src/app/shared/components/search/search.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@
import { Post } from 'src/app/models/post.model';
import { SearchService } from './search.service';
import { Component } from '@angular/core';
import { PostItem } from 'src/app/models/postItem.model';
import { User } from 'src/app/models/user.model';
import { PostsService } from 'src/app/post/services/posts.service';


@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css'],
})
export class SearchComponent {}
export class SearchComponent {
userResults: User[] = []
postResults: Post[] = []

constructor(private searchService: SearchService, private postsService: PostsService) { }

getUser(username: String) {
this.searchService.getUser(username).subscribe(
Response => this.userResults = Response as User[]
)
}

getPost(name: String) {
this.searchService.getPost(name).subscribe(
Response => this.postResults = Response as Post[]
)
}

searcher(name: String) {
this.getUser(name)
this.getPost(name)

}




hideContainer() {
//[0]: desktop div [1]: mobile div
//hide suggestion box
(document.getElementsByClassName("autoCompleteBox")[0] as HTMLElement).style.visibility = "hidden";
(document.getElementsByClassName("autoCompleteBox")[1] as HTMLElement).style.visibility = "hidden";
//add border bottom radius
(document.getElementsByClassName("searchBar")[0] as HTMLElement).style.borderBottomLeftRadius = "10px";
(document.getElementsByClassName("searchBar")[1] as HTMLElement).style.borderBottomLeftRadius = "10px";
(document.getElementsByClassName("searchWrapper")[0] as HTMLElement).style.borderBottomRightRadius = "10px";
(document.getElementsByClassName("searchWrapper")[1] as HTMLElement).style.borderBottomRightRadius = "10px";
}

showContainer() {
//[0]: desktop div [1]: mobile div
//reveal suggestion box
(document.getElementsByClassName("autoCompleteBox")[0] as HTMLElement).style.visibility = "visible";
(document.getElementsByClassName("autoCompleteBox")[1] as HTMLElement).style.visibility = "visible";

//remove border bottom radius
(document.getElementsByClassName("searchBar")[0] as HTMLElement).style.borderBottomLeftRadius = "0px";
(document.getElementsByClassName("searchBar")[1] as HTMLElement).style.borderBottomLeftRadius = "0px";
(document.getElementsByClassName("searchWrapper")[0] as HTMLElement).style.borderBottomRightRadius = "0px";
(document.getElementsByClassName("searchWrapper")[1] as HTMLElement).style.borderBottomRightRadius = "0px";
}

fetchImage(src: number | undefined) {
return this.postsService.downloadImage(src as number);
}
}
16 changes: 16 additions & 0 deletions front/src/app/shared/components/search/search.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* tslint:disable:no-unused-variable */

import { TestBed, async, inject } from '@angular/core/testing';
import { SearchService } from './search.service';

describe('Service: Search', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SearchService]
});
});

it('should ...', inject([SearchService], (service: SearchService) => {
expect(service).toBeTruthy();
}));
});
26 changes: 26 additions & 0 deletions front/src/app/shared/components/search/search.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { User } from 'src/app/models/user.model';
import { HttpClient } from '@angular/common/http';
import { Post } from 'src/app/models/post.model';


@Injectable({
providedIn: 'root'
})
export class SearchService {


constructor(private httpClient: HttpClient) { }

getUser(username: String): Observable<User[]> {
return this.httpClient.get<User[]>("api/users/names/" + username);
}

getPost(username: String): Observable<Post[]> {
return this.httpClient.get<Post[]>("api/posts/names/" + username);
}



}

0 comments on commit 5922745

Please sign in to comment.