Skip to content

Commit 2fefffb

Browse files
committed
Added PremiumMembershipOnly controller
1 parent d641d61 commit 2fefffb

File tree

5 files changed

+46
-30
lines changed

5 files changed

+46
-30
lines changed

src/main/docker/app.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# This configuration is intended for development purpose, it's **your** responsibility to harden it for production
21
version: '3.8'
32
services:
43
blog-app:
@@ -13,14 +12,10 @@ services:
1312
- SPRING_LIQUIBASE_URL=jdbc:postgresql://blog-postgresql:5432/blog
1413
- EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE=http://admin:[email protected]:8761/eureka/
1514
- JHIPSTER_SLEEP=30 # gives time for other services to boot before the application
16-
# If you want to expose these ports outside your dev PC,
17-
# remove the "127.0.0.1:" prefix
1815
ports:
1916
- "8080:8080"
2017
depends_on:
2118
- blog-postgresql
22-
- eureka-server
23-
# - zipkin
2419
- jhipster-control-center
2520
blog-postgresql:
2621
image: postgres:14.2
@@ -33,23 +28,6 @@ services:
3328
- POSTGRES_HOST_AUTH_METHOD=trust
3429
ports:
3530
- "5432:5432"
36-
eureka-server:
37-
image: taskbeez/eureka-server:master
38-
hostname: eureka-server
39-
ports:
40-
- "8761:8761"
41-
# config-server:
42-
# image: hyness/spring-cloud-config-server:latest
43-
# ports:
44-
# - "8888:8888"
45-
# links:
46-
# - "eureka-server"
47-
# volumes:
48-
# - ./config-server/config:/app/config
49-
# zipkin:
50-
# image: openzipkin/zipkin:2.23
51-
# ports:
52-
# - "9411:9411"
5331
jhipster-control-center:
5432
image: 'jhipster/jhipster-control-center:v0.5.0'
5533
command:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.cevheri.blog.service.error;
2+
3+
import org.zalando.problem.AbstractThrowableProblem;
4+
import org.zalando.problem.Status;
5+
6+
import java.net.URI;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class PremiumMemberShipOnlyException extends AbstractThrowableProblem {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
private final String entityName;
15+
16+
private final String errorKey;
17+
18+
public PremiumMemberShipOnlyException(String defaultMessage, String entityName, String errorKey) {
19+
this(URI.create("https://www.cevheri-blog.herokuapp.com/premium-membership-only-exception"), defaultMessage, entityName, errorKey);
20+
}
21+
22+
public PremiumMemberShipOnlyException(URI type, String defaultMessage, String entityName, String errorKey) {
23+
super(type, defaultMessage, Status.BAD_REQUEST, null, null, null, getAlertParameters(entityName, errorKey));
24+
this.entityName = entityName;
25+
this.errorKey = errorKey;
26+
}
27+
28+
public String getEntityName() {
29+
return entityName;
30+
}
31+
32+
public String getErrorKey() {
33+
return errorKey;
34+
}
35+
36+
private static Map<String, Object> getAlertParameters(String entityName, String errorKey) {
37+
Map<String, Object> parameters = new HashMap<>();
38+
parameters.put("message", "error." + errorKey);
39+
parameters.put("params", entityName);
40+
return parameters;
41+
}
42+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.cevheri.blog.service.dto.PostDTO;
1414
import com.cevheri.blog.service.dto.PostViewDTO;
1515
import com.cevheri.blog.service.dto.UpdatePostDTO;
16+
import com.cevheri.blog.service.error.PremiumMemberShipOnlyException;
1617
import com.cevheri.blog.service.mapper.PostMapper;
1718

1819
import java.util.Optional;
@@ -142,7 +143,7 @@ private void addPostViews(PostDTO t) {
142143
private void checkPremiumMembership(PostDTO t) {
143144
if (t.getPaidMemberOnly()) {
144145
if (!SecurityUtils.hasCurrentUserAnyOfAuthorities(AuthoritiesConstants.PREMIUM)) {
145-
throw new RuntimeException("Premium membership only!");
146+
throw new PremiumMemberShipOnlyException("Premium membership only!", "post","premiumMembershipOnly");
146147
}
147148
}
148149
}

src/main/java/com/cevheri/blog/web/rest/PostResource.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@ public ResponseEntity<?> updatePost(
100100
if (!Objects.equals(id, updatePostDTO.getId())) {
101101
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
102102
}
103-
104-
// if (!postRepository.existsById(id)) {
105-
// throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
106-
// }
107-
108103
var postDTO = postService.findOne(id);
109104
if (postDTO.isEmpty()) {
110105
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
@@ -116,7 +111,6 @@ public ResponseEntity<?> updatePost(
116111
}
117112

118113
convertToPostDto(updatePostDTO, postDTO);
119-
120114
PostDTO result = postService.update(postDTO.get());
121115
return ResponseEntity
122116
.ok()

src/main/webapp/i18n/en/error.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"500": "Internal server error."
1010
},
1111
"concurrencyFailure": "Another user modified this data at the same time as you. Your changes were rejected.",
12-
"validation": "Validation error on the server."
12+
"validation": "Validation error on the server.",
13+
"premiumMembershipOnly": "Premium Membership Only!"
1314
}
1415
}

0 commit comments

Comments
 (0)