From 6a3798cebd66387df411b0c17d205fb3c7e73817 Mon Sep 17 00:00:00 2001
From: "michael.breu" <michael.breu@uibk.ac.at>
Date: Wed, 14 Apr 2021 18:43:46 +0200
Subject: [PATCH] Erster Durchstich

---
 .../repository/UserWatchListRepository.java   | 12 ++++++
 .../service/UserWatchListService.java         | 15 +++++++
 .../web/rest/UserWatchListResource.java       | 20 +++++++++
 .../user-watch-list.service.ts                |  4 ++
 .../app/layouts/navbar/navbar.component.html  |  3 ++
 .../search-input/search-input.component.html  |  2 +
 src/main/webapp/app/search/search.module.ts   |  6 ++-
 .../app/watchlist/watchlist.component.html    |  6 +++
 .../app/watchlist/watchlist.component.scss    |  0
 .../app/watchlist/watchlist.component.ts      | 43 +++++++++++++++++++
 10 files changed, 109 insertions(+), 2 deletions(-)
 create mode 100644 src/main/webapp/app/watchlist/watchlist.component.html
 create mode 100644 src/main/webapp/app/watchlist/watchlist.component.scss
 create mode 100644 src/main/webapp/app/watchlist/watchlist.component.ts

diff --git a/src/main/java/at/ac/uibk/gitsearch/repository/UserWatchListRepository.java b/src/main/java/at/ac/uibk/gitsearch/repository/UserWatchListRepository.java
index b7455475c..7d14d8427 100644
--- a/src/main/java/at/ac/uibk/gitsearch/repository/UserWatchListRepository.java
+++ b/src/main/java/at/ac/uibk/gitsearch/repository/UserWatchListRepository.java
@@ -1,7 +1,10 @@
 package at.ac.uibk.gitsearch.repository;
 
+import at.ac.uibk.gitsearch.domain.User;
 import at.ac.uibk.gitsearch.domain.UserWatchList;
 
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.*;
 import org.springframework.stereotype.Repository;
 
@@ -16,4 +19,13 @@ public interface UserWatchListRepository extends JpaRepository<UserWatchList, Lo
 
     @Query("select userWatchList from UserWatchList userWatchList where userWatchList.userId.login = ?#{principal.username}")
     List<UserWatchList> findByUserIdIsCurrentUser();
+    
+	/**
+	 * Returns all watchlists of the current user.
+	 *
+	 * @param userId the id of the user
+	 * @return the list of watchlists
+	 */
+	List<UserWatchList> findByUserId(long UserId);
+
 }
diff --git a/src/main/java/at/ac/uibk/gitsearch/service/UserWatchListService.java b/src/main/java/at/ac/uibk/gitsearch/service/UserWatchListService.java
index 6528473ce..37a58ffce 100644
--- a/src/main/java/at/ac/uibk/gitsearch/service/UserWatchListService.java
+++ b/src/main/java/at/ac/uibk/gitsearch/service/UserWatchListService.java
@@ -13,7 +13,9 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 import static org.elasticsearch.index.query.QueryBuilders.*;
 
@@ -66,6 +68,19 @@ public class UserWatchListService {
             .map(userWatchListMapper::toDto);
     }
 
