diff --git a/src/main/java/com/loafle/overflow/commons/model/PageParams.java b/src/main/java/com/loafle/overflow/commons/model/PageParams.java new file mode 100644 index 0000000..b459a25 --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/model/PageParams.java @@ -0,0 +1,44 @@ +package com.loafle.overflow.commons.model; + +/** + * Created by insanity on 17. 8. 25. + */ +public class PageParams { + + private int pageNo; + private int countPerPage; + private String sortCol; + private String sortDirection; + + public int getPageNo() { + return pageNo; + } + + public void setPageNo(int pageNo) { + this.pageNo = pageNo; + } + + public int getCountPerPage() { + return countPerPage; + } + + public void setCountPerPage(int countPerPage) { + this.countPerPage = countPerPage; + } + + public String getSortCol() { + return sortCol; + } + + public void setSortCol(String sortCol) { + this.sortCol = sortCol; + } + + public String getSortDirection() { + return sortDirection; + } + + public void setSortDirection(String sortDirection) { + this.sortDirection = sortDirection; + } +} diff --git a/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java b/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java new file mode 100644 index 0000000..e39518c --- /dev/null +++ b/src/main/java/com/loafle/overflow/commons/utils/PageUtil.java @@ -0,0 +1,22 @@ +package com.loafle.overflow.commons.utils; + +import com.loafle.overflow.commons.model.PageParams; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; + +/** + * Created by insanity on 17. 8. 25. + */ +public class PageUtil { + private static Sort.Direction getSortDirection(String sortDirection) { + if(sortDirection.equalsIgnoreCase("ascending")) { + return Sort.Direction.ASC; + } + return Sort.Direction.DESC; + } + + public static PageRequest getPageRequest(PageParams pageParams) { + return new PageRequest(pageParams.getPageNo(), pageParams.getCountPerPage(), + new Sort(PageUtil.getSortDirection(pageParams.getSortDirection()), pageParams.getSortCol())); + } +} diff --git a/src/main/java/com/loafle/overflow/module/history/service/HistoryService.java b/src/main/java/com/loafle/overflow/module/history/service/HistoryService.java index 5d096de..bd0a970 100644 --- a/src/main/java/com/loafle/overflow/module/history/service/HistoryService.java +++ b/src/main/java/com/loafle/overflow/module/history/service/HistoryService.java @@ -1,5 +1,7 @@ package com.loafle.overflow.module.history.service; +import com.loafle.overflow.commons.model.PageParams; +import com.loafle.overflow.commons.utils.PageUtil; import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.history.dao.HistoryDAO; import com.loafle.overflow.module.history.model.History; @@ -7,9 +9,6 @@ import com.loafle.overflow.module.meta.model.MetaHistoryType; import com.loafle.overflow.module.probe.model.Probe; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; @Service("HistoryService") @@ -22,32 +21,21 @@ public class HistoryService { return this.historyDAO.save(history); } -// public List readAllByProbe(Probe probe) { -// return this.historyDAO.findAllByProbeOrderByIdDesc(probe); -// } - public Page readAllByProbeAndType(Probe probe, MetaHistoryType type, int pageNo, int countPerPage) { - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - return this.historyDAO.findAllByProbeAndType(probe, type, pageRequest); + public Page readAllByProbeAndType(Probe probe, MetaHistoryType type, PageParams pageParams) { + return this.historyDAO.findAllByProbeAndType(probe, type, PageUtil.getPageRequest(pageParams)); } - public Page readAllByProbe(Probe probe, int pageNo, int countPerPage) { - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - return this.historyDAO.findAllByProbe(probe, pageRequest); + public Page readAllByProbe(Probe probe, PageParams pageParams) { + return this.historyDAO.findAllByProbe(probe, PageUtil.getPageRequest(pageParams)); } - public Page readAllByDomain(Domain domain, int pageNo, int countPerPage) { - - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - return this.historyDAO.findAllByDomain(domain, pageRequest); + public Page readAllByDomain(Domain domain, PageParams pageParams) { + return this.historyDAO.findAllByDomain(domain, PageUtil.getPageRequest(pageParams)); } - public Page readAllByDomainAndType(Domain domain, MetaHistoryType type, int pageNo, int countPerPage) { - Pageable pageRequest = - new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); - return this.historyDAO.findAllByDomainAndType(domain, type, pageRequest); + public Page readAllByDomainAndType(Domain domain, MetaHistoryType type, PageParams pageParams) { + return this.historyDAO.findAllByDomainAndType(domain, type, PageUtil.getPageRequest(pageParams)); } + } diff --git a/src/main/java/com/loafle/overflow/module/notification/dao/NotificationDAO.java b/src/main/java/com/loafle/overflow/module/notification/dao/NotificationDAO.java new file mode 100644 index 0000000..7c1f0c8 --- /dev/null +++ b/src/main/java/com/loafle/overflow/module/notification/dao/NotificationDAO.java @@ -0,0 +1,22 @@ +package com.loafle.overflow.module.notification.dao; + +import com.loafle.overflow.module.member.model.Member; +import com.loafle.overflow.module.notification.model.Notification; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +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; + +/** + * Created by insanity on 17. 8. 25. + */ +@Repository +public interface NotificationDAO extends JpaRepository { + + Page findAllByMember(Member member, Pageable pageRequest); + + @Query("SELECT n FROM Notification n WHERE n.member.id = :#{#member.id} and n.confirmDate IS NULL") + Page findAllUnconfirmedByMember(@Param("member") Member member, Pageable pageRequest); +} diff --git a/src/main/java/com/loafle/overflow/module/notification/model/Notification.java b/src/main/java/com/loafle/overflow/module/notification/model/Notification.java new file mode 100644 index 0000000..15a9bc8 --- /dev/null +++ b/src/main/java/com/loafle/overflow/module/notification/model/Notification.java @@ -0,0 +1,78 @@ +package com.loafle.overflow.module.notification.model; + +import com.loafle.overflow.module.member.model.Member; + +import javax.persistence.*; +import java.util.Date; + +/** + * Created by insanity on 17. 8. 25. + */ +@Entity +@Table(name = "NOTIFICATION", schema = "public") +public class Notification { + private long id; + private Date createDate; + private String title; + private String message; + private Member member; + private Date confirmDate; + + @Id + @GeneratedValue(strategy= GenerationType.IDENTITY) + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "CREATE_DATE", nullable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false) + public Date getCreateDate() { + return createDate; + } + + public void setCreateDate(Date createDate) { + this.createDate = createDate; + } + + @Column(name = "TITLE", nullable = false, length = 50) + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Column(name = "MESSAGE", nullable = false, length = 255) + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + @ManyToOne + @JoinColumn(name = "MEMBER_ID", nullable = false) + public Member getMember() { + return member; + } + + public void setMember(Member member) { + this.member = member; + } + + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "CREATE_DATE", nullable = true) + public Date getConfirmDate() { + return confirmDate; + } + + public void setConfirmDate(Date confirmDate) { + this.confirmDate = confirmDate; + } +} diff --git a/src/main/java/com/loafle/overflow/module/notification/service/NotificationService.java b/src/main/java/com/loafle/overflow/module/notification/service/NotificationService.java new file mode 100644 index 0000000..0d97110 --- /dev/null +++ b/src/main/java/com/loafle/overflow/module/notification/service/NotificationService.java @@ -0,0 +1,37 @@ +package com.loafle.overflow.module.notification.service; + +import com.loafle.overflow.module.member.model.Member; +import com.loafle.overflow.module.notification.dao.NotificationDAO; +import com.loafle.overflow.module.notification.model.Notification; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +/** + * Created by insanity on 17. 8. 25. + */ +@Service("NotificationService") +public class NotificationService { + + @Autowired + private NotificationDAO notificationDAO; + + public Notification regist(Notification notification) { + return this.notificationDAO.save(notification); + } + + public Page readAllByMember(Member member, int pageNo, int countPerPage) { + Pageable pageRequest = + new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); + return this.notificationDAO.findAllByMember(member, pageRequest); + } + + public Page readAllUnconfirmedByMember(Member member, int pageNo, int countPerPage) { + Pageable pageRequest = + new PageRequest(pageNo, countPerPage, new Sort(Sort.Direction.DESC, "id")); + return this.notificationDAO.findAllUnconfirmedByMember(member, pageRequest); + } +} diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql index 52c71ed..4a78def 100644 --- a/src/main/resources/init.sql +++ b/src/main/resources/init.sql @@ -694,6 +694,67 @@ INSERT INTO public.history (id,create_date,message,domain_id,member_id,probe_id, INSERT INTO public.history (id,create_date,message,domain_id,member_id,probe_id,type_id) VALUES ( 100,'2017-08-24 18:33:27.217','Test History 99',1,1,1,3); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +1,'2017-08-25 16:58:48.430','Message0','Title0',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +2,NULL,'Message1','Title1',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +3,'2017-08-25 16:58:48.520','Message2','Title2',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +4,NULL,'Message3','Title3',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +5,'2017-08-25 16:58:48.540','Message4','Title4',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +6,NULL,'Message5','Title5',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +7,'2017-08-25 16:58:48.566','Message6','Title6',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +8,NULL,'Message7','Title7',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +9,'2017-08-25 16:58:48.596','Message8','Title8',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +10,NULL,'Message9','Title9',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +11,'2017-08-25 16:58:48.628','Message10','Title10',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +12,NULL,'Message11','Title11',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +13,'2017-08-25 16:58:48.661','Message12','Title12',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +14,NULL,'Message13','Title13',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +15,'2017-08-25 16:58:48.682','Message14','Title14',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +16,NULL,'Message15','Title15',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +17,'2017-08-25 16:58:48.710','Message16','Title16',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +18,NULL,'Message17','Title17',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +19,'2017-08-25 16:58:48.734','Message18','Title18',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +20,NULL,'Message19','Title19',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +21,'2017-08-25 16:58:48.755','Message20','Title20',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +22,NULL,'Message21','Title21',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +23,'2017-08-25 16:58:48.778','Message22','Title22',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +24,NULL,'Message23','Title23',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +25,'2017-08-25 16:58:48.804','Message24','Title24',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +26,NULL,'Message25','Title25',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +27,'2017-08-25 16:58:48.829','Message26','Title26',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +28,NULL,'Message27','Title27',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +29,'2017-08-25 16:58:48.850','Message28','Title28',1); +INSERT INTO public.notification (id,create_date,message,title,member_id) VALUES ( +30,NULL,'Message29','Title29',1); + diff --git a/src/test/java/com/loafle/overflow/module/history/service/HistoryServiceTest.java b/src/test/java/com/loafle/overflow/module/history/service/HistoryServiceTest.java index 7fe4565..542ec6b 100644 --- a/src/test/java/com/loafle/overflow/module/history/service/HistoryServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/history/service/HistoryServiceTest.java @@ -1,5 +1,6 @@ package com.loafle.overflow.module.history.service; +import com.loafle.overflow.commons.model.PageParams; import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.history.model.History; import com.loafle.overflow.module.member.model.Member; @@ -51,16 +52,24 @@ public class HistoryServiceTest { // } @Test - @Ignore public void readAllByProbe() { - Page result = this.historyService.readAllByProbe(new Probe(1), 0, 10); + PageParams p = new PageParams(); + p.setPageNo(0); + p.setCountPerPage(10); + p.setSortCol("type.name"); + p.setSortDirection("descending"); + + Page result = this.historyService.readAllByProbe(new Probe(1), p); + for (History h : result.getContent()) { + System.out.println(h.getId()); + } Assert.assertEquals(10, result.getContent().size()); } - @Test - @Ignore - public void readAllByDomain() { - Page result = this.historyService.readAllByDomain(new Domain(1), 0, 10); - Assert.assertNotNull(result.getContent()); - } +// @Test +// @Ignore +// public void readAllByDomain() { +// Page result = this.historyService.readAllByDomain(new Domain(1), 0, 10); +// Assert.assertNotNull(result.getContent()); +// } } diff --git a/src/test/java/com/loafle/overflow/module/notification/service/NotificationServiceTest.java b/src/test/java/com/loafle/overflow/module/notification/service/NotificationServiceTest.java new file mode 100644 index 0000000..f93ee6c --- /dev/null +++ b/src/test/java/com/loafle/overflow/module/notification/service/NotificationServiceTest.java @@ -0,0 +1,56 @@ +package com.loafle.overflow.module.notification.service; + +import com.loafle.overflow.module.member.model.Member; +import com.loafle.overflow.module.notification.model.Notification; +import com.loafle.overflow.spring.AppConfigTest; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.Date; + +/** + * Created by insanity on 17. 8. 25. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {AppConfigTest.class}) +public class NotificationServiceTest { + @Autowired + private NotificationService notificationService; + + @Test + @Ignore + public void testRegist() { + for (int i=0; i<30; i++) { + Notification n = new Notification(); + n.setMember(new Member(1)); + n.setTitle("Title"+i); + n.setMessage("Message"+i); + if((i%2)==0) { + n.setConfirmDate(new Date()); + } + this.notificationService.regist(n); + } + } + + @Test + @Ignore + public void testReadAll() { + this.testRegist(); + Page notis = this.notificationService.readAllByMember(new Member(1), 0, 10); + Assert.assertEquals(30, notis.getTotalElements()); + } + + @Test + @Ignore + public void testReadAllUnread() { + this.testRegist(); + Page notis = this.notificationService.readAllUnconfirmedByMember(new Member(1), 0, 10); + Assert.assertEquals(15, notis.getTotalElements()); + } +}