Skip to content

Commit

Permalink
Merge pull request #261 from kagemomiji/issue212-replace-rating-dao
Browse files Browse the repository at this point in the history
#212 implement UserRatingRepository
  • Loading branch information
kagemomiji authored Aug 8, 2023
2 parents 4654e5b + 81fc6c5 commit 7508220
Show file tree
Hide file tree
Showing 7 changed files with 656 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
This file is part of Airsonic.
Airsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Airsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2023 (C) Y.Tory
*/

package org.airsonic.player.domain.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

@Entity
@Table(name = "user_rating")
@IdClass(UserRatingKey.class)
public class UserRating {

@Id
private String username;

@Id
@Column(name = "media_file_id")
private int mediaFileId;

@Min(1)
@Max(5)
private Integer rating;

public UserRating() {
}

public UserRating(String username, int mediaFileId, int rating) {
this.username = username;
this.mediaFileId = mediaFileId;
this.rating = rating;
}

public String getUsername() {
return username;
}

public int getMediaFileId() {
return mediaFileId;
}

public Integer getRating() {
return rating;
}

public void setUsername(String username) {
this.username = username;
}

public void setMediaFileId(int mediaFileId) {
this.mediaFileId = mediaFileId;
}

public void setRating(int rating) {
this.rating = rating;
}

@Override
public String toString() {
return "UserRating{" +
"username='" + username + '\'' +
", media_file_id=" + mediaFileId +
", rating=" + rating +
'}';
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
This file is part of Airsonic.
Airsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Airsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2023 (C) Y.Tory
*/

package org.airsonic.player.domain.entity;

import java.io.Serializable;
import java.util.Objects;

public class UserRatingKey implements Serializable {

private String username;

private int mediaFileId;

public UserRatingKey() {
}

public UserRatingKey(String username, int mediaFileId) {
this.username = username;
this.mediaFileId = mediaFileId;
}

public String getUsername() {
return username;
}

public int getMediaFileId() {
return mediaFileId;
}

public void setUsername(String username) {
this.username = username;
}

public void setMediaFileId(int mediaFileId) {
this.mediaFileId = mediaFileId;
}

@Override
public String toString() {
return "UserRatingKey{" +
"username='" + username + '\'' +
", mediaFileId=" + mediaFileId +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UserRatingKey)) return false;

UserRatingKey that = (UserRatingKey) o;

if (mediaFileId != that.mediaFileId) return false;
if (!username.equals(that.username)) return false;

return true;
}

@Override
public int hashCode() {
return Objects.hash(username, mediaFileId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
This file is part of Airsonic.
Airsonic is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Airsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2023 (C) Y.Tory
*/

package org.airsonic.player.repository;

import org.airsonic.player.domain.entity.UserRating;
import org.airsonic.player.domain.entity.UserRatingKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;

import java.util.Optional;

@Repository
public interface UserRatingRepository extends JpaRepository<UserRating, UserRatingKey> {

public Optional<UserRating> findOptByUsernameAndMediaFileId(String username, int mediaFileId);

@Query("SELECT AVG(u.rating) FROM UserRating u WHERE u.mediaFileId = :mediaFileId")
public Double getAverageRatingByMediaFileId(@Param("mediaFileId") int mediaFileId);

@Transactional
public void deleteByUsernameAndMediaFileId(String username, int mediaFileId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
You should have received a copy of the GNU General Public License
along with Airsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2023 (C) Y.Tory
Copyright 2016 (C) Airsonic Authors
Based upon Subsonic, Copyright 2009 (C) Sindre Mehus
*/
Expand All @@ -22,8 +23,13 @@
import org.airsonic.player.dao.RatingDao;
import org.airsonic.player.domain.MediaFile;
import org.airsonic.player.domain.MusicFolder;
import org.airsonic.player.domain.entity.UserRating;
import org.airsonic.player.repository.UserRatingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -36,12 +42,16 @@
@Service
public class RatingService {

private static final Logger LOG = LoggerFactory.getLogger(RatingService.class);

@Autowired
private RatingDao ratingDao;
@Autowired
private SecurityService securityService;
@Autowired
private MediaFileService mediaFileService;
@Autowired
private UserRatingRepository userRatingRepository;

/**
* Returns the highest rated albums.
Expand All @@ -66,8 +76,21 @@ public List<MediaFile> getHighestRatedAlbums(int offset, int count, List<MusicFo
* @param mediaFile The music file.
* @param rating The rating between 1 and 5, or <code>null</code> to remove the rating.
*/
@Transactional
public void setRatingForUser(String username, MediaFile mediaFile, Integer rating) {
ratingDao.setRatingForUser(username, mediaFile, rating);
if (username == null || mediaFile == null) {
return;
}
if (rating == null) {
userRatingRepository.deleteByUsernameAndMediaFileId(username, mediaFile.getId());
} else {
UserRating userRating = new UserRating(username, mediaFile.getId(), rating);
try {
userRatingRepository.save(userRating);
} catch (Exception e) {
LOG.error("Failed to save rating for user {} and media file {}", username, mediaFile.getId(), e);
}
}
}

/**
Expand All @@ -76,8 +99,12 @@ public void setRatingForUser(String username, MediaFile mediaFile, Integer ratin
* @param mediaFile The music file.
* @return The average rating, or <code>null</code> if no ratings are set.
*/
@Transactional
public Double getAverageRating(MediaFile mediaFile) {
return ratingDao.getAverageRating(mediaFile);
if (mediaFile == null) {
return null;
}
return userRatingRepository.getAverageRatingByMediaFileId(mediaFile.getId());
}

/**
Expand All @@ -87,23 +114,16 @@ public Double getAverageRating(MediaFile mediaFile) {
* @param mediaFile The music file.
* @return The rating, or <code>null</code> if no rating is set.
*/
@Transactional
public Integer getRatingForUser(String username, MediaFile mediaFile) {
return ratingDao.getRatingForUser(username, mediaFile);
if (username == null || mediaFile == null) {
return null;
}
return userRatingRepository.findOptByUsernameAndMediaFileId(username, mediaFile.getId()).map(UserRating::getRating).orElse(null);
}

public int getRatedAlbumCount(String username, List<MusicFolder> musicFolders) {
return ratingDao.getRatedAlbumCount(username, musicFolders);
}

public void setRatingDao(RatingDao ratingDao) {
this.ratingDao = ratingDao;
}

public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}

public void setMediaFileService(MediaFileService mediaFileService) {
this.mediaFileService = mediaFileService;
}
}
Loading

0 comments on commit 7508220

Please sign in to comment.