+    /**
+     * Get all the userWatchLists.
+     *
+     * @param pageable the pagination information.
+     * @return the list of entities.
+     */
+    @Transactional(readOnly = true)
+    public List<UserWatchListDTO> findByUserIdIsCurrentUser() {
+        log.debug("Request to get all UserWatchLists");
+        return userWatchListRepository.findByUserIdIsCurrentUser().stream()
+            .map(userWatchListMapper::toDto).collect(Collectors.toList());
+    }
+
 
     /**
      * Get one userWatchList by id.
diff --git a/src/main/java/at/ac/uibk/gitsearch/web/rest/UserWatchListResource.java b/src/main/java/at/ac/uibk/gitsearch/web/rest/UserWatchListResource.java
index 5dbb60cf6..5c7c252bc 100644
--- a/src/main/java/at/ac/uibk/gitsearch/web/rest/UserWatchListResource.java
+++ b/src/main/java/at/ac/uibk/gitsearch/web/rest/UserWatchListResource.java
@@ -18,6 +18,7 @@ import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
 import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
@@ -60,6 +61,7 @@ public class UserWatchListResource {
      * @throws URISyntaxException if the Location URI syntax is incorrect.
      */
     @PostMapping("/user-watch-lists")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<UserWatchListDTO> createUserWatchList(@Valid @RequestBody UserWatchListDTO userWatchListDTO) throws URISyntaxException {
         log.debug("REST request to save UserWatchList : {}", userWatchListDTO);
         if (userWatchListDTO.getId() != null) {
@@ -81,6 +83,7 @@ public class UserWatchListResource {
      * @throws URISyntaxException if the Location URI syntax is incorrect.
      */
     @PutMapping("/user-watch-lists")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<UserWatchListDTO> updateUserWatchList(@Valid @RequestBody UserWatchListDTO userWatchListDTO) throws URISyntaxException {
         log.debug("REST request to update UserWatchList : {}", userWatchListDTO);
         if (userWatchListDTO.getId() == null) {
@@ -100,6 +103,7 @@ public class UserWatchListResource {
      * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of userWatchLists in body.
      */
     @GetMapping("/user-watch-lists")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<List<UserWatchListDTO>> getAllUserWatchLists(UserWatchListCriteria criteria, Pageable pageable) {
         log.debug("REST request to get UserWatchLists by criteria: {}", criteria);
         Page<UserWatchListDTO> page = userWatchListQueryService.findByCriteria(criteria, pageable);
@@ -107,6 +111,18 @@ public class UserWatchListResource {
         return ResponseEntity.ok().headers(headers).body(page.getContent());
     }
 
+    /**
+     * {@code GET  /user-watch-lists} : get all the userWatchLists.
+     *
+     * @param pageable the pagination information.
+     * @param criteria the criteria which the requested entities should match.
+     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of userWatchLists in body.
+     */
+    @GetMapping("/my-user-watch-lists")
+    public ResponseEntity<List<UserWatchListDTO>> getUserWatchListsOfCurrentUser() {
+        List<UserWatchListDTO> page = userWatchListService.findByUserIdIsCurrentUser();
+        return ResponseEntity.ok().body(page);
+    }
     /**
      * {@code GET  /user-watch-lists/count} : count all the userWatchLists.
      *
@@ -114,6 +130,7 @@ public class UserWatchListResource {
      * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the count in body.
      */
     @GetMapping("/user-watch-lists/count")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<Long> countUserWatchLists(UserWatchListCriteria criteria) {
         log.debug("REST request to count UserWatchLists by criteria: {}", criteria);
         return ResponseEntity.ok().body(userWatchListQueryService.countByCriteria(criteria));
@@ -126,6 +143,7 @@ public class UserWatchListResource {
      * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the userWatchListDTO, or with status {@code 404 (Not Found)}.
      */
     @GetMapping("/user-watch-lists/{id}")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<UserWatchListDTO> getUserWatchList(@PathVariable Long id) {
         log.debug("REST request to get UserWatchList : {}", id);
         Optional<UserWatchListDTO> userWatchListDTO = userWatchListService.findOne(id);
@@ -139,6 +157,7 @@ public class UserWatchListResource {
      * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
      */
     @DeleteMapping("/user-watch-lists/{id}")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<Void> deleteUserWatchList(@PathVariable Long id) {
         log.debug("REST request to delete UserWatchList : {}", id);
         userWatchListService.delete(id);
@@ -154,6 +173,7 @@ public class UserWatchListResource {
      * @return the result of the search.
      */
     @GetMapping("/_search/user-watch-lists")
+    @PreAuthorize("hasAnyRole('ADMIN')")
     public ResponseEntity<List<UserWatchListDTO>> searchUserWatchLists(@RequestParam String query, Pageable pageable) {
         log.debug("REST request to search for a page of UserWatchLists for query {}", query);
         Page<UserWatchListDTO> page = userWatchListService.search(query, pageable);
diff --git a/src/main/webapp/app/entities/user-watch-list/user-watch-list.service.ts b/src/main/webapp/app/entities/user-watch-list/user-watch-list.service.ts
index 37f895fa4..5a6eed7a4 100644
--- a/src/main/webapp/app/entities/user-watch-list/user-watch-list.service.ts
+++ b/src/main/webapp/app/entities/user-watch-list/user-watch-list.service.ts
@@ -28,6 +28,10 @@ export class UserWatchListService {
     return this.http.get<IUserWatchList>(`${this.resourceUrl}/${id}`, { observe: 'response' });
   }
 
+    /** returns the watchlists of the current user */
+  findMyWatchlists(): Observable<EntityArrayResponseType> {
+    return this.http.get<IUserWatchList[]>(`${SERVER_API_URL}/api/my-user-watch-lists`, { observe: 'response' });
+  }
   query(req?: any): Observable<EntityArrayResponseType> {
     const options = createRequestOption(req);
     return this.http.get<IUserWatchList[]>(this.resourceUrl, { params: options, observe: 'response' });
diff --git a/src/main/webapp/app/layouts/navbar/navbar.component.html b/src/main/webapp/app/layouts/navbar/navbar.component.html
index ef280ff48..e2933ab16 100644
--- a/src/main/webapp/app/layouts/navbar/navbar.component.html
+++ b/src/main/webapp/app/layouts/navbar/navbar.component.html
@@ -288,6 +288,9 @@
                             <span class="spawn-submenu" jhiTranslate="global.menu.account.settingsDescription"
                                   *ngSwitchCase="true">Manage your user settings</span>
 
+                            <span class="spawn-submenu"
+                                  *ngSwitchCase="true">My Watchlists</span>
+
                             <a class="dropdown-item" routerLink="account/password" routerLinkActive="active"
                                *ngSwitchCase="true" (click)="collapseNavbar()">
                                 <fa-icon icon="lock" [fixedWidth]="true"></fa-icon>
diff --git a/src/main/webapp/app/search/search-input/search-input.component.html b/src/main/webapp/app/search/search-input/search-input.component.html
index bb52cb6b5..3dcc27ec2 100644
--- a/src/main/webapp/app/search/search-input/search-input.component.html
+++ b/src/main/webapp/app/search/search-input/search-input.component.html
@@ -73,6 +73,8 @@
             </form>
         </div>
     </ng-container>
+    <jhi-watchlist>
+    </jhi-watchlist>
 </div>
 
 
diff --git a/src/main/webapp/app/search/search.module.ts b/src/main/webapp/app/search/search.module.ts
index d3bd28c6d..cd80511ed 100644
--- a/src/main/webapp/app/search/search.module.ts
+++ b/src/main/webapp/app/search/search.module.ts
@@ -8,11 +8,13 @@ import { ExerciseModule } from 'app/exercise/exercise.module';
 
 import { SearchComponent } from './search.component';
 import { SearchInputComponent } from './search-input/search-input.component';
+import { WatchlistComponent } from '../watchlist/watchlist.component';
 
 
 @NgModule({
-  imports: [RouterModule.forChild([SEARCH_ROUTE]), GitSearchV2SharedModule, QueryParamModule, InfiniteScrollModule, ExerciseModule],
-  declarations: [SearchComponent, SearchInputComponent],
+  imports: [RouterModule.forChild([SEARCH_ROUTE]), GitSearchV2SharedModule, QueryParamModule, InfiniteScrollModule,
+     ExerciseModule],
+  declarations: [SearchComponent, SearchInputComponent, WatchlistComponent],
   exports: [SearchComponent],
 })
 export class SearchModule {}
diff --git a/src/main/webapp/app/watchlist/watchlist.component.html b/src/main/webapp/app/watchlist/watchlist.component.html
new file mode 100644
index 000000000..50ebf64a0
--- /dev/null
+++ b/src/main/webapp/app/watchlist/watchlist.component.html
@@ -0,0 +1,6 @@
+<div >
+    <div *ngFor="let watchList of myWatchLists">{{watchList.name}} 
+    </div>
+</div>
+
+
diff --git a/src/main/webapp/app/watchlist/watchlist.component.scss b/src/main/webapp/app/watchlist/watchlist.component.scss
new file mode 100644
index 000000000..e69de29bb
diff --git a/src/main/webapp/app/watchlist/watchlist.component.ts b/src/main/webapp/app/watchlist/watchlist.component.ts
new file mode 100644
index 000000000..75e080180
--- /dev/null
+++ b/src/main/webapp/app/watchlist/watchlist.component.ts
@@ -0,0 +1,43 @@
+import { Component, OnDestroy, OnInit } from '@angular/core';
+import { HttpResponse } from '@angular/common/http';
+import { UserWatchListService } from '../entities/user-watch-list/user-watch-list.service';
+import { IUserWatchList } from 'app/shared/model/user-watch-list.model';
+
+
+@Component({
+    selector: 'jhi-watchlist',
+    templateUrl: './watchlist.component.html',
+    styleUrls: ['./watchlist.component.scss'],
+})
+export class WatchlistComponent implements OnInit, OnDestroy {
+
+    myWatchLists: IUserWatchList[] | undefined;
+
+    constructor(
+        protected watchListService: UserWatchListService,
+    ) {
+    }
+
+
+    ngOnInit(): void {
+        if (!this.myWatchLists) {
+            this.watchListService.findMyWatchlists().subscribe((data: HttpResponse<IUserWatchList[]>) => {
+                if (data.body) {
+                    this.myWatchLists = data.body;
+                    /*
+                    this.myWatchLists = [];
+                    for (let i = 0; i < data.body.length; i++) {
+                        this.myWatchLists.push(data.body[i]);
+                    }
+                    */
+                }
+
+            })
+
+        }
+    }
+
+    ngOnDestroy(): void {
+    }
+
+}
-- 
GitLab