Skip to content

Commit caed785

Browse files
authored
Merge pull request #12 from objectcomputing/getresponses
Getresponses
2 parents d51b660 + 9c646d2 commit caed785

19 files changed

Lines changed: 698 additions & 52 deletions

File tree

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3'
2+
services:
3+
postgresql:
4+
image: postgres:11.6
5+
environment:
6+
POSTGRES_DB: pulsesurveydb
7+
POSTGRES_USER: postgres
8+
POSTGRES_PASSWORD: postgres
9+
volumes:
10+
- ./server/src/main/resources/db/2019.12.18.14.06.00_createInitialDb.sql:/docker-entrypoint-initdb.d/2019.12.18.14.06.00_createInitialDb.sql
11+
ports:
12+
- "5432:5432"
13+
container_name: pulsesurveydb
14+

send-surveys/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ dependencies {
5353
implementation "io.micronaut:micronaut-function-aws:1.3.6"
5454
runtimeOnly "com.amazonaws:aws-lambda-java-log4j2:1.0.0"
5555

56+
compile 'io.micronaut:micronaut-views'
57+
compile 'com.github.jknack:handlebars:4.1.0'
5658
compile 'io.micronaut.data:micronaut-data-jdbc:1.0.0.M5'
5759
compile 'com.github.spullara.mustache.java:compiler:0.9.6'
5860
compile "com.sun.mail:javax.mail:1.6.2"

send-surveys/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
micronautVersion=1.2.8
1+
micronautVersion=1.3.1
22
micronautDataVersion=1.0.0.M5

send-surveys/src/main/java/com/objectcomputing/pulsesurvey/model/Response.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package com.objectcomputing.pulsesurvey.model;
22

3+
import io.micronaut.data.annotation.AutoPopulated;
4+
import io.micronaut.data.annotation.DateCreated;
35
import io.micronaut.data.annotation.Id;
46
import org.joda.time.DateTimeZone;
57

68
import javax.persistence.Column;
9+
import javax.persistence.Entity;
10+
import javax.persistence.Table;
11+
import java.time.LocalDateTime;
712
import java.util.UUID;
813

14+
@Entity
15+
@Table(name = "response")
916
public class Response {
1017

1118
@Id
19+
@Column(name="responseid")
20+
@AutoPopulated
1221
private UUID responseId;
1322

14-
@Column
23+
@Column(name="responsekey")
1524
private UUID responseKey;
1625

1726
@Column
18-
private int selected;
27+
private String selected;
1928

20-
@Column
21-
private DateTimeZone createdOn;
29+
@Column(name="createdon")
30+
@DateCreated
31+
private LocalDateTime createdOn;
2232

2333
public UUID getResponseId() {
2434
return responseId;
@@ -36,26 +46,26 @@ public void setResponseKey(UUID responseKey) {
3646
this.responseKey = responseKey;
3747
}
3848

39-
public int getSelected() {
49+
public String getSelected() {
4050
return selected;
4151
}
4252

43-
public void setSelected(int selected) {
53+
public void setSelected(String selected) {
4454
this.selected = selected;
4555
}
4656

47-
public DateTimeZone getCreatedOn() {
57+
public LocalDateTime getCreatedOn() {
4858
return createdOn;
4959
}
5060

51-
public void setCreatedOn(DateTimeZone createdOn) {
61+
public void setCreatedOn(LocalDateTime createdOn) {
5262
this.createdOn = createdOn;
5363
}
5464

5565
public Response() {
5666
}
5767

58-
public Response(UUID responseId, UUID responseKey, int selected, DateTimeZone createdOn) {
68+
public Response(UUID responseId, UUID responseKey, String selected, LocalDateTime createdOn) {
5969
this.responseId = responseId;
6070
this.responseKey = responseKey;
6171
this.selected = selected;

send-surveys/src/main/java/com/objectcomputing/pulsesurvey/model/ResponseKey.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class ResponseKey {
2424
@DateCreated
2525
private LocalDateTime issuedOn;
2626

27+
@Column(name="used")
28+
private boolean used = false;
29+
2730
public UUID getResponseKey() {
2831
return responseKey;
2932
}
@@ -40,11 +43,16 @@ public void setIssuedOn(LocalDateTime issuedOn) {
4043
this.issuedOn = issuedOn;
4144
}
4245

46+
public boolean isUsed() { return used; }
47+
48+
public void setUsed(boolean used) { this.used = used; }
49+
4350
public ResponseKey() {
4451
}
4552

46-
public ResponseKey(UUID responseKey, LocalDateTime issuedOn) {
53+
public ResponseKey(UUID responseKey, LocalDateTime issuedOn, boolean used) {
4754
this.responseKey = responseKey;
4855
this.issuedOn = issuedOn;
56+
this.used = used;
4957
}
5058
}

send-surveys/src/main/java/com/objectcomputing/pulsesurvey/model/UserComments.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
package com.objectcomputing.pulsesurvey.model;
22

3+
import io.micronaut.data.annotation.AutoPopulated;
4+
import io.micronaut.data.annotation.DateCreated;
35
import io.micronaut.data.annotation.Id;
46
import org.joda.time.DateTimeZone;
57

68
import javax.persistence.Column;
9+
import javax.persistence.Entity;
10+
import javax.persistence.Table;
11+
import java.time.LocalDateTime;
712
import java.util.UUID;
813

14+
@Entity
15+
@Table(name = "usercomments")
916
public class UserComments {
1017

1118
@Id
19+
@Column(name="commentid")
20+
@AutoPopulated
1221
private UUID commentId;
1322

14-
@Column
23+
@Column(name="responsekey")
1524
private UUID responseKey;
1625

17-
@Column
26+
@Column(name="commenttext")
1827
private String commentText;
1928

20-
@Column
21-
private DateTimeZone createdOn;
29+
@Column(name="createdon")
30+
@DateCreated
31+
private LocalDateTime createdOn;
2232

2333
public UUID getCommentId() {
2434
return commentId;
@@ -44,18 +54,18 @@ public void setCommentText(String commentText) {
4454
this.commentText = commentText;
4555
}
4656

47-
public DateTimeZone getCreatedOn() {
57+
public LocalDateTime getCreatedOn() {
4858
return createdOn;
4959
}
5060

51-
public void setCreatedOn(DateTimeZone createdOn) {
61+
public void setCreatedOn(LocalDateTime createdOn) {
5262
this.createdOn = createdOn;
5363
}
5464

5565
public UserComments() {
5666
}
5767

58-
public UserComments(UUID commentId, UUID responseKey, String commentText, DateTimeZone createdOn) {
68+
public UserComments(UUID commentId, UUID responseKey, String commentText, LocalDateTime createdOn) {
5969
this.commentId = commentId;
6070
this.responseKey = responseKey;
6171
this.commentText = commentText;
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package com.objectcomputing.pulsesurvey.receive.responses;
2+
3+
import com.objectcomputing.pulsesurvey.model.Response;
4+
import com.objectcomputing.pulsesurvey.model.ResponseKey;
5+
import com.objectcomputing.pulsesurvey.model.UserComments;
6+
import com.objectcomputing.pulsesurvey.repositories.ResponseKeyRepository;
7+
import com.objectcomputing.pulsesurvey.repositories.ResponseRepository;
8+
import com.objectcomputing.pulsesurvey.repositories.UserCommentsRepository;
9+
import io.micronaut.context.annotation.Value;
10+
import io.micronaut.core.util.CollectionUtils;
11+
import io.micronaut.http.HttpResponse;
12+
import io.micronaut.http.MediaType;
13+
import io.micronaut.http.annotation.Consumes;
14+
import io.micronaut.http.annotation.Controller;
15+
import io.micronaut.http.annotation.Get;
16+
import io.micronaut.http.annotation.Post;
17+
import io.micronaut.http.annotation.Produces;
18+
import io.micronaut.views.View;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import javax.inject.Inject;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
import java.util.Optional;
26+
import java.util.UUID;
27+
import java.util.concurrent.atomic.AtomicBoolean;
28+
import java.util.concurrent.atomic.AtomicReference;
29+
30+
@Controller("/happiness")
31+
public class SurveyResponseController {
32+
33+
private static final Logger LOG = LoggerFactory.getLogger(SurveyResponseController.class);
34+
35+
@Inject
36+
private ResponseKeyRepository responseKeyRepo;
37+
38+
@Inject
39+
private ResponseRepository responseRepo;
40+
41+
@Inject
42+
private UserCommentsRepository userCommentsRepo;
43+
44+
public void setResponseKeyRepo(ResponseKeyRepository responseKeyRepository) {
45+
this.responseKeyRepo = responseKeyRepository;
46+
}
47+
48+
public void setResponseRepo(ResponseRepository responseRepository) {
49+
this.responseRepo = responseRepository;
50+
}
51+
52+
public void setUserCommentsRepo(UserCommentsRepository userCommentsRepository) {
53+
this.userCommentsRepo = userCommentsRepository;
54+
}
55+
56+
@Produces(MediaType.TEXT_PLAIN)
57+
@Get("/received")
58+
HttpResponse<String> happinessReceived(String currentEmotion, String surveyKey) {
59+
60+
LOG.info("Hello, I have received your emotion of: " + currentEmotion + "!" +
61+
" with a key of: " + surveyKey);
62+
63+
return HttpResponse.ok("Hello, I have received your emotion of: " + currentEmotion + "!" +
64+
" with a key of: " + surveyKey);
65+
}
66+
67+
@Get
68+
@Produces(MediaType.TEXT_PLAIN)
69+
HttpResponse<String> happiness(String currentEmotion, String surveyKey) {
70+
71+
LOG.info("Hello, your current emotion is " + currentEmotion + "!" +
72+
" with a key of: " + surveyKey);
73+
74+
boolean validKey = false;
75+
validKey = validateKey(surveyKey);
76+
77+
LOG.info("happiness - key is valid? " + validKey);
78+
79+
// if yes - store happiness and comments
80+
if (validKey) {
81+
boolean responseAdded = saveResponse(currentEmotion, surveyKey);
82+
83+
if (responseAdded) {
84+
markKeyAsUsed(surveyKey);
85+
86+
try {
87+
LOG.info("redirecting to /happiness/comment");
88+
return HttpResponse.redirect(new URI("/happiness/comment?surveyKey="+surveyKey));
89+
} catch (URISyntaxException e) {
90+
e.printStackTrace();
91+
LOG.error("unable to redirect to /happiness/comment " + e.getMessage());
92+
}
93+
}
94+
} else {
95+
LOG.warn("This key is not valid: " + surveyKey);
96+
}
97+
98+
return HttpResponse.ok("Hello, your current emotion of " + currentEmotion + "!" +
99+
" is duly noted.");
100+
}
101+
102+
@Get("comment")
103+
@View("comment")
104+
public HttpResponse displayComments(String surveyKey) {
105+
106+
LOG.info("in /comment. surveyKey = " + surveyKey);
107+
return HttpResponse.ok(CollectionUtils.mapOf("surveyKey", surveyKey));
108+
}
109+
110+
@Post("userComments")
111+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
112+
@View("thankyou")
113+
public HttpResponse sendThankYouWithCommentBlock
114+
(@Value("userComments") String userComments,
115+
@Value("surveyKey") String surveyKey) {
116+
117+
LOG.info("The user has commented: " + userComments);
118+
LOG.info("With surveyKey: " + surveyKey);
119+
// put comment into the db using the survey key
120+
saveUserComment(surveyKey, userComments);
121+
122+
return HttpResponse.ok();
123+
}
124+
125+
@Get("thanks")
126+
@View("thankyou")
127+
public HttpResponse sendThankYou() {
128+
129+
LOG.info("Sending final thank you web page " );
130+
return HttpResponse.ok();
131+
132+
}
133+
134+
boolean validateKey(String surveyKey) {
135+
136+
LOG.info("Validating key " + surveyKey);
137+
Optional<ResponseKey> responseKey = responseKeyRepo.findById(UUID.fromString(surveyKey));
138+
AtomicBoolean isValid = new AtomicBoolean(false);
139+
140+
responseKey.ifPresent(key -> {
141+
isValid.set(!key.isUsed());
142+
});
143+
144+
LOG.debug("Survey Key " + surveyKey + (isValid.get()?" is used":" is not used"));
145+
146+
LOG.info("key is valid? " + isValid);
147+
148+
return isValid.get();
149+
}
150+
151+
boolean saveResponse(String currentEmotion, String surveyKey) {
152+
153+
boolean responseAdded = false;
154+
Response response = new Response();
155+
156+
response.setResponseKey(UUID.fromString(surveyKey));
157+
response.setSelected(currentEmotion);
158+
response = responseRepo.save(response);
159+
160+
LOG.info("Adding response " + currentEmotion + " ");
161+
162+
if (response.getResponseId() != null) responseAdded = true;
163+
164+
return responseAdded;
165+
}
166+
167+
private void saveUserComment(String surveyKey, String comments) {
168+
169+
UserComments userComments = new UserComments();
170+
userComments.setCommentText(comments);
171+
userComments.setResponseKey(UUID.fromString(surveyKey));
172+
userCommentsRepo.save(userComments);
173+
}
174+
175+
ResponseKey markKeyAsUsed(String surveyKey) {
176+
177+
LOG.info("Marking key as used: " + surveyKey + " from responsekeys");
178+
179+
AtomicReference<ResponseKey> returnedResponseKey = new AtomicReference<>(new ResponseKey());
180+
Optional<ResponseKey> responseKey = responseKeyRepo.findById(UUID.fromString(surveyKey));
181+
182+
responseKey.ifPresent(responseKeyToSave -> {
183+
responseKeyToSave.setUsed(true);
184+
returnedResponseKey.set(responseKeyRepo.update(responseKeyToSave));
185+
});
186+
return returnedResponseKey.get();
187+
}
188+
189+
}

0 commit comments

Comments
 (0)