Skip to content

Commit 6b4b5b6

Browse files
committed
added post-view and postviewCount api
1 parent 5d7a8c6 commit 6b4b5b6

File tree

12 files changed

+510
-35
lines changed

12 files changed

+510
-35
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/src/main/webapp/content/css/main.css
55
/target/classes/static/**
66
/src/test/javascript/coverage/
7-
7+
/gen/
88
######################
99
# Node
1010
######################

src/main/java/com/cevheri/blog/domain/Post.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public class Post extends AbstractAuditingEntity
7272

7373
// jhipster-needle-entity-add-field - JHipster will add fields here
7474

75+
private String integrationId;
76+
77+
public String getIntegrationId() {
78+
return integrationId;
79+
}
80+
81+
public void setIntegrationId(String integrationId) {
82+
this.integrationId = integrationId;
83+
}
84+
7585
public Long getId() {
7686
return this.id;
7787
}
@@ -216,6 +226,7 @@ public String toString() {
216226
", content='" + getContent() + "'" +
217227
", paidMemberOnly='" + getPaidMemberOnly() + "'" +
218228
", publishThirdPartyApp='" + getPublishThirdPartyApp() + "'" +
229+
", integrationId='" + getIntegrationId() + "'" +
219230
"}";
220231
}
221232
}

src/main/java/com/cevheri/blog/repository/PostRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ default Page<Post> findAllWithEagerRelationships(Pageable pageable) {
4545
Optional<Post> findOneWithToOneRelationships(@Param("id") Long id);
4646

4747
Page<Post> findByBlogUserLoginOrderByCreatedDateDesc(String currentUserLogin, Pageable pageable);
48+
4849
}

src/main/java/com/cevheri/blog/repository/PostViewRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ default Page<PostView> findAllWithEagerRelationships(Pageable pageable) {
3737

3838
@Query("select postView from PostView postView left join fetch postView.post where postView.id =:id")
3939
Optional<PostView> findOneWithToOneRelationships(@Param("id") Long id);
40+
41+
Integer countByPost_Id(@Param("postId") Long postId);
4042
}

src/main/java/com/cevheri/blog/service/PostService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.cevheri.blog.service.dto.PostDTO;
44
import java.util.Optional;
5+
6+
import com.cevheri.blog.service.dto.UpdatePostDTO;
57
import org.springframework.data.domain.Page;
68
import org.springframework.data.domain.Pageable;
79

@@ -23,7 +25,7 @@ public interface PostService {
2325
* @param postDTO the entity to update.
2426
* @return the persisted entity.
2527
*/
26-
PostDTO update(PostDTO postDTO);
28+
PostDTO update(UpdatePostDTO postDTO);
2729

2830
/**
2931
* Partially updates a post.
@@ -63,4 +65,6 @@ public interface PostService {
6365
* @param id the id of the entity.
6466
*/
6567
void delete(Long id);
68+
69+
Integer viewCount(Long id);
6670
}

