Changeset 0808ef2


Ignore:
Timestamp:
02/10/26 13:53:31 (5 months ago)
Author:
Filip Gavrilovski <filipgavrilovski28@…>
Branches:
main
Children:
1579b4f
Parents:
890e036
Message:

fix bug that prevented nonauthenticated users from searching and accessing user and album pages

Location:
finkwave/src/main/java/com/ukim/finki/develop/finkwave
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/controller/UserController.java

    r890e036 r0808ef2  
    3232        return ResponseEntity.ok(usersService.search(userType, searchTerm, limit));
    3333    }
     34
    3435}
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/AlbumService.java

    r890e036 r0808ef2  
    2626            throw new AlbumNotFoundException(id);
    2727        }
    28         Long currentUserId=authService.getCurrentUserID();
     28        Long currentUserId=authService.getCurrentUserIDOptional().orElse(null);
    2929
    3030        Album album=albumRepository.findById(id).orElseThrow(()->new AlbumNotFoundException(id));
     
    4444
    4545    public List<MusicalEntityDto> searchAlbums(String searchTerm){
    46         return albumRepository.searchAlbums(authService.getCurrentUserID(), searchTerm);
     46        return albumRepository.searchAlbums(authService.getCurrentUserIDOptional().orElse(null), searchTerm);
    4747    }
    4848
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/ArtistService.java

    r890e036 r0808ef2  
    99
    1010import java.util.List;
    11 import java.util.stream.Collectors;
    1211
    1312@Service
    1413@AllArgsConstructor
    1514public class ArtistService {
    16 
    1715    private final ArtistContributionRepository artistContributionRepository;
    18     private final AuthService authService;
    1916
    2017
    2118    @Transactional(readOnly=true)
    22     public List<ArtistContributionDto> getArtistContributions(Long artistId){
    23         Long currentUserId=authService.getCurrentUserID();
     19    public List<ArtistContributionDto> getArtistContributions(Long artistId, Long currentUserId){
    2420        return artistContributionRepository.findContributionsByArtistId(currentUserId,artistId);
    2521    }
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/AuthService.java

    r890e036 r0808ef2  
    55import java.nio.file.Path;
    66import java.nio.file.Paths;
     7import java.util.List;
     8import java.util.Optional;
    79import java.util.UUID;
    810
     
    123125    }
    124126
     127    public Optional<Long> getCurrentUserIDOptional(){
     128        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
     129        if (authentication == null || !authentication.isAuthenticated()) {
     130            return Optional.empty();
     131        }
     132        String username=authentication.getName();
     133        return userRepository.findByUsername(username).map(User::getId);
     134    }
    125135
    126136    private User createNonAdminUser(AuthRequestDto authRequestDto) throws IOException {
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/FollowService.java

    r890e036 r0808ef2  
    5555        Long id=nonAdminUser.getId();
    5656        List<Follow> followers = followRepository.findFollowersWithProfile(id);
    57         Long currentUserId=authService.getCurrentUserID();
     57        Long currentUserId=authService.getCurrentUserIDOptional().orElse(null);
    5858
    5959
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/NonAdminUserService.java

    r890e036 r0808ef2  
    4141    @Transactional(readOnly = true)
    4242    public NonAdminUserDto getNonAdminUserProfile(String username) {
    43 
    44         Long currentUserId= authService.getCurrentUserID();
    45 
     43        Long currentUserId = authService.getCurrentUserIDOptional().orElse(null);
    4644
    4745        Optional<Artist>artistOptional=artistRepository.findByUsername(username);
     
    6361
    6462    private FollowStatusDto getFollowingInfo(Long id,Long currentUserId){
     63
    6564        Long followers = followRepository.countByFolloweeId(id);
    6665        Long following = followRepository.countByFollowerId(id);
    67         boolean isFollowing = followRepository.isFollowing(currentUserId, id);
     66        boolean isFollowing = currentUserId != null && followRepository.isFollowing(currentUserId, id);
    6867        return new FollowStatusDto(isFollowing,followers,following);
    6968    }
     
    7271        Long artistId=artist.getId();
    7372
    74         List<ArtistContributionDto>artistContributionDtos=artistService.getArtistContributions(artistId);
     73        List<ArtistContributionDto>artistContributionDtos=artistService.getArtistContributions(artistId, currentUserId);
    7574        FollowStatusDto followStatusDto=getFollowingInfo(artistId,currentUserId);
    7675
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/PlaylistService.java

    r890e036 r0808ef2  
    3535
    3636    public PlaylistDto getPlaylist(Long id){
    37         Long currentUserId=authService.getCurrentUserID();
     37        Long currentUserId=authService.getCurrentUserIDOptional().orElse(null);
    3838        Playlist playlist = playlistRepository.findById(id).orElseThrow(()-> new PlaylistNotFoundException(id));
    3939        List<MusicalEntityDto>songsInPlaylist=songRepository.findSongsByPlaylistId(id,currentUserId);
  • finkwave/src/main/java/com/ukim/finki/develop/finkwave/service/SongService.java

    r890e036 r0808ef2  
    2727
    2828    public List<SongDto> searchSongs(String searchTerm){
    29         Long userId = authService.getCurrentUserID();
     29        Long userId = authService.getCurrentUserIDOptional().orElse(null);
    3030        return songRepository.searchSongs(userId, searchTerm);
    3131    }
     
    3636    }
    3737
    38     public MusicalEntityDto getSongById(Long songId){
    39         Long userId = null;
    40         try {
    41             userId = authService.getCurrentUserID();
    42         } catch (Exception ignored) {}
    43         return songRepository.getSongById(songId, userId);
    44     }
     38//    public MusicalEntityDto getSongById(Long songId){
     39//        Long userId = null;
     40//        try {
     41//            userId = authService.getCurrentUserID();
     42//        } catch (Exception ignored) {}
     43//        return songRepository.getSongById(songId, userId);
     44//    }
    4545
    4646    public SongDetailsDto getSongDetails(Long songId){
Note: See TracChangeset for help on using the changeset viewer.