Skip to content

Commit

Permalink
Commit only allow user tagged to see activity
Browse files Browse the repository at this point in the history
  • Loading branch information
sayoungestguy committed Aug 29, 2024
1 parent bb90c69 commit cd6df27
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.teamsixnus.scaleup.repository;

import com.teamsixnus.scaleup.domain.Activity;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.*;
import org.springframework.stereotype.Repository;

Expand All @@ -9,4 +13,6 @@
*/
@SuppressWarnings("unused")
@Repository
public interface ActivityRepository extends JpaRepository<Activity, Long> {}
public interface ActivityRepository extends JpaRepository<Activity, Long> {
Page<Activity> findAllByCreatorProfileUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.teamsixnus.scaleup.service;

import com.teamsixnus.scaleup.domain.Activity;
import com.teamsixnus.scaleup.domain.User;
import com.teamsixnus.scaleup.repository.ActivityRepository;
import com.teamsixnus.scaleup.service.dto.ActivityDTO;
import com.teamsixnus.scaleup.service.mapper.ActivityMapper;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,10 +28,12 @@ public class ActivityService {
private final ActivityRepository activityRepository;

private final ActivityMapper activityMapper;
private final UserService userService;

public ActivityService(ActivityRepository activityRepository, ActivityMapper activityMapper) {
public ActivityService(ActivityRepository activityRepository, ActivityMapper activityMapper, UserService userService) {
this.activityRepository = activityRepository;
this.activityMapper = activityMapper;
this.userService = userService;
}

/**
Expand Down Expand Up @@ -101,6 +107,12 @@ public Optional<ActivityDTO> findOne(Long id) {
return activityRepository.findById(id).map(activityMapper::toDto);
}

@Transactional(readOnly = true)
public Page<ActivityDTO> findAllByCurrentUser(Pageable pageable) {
User currentUser = userService.getUserWithAuthorities().orElseThrow(() -> new UsernameNotFoundException("User not found"));
return activityRepository.findAllByCreatorProfileUserId(currentUser.getId(), pageable).map(activityMapper::toDto);
}

/**
* Delete the activity by id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public ResponseEntity<ActivityDTO> partialUpdateActivity(
@GetMapping("")
public ResponseEntity<List<ActivityDTO>> getAllActivities(@org.springdoc.core.annotations.ParameterObject Pageable pageable) {
log.debug("REST request to get a page of Activities");
Page<ActivityDTO> page = activityService.findAll(pageable);
Page<ActivityDTO> page = activityService.findAllByCurrentUser(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
Expand All @@ -162,6 +162,20 @@ public ResponseEntity<ActivityDTO> getActivity(@PathVariable("id") Long id) {
return ResponseUtil.wrapOrNotFound(activityDTO);
}

/**
* {@code GET /activities} : get all the activities.
*
* @param pageable the pagination information.
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of activities in body.
*/
// @GetMapping("")
// public ResponseEntity<List<ActivityDTO>> getAllActivitiesByCurrentUser(@org.springdoc.core.annotations.ParameterObject Pageable pageable) {
// log.debug("REST request to get a page of Activities");
// Page<ActivityDTO> page = activityService.findAllByCurrentUser(pageable);
// HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
// return ResponseEntity.ok().headers(headers).body(page.getContent());
// }

/**
* {@code DELETE /activities/:id} : delete the "id" activity.
*
Expand Down
64 changes: 32 additions & 32 deletions src/main/webapp/app/entities/activity/activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,21 @@ export const Activity = () => {
<th className="hand" onClick={sort('details')}>
Details <FontAwesomeIcon icon={getSortIconByFieldName('details')} />
</th>
<th className="hand" onClick={sort('createdBy')}>
Created By <FontAwesomeIcon icon={getSortIconByFieldName('createdBy')} />
</th>
<th className="hand" onClick={sort('createdDate')}>
Created Date <FontAwesomeIcon icon={getSortIconByFieldName('createdDate')} />
</th>
<th className="hand" onClick={sort('lastModifiedBy')}>
Last Modified By <FontAwesomeIcon icon={getSortIconByFieldName('lastModifiedBy')} />
</th>
<th className="hand" onClick={sort('lastModifiedDate')}>
Last Modified Date <FontAwesomeIcon icon={getSortIconByFieldName('lastModifiedDate')} />
</th>
<th>
Creator Profile <FontAwesomeIcon icon="sort" />
</th>
{/*<th className="hand" onClick={sort('createdBy')}>*/}

Check warning on line 144 in src/main/webapp/app/entities/activity/activity.tsx

View workflow job for this annotation

GitHub Actions / Frontend Test Stage

Expected space or tab after '/*' in comment
{/* Created By <FontAwesomeIcon icon={getSortIconByFieldName('createdBy')} />*/}
{/*</th>*/}

Check warning on line 146 in src/main/webapp/app/entities/activity/activity.tsx

View workflow job for this annotation

GitHub Actions / Frontend Test Stage

Expected space or tab after '/*' in comment
{/*<th className="hand" onClick={sort('createdDate')}>*/}

Check warning on line 147 in src/main/webapp/app/entities/activity/activity.tsx

View workflow job for this annotation

GitHub Actions / Frontend Test Stage

Expected space or tab after '/*' in comment
{/* Created Date <FontAwesomeIcon icon={getSortIconByFieldName('createdDate')} />*/}
{/*</th>*/}
{/*<th className="hand" onClick={sort('lastModifiedBy')}>*/}
{/* Last Modified By <FontAwesomeIcon icon={getSortIconByFieldName('lastModifiedBy')} />*/}
{/*</th>*/}
{/*<th className="hand" onClick={sort('lastModifiedDate')}>*/}
{/* Last Modified Date <FontAwesomeIcon icon={getSortIconByFieldName('lastModifiedDate')} />*/}
{/*</th>*/}
{/*<th>*/}
{/* Creator Profile <FontAwesomeIcon icon="sort" />*/}
{/*</th>*/}
<th>
Skill <FontAwesomeIcon icon="sort" />
</th>
Expand All @@ -176,23 +176,23 @@ export const Activity = () => {
<td>{activity.duration}</td>
<td>{activity.venue}</td>
<td>{activity.details}</td>
<td>{activity.createdBy}</td>
<td>
{activity.createdDate ? <TextFormat type="date" value={activity.createdDate} format={APP_DATE_FORMAT} /> : null}
</td>
<td>{activity.lastModifiedBy}</td>
<td>
{activity.lastModifiedDate ? (
<TextFormat type="date" value={activity.lastModifiedDate} format={APP_DATE_FORMAT} />
) : null}
</td>
<td>
{activity.creatorProfile ? (
<Link to={`/user-profile/${activity.creatorProfile.id}`}>{activity.creatorProfile.id}</Link>
) : (
''
)}
</td>
{/*<td>{activity.createdBy}</td>*/}
{/*<td>*/}
{/* {activity.createdDate ? <TextFormat type="date" value={activity.createdDate} format={APP_DATE_FORMAT} /> : null}*/}
{/*</td>*/}
{/*<td>{activity.lastModifiedBy}</td>*/}
{/*<td>*/}
{/* {activity.lastModifiedDate ? (*/}
{/* <TextFormat type="date" value={activity.lastModifiedDate} format={APP_DATE_FORMAT} />*/}
{/* ) : null}*/}
{/*</td>*/}
{/*<td>*/}
{/* {activity.creatorProfile ? (*/}
{/* <Link to={`/user-profile/${activity.creatorProfile.id}`}>{activity.creatorProfile.id}</Link>*/}
{/* ) : (*/}
{/* ''*/}
{/* )}*/}
{/*</td>*/}
<td>{activity.skill ? <Link to={`/skill/${activity.skill.id}`}>{activity.skill.id}</Link> : ''}</td>
<td className="text-end">
<div className="btn-group flex-btn-group-container">
Expand Down

0 comments on commit cd6df27

Please sign in to comment.