src/main/java/com/cevheri/blog/service/dto/PostDTO.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ public class PostDTO implements Serializable {
5050

5151
private Set<TagDTO> tags = new HashSet<>();
5252

53+
54+
private String integrationId;
55+
56+
public String getIntegrationId() {
57+
return integrationId;
58+
}
59+
60+
public void setIntegrationId(String integrationId) {
61+
this.integrationId = integrationId;
62+
}
63+
5364
public Long getId() {
5465
return id;
5566
}
@@ -144,6 +155,7 @@ public String toString() {
144155
", content='" + getContent() + "'" +
145156
", paidMemberOnly='" + getPaidMemberOnly() + "'" +
146157
", publishThirdPartyApp='" + getPublishThirdPartyApp() + "'" +
158+
", integrationId='" + getIntegrationId() + "'" +
147159
", user=" + getUser() +
148160
", blog=" + getBlog() +
149161
", tags=" + getTags() +
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.cevheri.blog.service.dto;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
import javax.persistence.Lob;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
8+
import java.io.Serializable;
9+
import java.util.HashSet;
10+
import java.util.Objects;
11+
import java.util.Set;
12+
13+
/**
14+
* A DTO for the {@link com.cevheri.blog.domain.Post} entity.
15+
*/
16+
@Schema(description = "Post page information. System users only!")
17+
public class UpdatePostDTO implements Serializable {
18+
19+
private Long id;
20+
21+
/**
22+
* Post title information
23+
*/
24+
@NotNull
25+
@Size(min = 3, max = 250)
26+
@Schema(description = "Post title information", required = true)
27+
private String title;
28+
29+
/**
30+
* Post content information
31+
*/
32+
33+
@Schema(description = "Post content information", required = true)
34+
@Lob
35+
private String content;
36+
37+
/**
38+
* Paid Membership
39+
*/
40+
@Schema(description = "Paid Membership")
41+
private Boolean paidMemberOnly;
42+
43+
/**
44+
* Publish third party app. for example Medium.
45+
*/
46+
@Schema(description = "Publish third party app. for example Medium.")
47+
private Boolean publishThirdPartyApp;
48+
49+
private Set<TagDTO> tags = new HashSet<>();
50+
51+
52+
public Long getId() {
53+
return id;
54+
}
55+
56+
public void setId(Long id) {
57+
this.id = id;
58+
}
59+
60+
public String getTitle() {
61+
return title;
62+
}
63+
64+
public void setTitle(String title) {
65+
this.title = title;
66+
}
67+
68+
public String getContent() {
69+
return content;
70+
}
71+
72+
public void setContent(String content) {
73+
this.content = content;
74+
}
75+
76+
public Boolean getPaidMemberOnly() {
77+
return paidMemberOnly;
78+
}
79+
80+
public void setPaidMemberOnly(Boolean paidMemberOnly) {
81+
this.paidMemberOnly = paidMemberOnly;
82+
}
83+
84+
public Boolean getPublishThirdPartyApp() {
85+
return publishThirdPartyApp;
86+
}
87+
88+
public void setPublishThirdPartyApp(Boolean publishThirdPartyApp) {
89+
this.publishThirdPartyApp = publishThirdPartyApp;
90+
}
91+
92+
public Set<TagDTO> getTags() {
93+
return tags;
94+
}
95+
96+
public void setTags(Set<TagDTO> tags) {
97+
this.tags = tags;
98+
}
99+
100+
@Override
101+
public boolean equals(Object o) {
102+
if (this == o) {
103+
return true;
104+
}
105+
if (!(o instanceof UpdatePostDTO)) {
106+
return false;
107+
}
108+
109+
UpdatePostDTO postDTO = (UpdatePostDTO) o;
110+
if (this.id == null) {
111+
return false;
112+
}
113+
return Objects.equals(this.id, postDTO.id);
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return Objects.hash(this.id);
119+
}
120+
121+
// prettier-ignore
122+
@Override
123+
public String toString() {
124+
return "PostDTO{" +
125+
"id=" + getId() +
126+
", title='" + getTitle() + "'" +
127+
", content='" + getContent() + "'" +
128+
", paidMemberOnly='" + getPaidMemberOnly() + "'" +
129+
", publishThirdPartyApp='" + getPublishThirdPartyApp() + "'" +
130+
", tags=" + getTags() +
131+
"}";
132+
}
133+
}

src/main/java/com/cevheri/blog/service/impl/PostServiceImpl.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
import com.cevheri.blog.domain.Post;
44
import com.cevheri.blog.repository.PostRepository;
5+
import com.cevheri.blog.repository.PostViewRepository;
56
import com.cevheri.blog.service.PostService;
7+
import com.cevheri.blog.service.PostViewService;
8+
import com.cevheri.blog.service.ThirdPartyBlogService;
69
import com.cevheri.blog.service.dto.PostDTO;
10+
import com.cevheri.blog.service.dto.PostViewDTO;
11+
import com.cevheri.blog.service.dto.UpdatePostDTO;
712
import com.cevheri.blog.service.mapper.PostMapper;
13+
814
import java.util.Optional;
15+
16+
import com.cevheri.blog.web.rest.errors.BadRequestAlertException;
917
import org.slf4j.Logger;
1018
import org.slf4j.LoggerFactory;
1119
import org.springframework.data.domain.Page;
@@ -23,24 +31,41 @@ public class PostServiceImpl implements PostService {
2331
private final Logger log = LoggerFactory.getLogger(PostServiceImpl.class);
2432

2533
private final PostRepository postRepository;
26-
34+
private final PostViewRepository postViewRepository;
2735
private final PostMapper postMapper;
36+
private final ThirdPartyBlogService thirdPartyBlogService;
37+
private final PostViewService postViewService;
2838

29-
public PostServiceImpl(PostRepository postRepository, PostMapper postMapper) {
39+
public PostServiceImpl(PostRepository postRepository,
40+
PostViewRepository postViewRepository,
41+
PostMapper postMapper,
42+
ThirdPartyBlogService thirdPartyBlogService,
43+
PostViewService postViewService) {
3044
this.postRepository = postRepository;
45+
this.postViewRepository = postViewRepository;
3146
this.postMapper = postMapper;
47+
this.thirdPartyBlogService = thirdPartyBlogService;
48+
this.postViewService = postViewService;
3249
}
3350

3451
@Override
3552
public PostDTO save(PostDTO postDTO) {
3653
log.debug("Request to save Post : {}", postDTO);
3754
Post post = postMapper.toEntity(postDTO);
3855
post = postRepository.save(post);
56+
if (postDTO.getPublishThirdPartyApp()) {
57+
58+
String integrationId = thirdPartyBlogService.sendPost(postDTO);
59+
60+
post.setIntegrationId(integrationId);
61+
postRepository.save(post);
62+
}
3963
return postMapper.toDto(post);
4064
}
4165

66+
4267
@Override
43-
public PostDTO update(PostDTO postDTO) {
68+
public PostDTO update(UpdatePostDTO postDTO) {
4469
log.debug("Request to save Post : {}", postDTO);
4570
Post post = postMapper.toEntity(postDTO);
4671
post = postRepository.save(post);
@@ -74,15 +99,31 @@ public Page<PostDTO> findAllWithEagerRelationships(Pageable pageable) {
7499
}
75100

76101
@Override
77-
@Transactional(readOnly = true)
78102
public Optional<PostDTO> findOne(Long id) {
79103
log.debug("Request to get Post : {}", id);
80-
return postRepository.findOneWithEagerRelationships(id).map(postMapper::toDto);
104+
var result = postRepository.findOneWithEagerRelationships(id).map(postMapper::toDto);
105+
result.ifPresentOrElse(t -> {
106+
PostViewDTO postView = new PostViewDTO();
107+
postView.setPost(t);
108+
postViewService.save(postView);
109+
},
110+
() -> {
111+
throw new BadRequestAlertException("Entity not found", "post", "idnotfound");
112+
}
113+
);
114+
115+
return result;
81116
}
82117

83118
@Override
84119
public void delete(Long id) {
85120
log.debug("Request to delete Post : {}", id);
86121
postRepository.deleteById(id);
87122
}
123+
124+
@Override
125+
public Integer viewCount(Long id) {
126+
log.debug("Request to ViewCount Post : {}", id);
127+
return postViewRepository.countByPost_Id(id);
128+
}
88129
}

0 commit comments

Comments
 (0)