[Spring][쇼핑몰 프로젝트][50] 상품 테이블 평균 평점 반영
https://kimvampa.tistory.com/301?category=771727
This commit is contained in:
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import com.vam.model.Criteria;
|
||||
import com.vam.model.ReplyDTO;
|
||||
import com.vam.model.UpdateReplyDTO;
|
||||
|
||||
public interface ReplyMapper {
|
||||
|
||||
@@ -28,4 +29,10 @@ public interface ReplyMapper {
|
||||
/* 댓글 삭제 */
|
||||
public int deleteReply(int replyId);
|
||||
|
||||
/* 평점 평균 구하기 */
|
||||
public Double getRatingAverage(int bookId);
|
||||
|
||||
/* 평점 평균 반영하기 */
|
||||
public int updateRating(UpdateReplyDTO dto);
|
||||
|
||||
}
|
||||
|
||||
32
VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java
Normal file
32
VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.vam.model;
|
||||
|
||||
public class UpdateReplyDTO {
|
||||
|
||||
private int bookId;
|
||||
|
||||
private double ratingAvg;
|
||||
|
||||
public int getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(int bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public double getRatingAvg() {
|
||||
return ratingAvg;
|
||||
}
|
||||
|
||||
public void setRatingAvg(double ratingAvg) {
|
||||
this.ratingAvg = ratingAvg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UpdateReplyDTO [bookId=" + bookId + ", ratingAvg=" + ratingAvg + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.vam.model.Criteria;
|
||||
import com.vam.model.PageDTO;
|
||||
import com.vam.model.ReplyDTO;
|
||||
import com.vam.model.ReplyPageDTO;
|
||||
import com.vam.model.UpdateReplyDTO;
|
||||
|
||||
@Service
|
||||
public class ReplyServiceImpl implements ReplyService{
|
||||
@@ -21,6 +22,8 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.enrollReply(dto);
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -54,6 +57,8 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.updateReply(dto);
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -68,8 +73,28 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.deleteReply(dto.getReplyId());
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setRating(int bookId) {
|
||||
|
||||
Double ratingAvg = replyMapper.getRatingAverage(bookId);
|
||||
|
||||
if(ratingAvg == null) {
|
||||
ratingAvg = 0.0;
|
||||
}
|
||||
|
||||
ratingAvg = (double) (Math.round(ratingAvg*10));
|
||||
ratingAvg = ratingAvg / 10;
|
||||
|
||||
UpdateReplyDTO urd = new UpdateReplyDTO();
|
||||
urd.setBookId(bookId);
|
||||
urd.setRatingAvg(ratingAvg);
|
||||
|
||||
replyMapper.updateRating(urd);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,7 +63,25 @@
|
||||
DELETE FROM vam_reply
|
||||
WHERE replyId = #{replyId}
|
||||
|
||||
</delete>
|
||||
</delete>
|
||||
|
||||
<select id="getRatingAverage" resultType="double">
|
||||
|
||||
select avg(rating)
|
||||
from vam_reply
|
||||
where bookId = #{bookId}
|
||||
|
||||
</select>
|
||||
|
||||
<update id="updateRating">
|
||||
|
||||
update vam_book
|
||||
set ratingAvg = #{ratingAvg}
|
||||
where bookId = #{bookId}
|
||||
|
||||
</update>
|
||||
|
||||
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -63,7 +63,25 @@
|
||||
DELETE FROM vam_reply
|
||||
WHERE replyId = #{replyId}
|
||||
|
||||
</delete>
|
||||
</delete>
|
||||
|
||||
<select id="getRatingAverage" resultType="double">
|
||||
|
||||
select avg(rating)
|
||||
from vam_reply
|
||||
where bookId = #{bookId}
|
||||
|
||||
</select>
|
||||
|
||||
<update id="updateRating">
|
||||
|
||||
update vam_book
|
||||
set ratingAvg = #{ratingAvg}
|
||||
where bookId = #{bookId}
|
||||
|
||||
</update>
|
||||
|
||||
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -1,5 +1,5 @@
|
||||
#Generated by Maven Integration for Eclipse
|
||||
#Sun Jan 09 17:57:01 KST 2022
|
||||
#Thu Feb 03 02:45:21 KST 2022
|
||||
m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa
|
||||
m2e.projectName=VamPa
|
||||
groupId=com.vam
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.util.List;
|
||||
|
||||
import com.vam.model.Criteria;
|
||||
import com.vam.model.ReplyDTO;
|
||||
import com.vam.model.UpdateReplyDTO;
|
||||
|
||||
public interface ReplyMapper {
|
||||
|
||||
@@ -28,4 +29,10 @@ public interface ReplyMapper {
|
||||
/* 댓글 삭제 */
|
||||
public int deleteReply(int replyId);
|
||||
|
||||
/* 평점 평균 구하기 */
|
||||
public Double getRatingAverage(int bookId);
|
||||
|
||||
/* 평점 평균 반영하기 */
|
||||
public int updateRating(UpdateReplyDTO dto);
|
||||
|
||||
}
|
||||
|
||||
30
VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java
Normal file
30
VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.vam.model;
|
||||
|
||||
public class UpdateReplyDTO {
|
||||
|
||||
private int bookId;
|
||||
|
||||
private double ratingAvg;
|
||||
|
||||
public int getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(int bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public double getRatingAvg() {
|
||||
return ratingAvg;
|
||||
}
|
||||
|
||||
public void setRatingAvg(double ratingAvg) {
|
||||
this.ratingAvg = ratingAvg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UpdateReplyDTO [bookId=" + bookId + ", ratingAvg=" + ratingAvg + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.vam.model.Criteria;
|
||||
import com.vam.model.PageDTO;
|
||||
import com.vam.model.ReplyDTO;
|
||||
import com.vam.model.ReplyPageDTO;
|
||||
import com.vam.model.UpdateReplyDTO;
|
||||
|
||||
@Service
|
||||
public class ReplyServiceImpl implements ReplyService{
|
||||
@@ -21,6 +22,8 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.enrollReply(dto);
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -53,6 +56,8 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.updateReply(dto);
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -67,7 +72,28 @@ public class ReplyServiceImpl implements ReplyService{
|
||||
|
||||
int result = replyMapper.deleteReply(dto.getReplyId());
|
||||
|
||||
setRating(dto.getBookId());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setRating(int bookId) {
|
||||
|
||||
Double ratingAvg = replyMapper.getRatingAverage(bookId);
|
||||
|
||||
if(ratingAvg == null) {
|
||||
ratingAvg = 0.0;
|
||||
}
|
||||
|
||||
ratingAvg = (double) (Math.round(ratingAvg*10));
|
||||
ratingAvg = ratingAvg / 10;
|
||||
|
||||
UpdateReplyDTO urd = new UpdateReplyDTO();
|
||||
urd.setBookId(bookId);
|
||||
urd.setRatingAvg(ratingAvg);
|
||||
|
||||
replyMapper.updateRating(urd);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -60,5 +60,21 @@
|
||||
WHERE replyId = #{replyId}
|
||||
|
||||
</delete>
|
||||
|
||||
<select id="getRatingAverage" resultType="double">
|
||||
|
||||
select avg(rating)
|
||||
from vam_reply
|
||||
where bookId = #{bookId}
|
||||
|
||||
</select>
|
||||
|
||||
<update id="updateRating">
|
||||
|
||||
update vam_book
|
||||
set ratingAvg = #{ratingAvg}
|
||||
where bookId = #{bookId}
|
||||
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@@ -60,5 +60,21 @@
|
||||
WHERE replyId = #{replyId}
|
||||
|
||||
</delete>
|
||||
|
||||
<select id="getRatingAverage" resultType="double">
|
||||
|
||||
select avg(rating)
|
||||
from vam_reply
|
||||
where bookId = #{bookId}
|
||||
|
||||
</select>
|
||||
|
||||
<update id="updateRating">
|
||||
|
||||
update vam_book
|
||||
set ratingAvg = #{ratingAvg}
|
||||
where bookId = #{bookId}
|
||||
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@@ -1,5 +1,5 @@
|
||||
#Generated by Maven Integration for Eclipse
|
||||
#Sun Jan 09 17:57:01 KST 2022
|
||||
#Thu Feb 03 02:45:21 KST 2022
|
||||
m2e.projectLocation=C\:\\Users\\sjinj\\git\\Blog_Project2\\VamPa_MySQL
|
||||
m2e.projectName=VamPa_MySQL
|
||||
groupId=com.vam
|
||||
|
||||
Reference in New Issue
Block a user