page util

This commit is contained in:
insanity 2017-08-25 19:38:07 +09:00
parent f049815821
commit 066c966588
9 changed files with 348 additions and 31 deletions

View File

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

View File

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

View File

@ -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<History> readAllByProbe(Probe probe) {
// return this.historyDAO.findAllByProbeOrderByIdDesc(probe);
// }
public Page<History> 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<History> readAllByProbeAndType(Probe probe, MetaHistoryType type, PageParams pageParams) {
return this.historyDAO.findAllByProbeAndType(probe, type, PageUtil.getPageRequest(pageParams));
}
public Page<History> 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<History> readAllByProbe(Probe probe, PageParams pageParams) {
return this.historyDAO.findAllByProbe(probe, PageUtil.getPageRequest(pageParams));
}
public Page<History> 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<History> readAllByDomain(Domain domain, PageParams pageParams) {
return this.historyDAO.findAllByDomain(domain, PageUtil.getPageRequest(pageParams));
}
public Page<History> 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<History> readAllByDomainAndType(Domain domain, MetaHistoryType type, PageParams pageParams) {
return this.historyDAO.findAllByDomainAndType(domain, type, PageUtil.getPageRequest(pageParams));
}
}

View File

@ -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<Notification, Long> {
Page<Notification> findAllByMember(Member member, Pageable pageRequest);
@Query("SELECT n FROM Notification n WHERE n.member.id = :#{#member.id} and n.confirmDate IS NULL")
Page<Notification> findAllUnconfirmedByMember(@Param("member") Member member, Pageable pageRequest);
}

View File

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

View File

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

View File

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

View File

@ -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<History> 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<History> 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<History> result = this.historyService.readAllByDomain(new Domain(1), 0, 10);
Assert.assertNotNull(result.getContent());
}
// @Test
// @Ignore
// public void readAllByDomain() {
// Page<History> result = this.historyService.readAllByDomain(new Domain(1), 0, 10);
// Assert.assertNotNull(result.getContent());
// }
}

View File

@ -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<Notification> notis = this.notificationService.readAllByMember(new Member(1), 0, 10);
Assert.assertEquals(30, notis.getTotalElements());
}
@Test
@Ignore
public void testReadAllUnread() {
this.testRegist();
Page<Notification> notis = this.notificationService.readAllUnconfirmedByMember(new Member(1), 0, 10);
Assert.assertEquals(15, notis.getTotalElements());
}
}