forked from airsonic-advanced/airsonic-advanced
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from kagemomiji/issue212-replace-rating-dao
#212 implement UserRatingRepository
- Loading branch information
Showing
7 changed files
with
656 additions
and
14 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
airsonic-main/src/main/java/org/airsonic/player/domain/entity/UserRating.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
|
||
|
||
} |
81 changes: 81 additions & 0 deletions
81
airsonic-main/src/main/java/org/airsonic/player/domain/entity/UserRatingKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
airsonic-main/src/main/java/org/airsonic/player/repository/UserRatingRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.