From 536c4fd13f1f9fd1c45106f2c2016b8b17b451ab Mon Sep 17 00:00:00 2001 From: SeoJin Kim Date: Thu, 3 Feb 2022 03:38:32 +0900 Subject: [PATCH] =?UTF-8?q?[Spring][=EC=87=BC=ED=95=91=EB=AA=B0=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8][50]=20=EC=83=81=ED=92=88?= =?UTF-8?q?=20=ED=85=8C=EC=9D=B4=EB=B8=94=20=ED=8F=89=EA=B7=A0=20=ED=8F=89?= =?UTF-8?q?=EC=A0=90=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://kimvampa.tistory.com/301?category=771727 --- .../main/java/com/vam/mapper/ReplyMapper.java | 7 ++++ .../java/com/vam/model/UpdateReplyDTO.java | 32 +++++++++++++++++++ .../com/vam/service/ReplyServiceImpl.java | 25 +++++++++++++++ .../resources/com/vam/mapper/ReplyMapper.xml | 20 +++++++++++- .../classes/com/vam/mapper/ReplyMapper.xml | 20 +++++++++++- .../maven/com.vam/controller/pom.properties | 2 +- .../main/java/com/vam/mapper/ReplyMapper.java | 7 ++++ .../java/com/vam/model/UpdateReplyDTO.java | 30 +++++++++++++++++ .../com/vam/service/ReplyServiceImpl.java | 26 +++++++++++++++ .../resources/com/vam/mapper/ReplyMapper.xml | 16 ++++++++++ .../classes/com/vam/mapper/ReplyMapper.xml | 16 ++++++++++ .../maven/com.vam/controller/pom.properties | 2 +- 12 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java create mode 100644 VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java diff --git a/VamPa/src/main/java/com/vam/mapper/ReplyMapper.java b/VamPa/src/main/java/com/vam/mapper/ReplyMapper.java index 4c5401f..84d8509 100644 --- a/VamPa/src/main/java/com/vam/mapper/ReplyMapper.java +++ b/VamPa/src/main/java/com/vam/mapper/ReplyMapper.java @@ -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); + } diff --git a/VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java b/VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java new file mode 100644 index 0000000..1587fc6 --- /dev/null +++ b/VamPa/src/main/java/com/vam/model/UpdateReplyDTO.java @@ -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 + "]"; + } + + + +} diff --git a/VamPa/src/main/java/com/vam/service/ReplyServiceImpl.java b/VamPa/src/main/java/com/vam/service/ReplyServiceImpl.java index d71895c..3f4a01d 100644 --- a/VamPa/src/main/java/com/vam/service/ReplyServiceImpl.java +++ b/VamPa/src/main/java/com/vam/service/ReplyServiceImpl.java @@ -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); + + } } diff --git a/VamPa/src/main/resources/com/vam/mapper/ReplyMapper.xml b/VamPa/src/main/resources/com/vam/mapper/ReplyMapper.xml index 80b497e..27b2f7b 100644 --- a/VamPa/src/main/resources/com/vam/mapper/ReplyMapper.xml +++ b/VamPa/src/main/resources/com/vam/mapper/ReplyMapper.xml @@ -63,7 +63,25 @@ DELETE FROM vam_reply WHERE replyId = #{replyId} - + + + + + + + update vam_book + set ratingAvg = #{ratingAvg} + where bookId = #{bookId} + + + + \ No newline at end of file diff --git a/VamPa/target/classes/com/vam/mapper/ReplyMapper.xml b/VamPa/target/classes/com/vam/mapper/ReplyMapper.xml index 80b497e..27b2f7b 100644 --- a/VamPa/target/classes/com/vam/mapper/ReplyMapper.xml +++ b/VamPa/target/classes/com/vam/mapper/ReplyMapper.xml @@ -63,7 +63,25 @@ DELETE FROM vam_reply WHERE replyId = #{replyId} - + + + + + + + update vam_book + set ratingAvg = #{ratingAvg} + where bookId = #{bookId} + + + + \ No newline at end of file diff --git a/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties b/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties index ffd551e..48c9b67 100644 --- a/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties +++ b/VamPa/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties @@ -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 diff --git a/VamPa_MySQL/src/main/java/com/vam/mapper/ReplyMapper.java b/VamPa_MySQL/src/main/java/com/vam/mapper/ReplyMapper.java index 0c0c8b9..09d1300 100644 --- a/VamPa_MySQL/src/main/java/com/vam/mapper/ReplyMapper.java +++ b/VamPa_MySQL/src/main/java/com/vam/mapper/ReplyMapper.java @@ -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); + } diff --git a/VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java b/VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java new file mode 100644 index 0000000..3c80454 --- /dev/null +++ b/VamPa_MySQL/src/main/java/com/vam/model/UpdateReplyDTO.java @@ -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 + "]"; + } + +} diff --git a/VamPa_MySQL/src/main/java/com/vam/service/ReplyServiceImpl.java b/VamPa_MySQL/src/main/java/com/vam/service/ReplyServiceImpl.java index 968d7c5..9695adc 100644 --- a/VamPa_MySQL/src/main/java/com/vam/service/ReplyServiceImpl.java +++ b/VamPa_MySQL/src/main/java/com/vam/service/ReplyServiceImpl.java @@ -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); + } } diff --git a/VamPa_MySQL/src/main/resources/com/vam/mapper/ReplyMapper.xml b/VamPa_MySQL/src/main/resources/com/vam/mapper/ReplyMapper.xml index 6f7a9d9..fb70129 100644 --- a/VamPa_MySQL/src/main/resources/com/vam/mapper/ReplyMapper.xml +++ b/VamPa_MySQL/src/main/resources/com/vam/mapper/ReplyMapper.xml @@ -60,5 +60,21 @@ WHERE replyId = #{replyId} + + + + + + update vam_book + set ratingAvg = #{ratingAvg} + where bookId = #{bookId} + + \ No newline at end of file diff --git a/VamPa_MySQL/target/classes/com/vam/mapper/ReplyMapper.xml b/VamPa_MySQL/target/classes/com/vam/mapper/ReplyMapper.xml index 6f7a9d9..fb70129 100644 --- a/VamPa_MySQL/target/classes/com/vam/mapper/ReplyMapper.xml +++ b/VamPa_MySQL/target/classes/com/vam/mapper/ReplyMapper.xml @@ -60,5 +60,21 @@ WHERE replyId = #{replyId} + + + + + + update vam_book + set ratingAvg = #{ratingAvg} + where bookId = #{bookId} + + \ No newline at end of file diff --git a/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties b/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties index 2eeb6f2..fd515d6 100644 --- a/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties +++ b/VamPa_MySQL/target/m2e-wtp/web-resources/META-INF/maven/com.vam/controller/pom.properties @@ -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