[Spring][쇼핑몰 프로젝트][50] 상품 테이블 평균 평점 반영

https://kimvampa.tistory.com/301?category=771727
This commit is contained in:
SeoJin Kim
2022-02-03 03:38:32 +09:00
parent fada52006a
commit 536c4fd13f
12 changed files with 199 additions and 4 deletions

View File

@@ -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);
}

View 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 + "]";
}
}

View File

@@ -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);
}
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -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

View File

@@ -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);
}

View 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 + "]";
}
}

View File

@@ -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);
}
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -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