Skip to content

Commit 0dc67fe

Browse files
authored
Merge pull request #48 from TAMULib/sprint4-b03412-voting
Sprint4 b03412 voting
2 parents 1fa4387 + 7e849f1 commit 0dc67fe

11 files changed

Lines changed: 201 additions & 35 deletions

File tree

src/main/java/edu/tamu/app/controller/ProjectController.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.security.access.prepost.PreAuthorize;
88
import org.springframework.web.bind.annotation.PathVariable;
9-
import org.springframework.web.bind.annotation.RequestBody;
109
import org.springframework.web.bind.annotation.RequestMapping;
1110
import org.springframework.web.bind.annotation.RestController;
1211

1312
import com.fasterxml.jackson.core.JsonParseException;
1413
import com.fasterxml.jackson.databind.JsonMappingException;
1514

1615
import edu.tamu.app.exception.UserNotFoundException;
17-
import edu.tamu.app.model.request.FeatureRequest;
16+
import edu.tamu.app.model.FeatureProposal;
1817
import edu.tamu.app.service.ProjectService;
1918
import edu.tamu.weaver.response.ApiResponse;
19+
import edu.tamu.weaver.validation.aspect.annotation.WeaverValidatedModel;
2020

2121
@RestController
2222
@RequestMapping("/projects")
@@ -37,12 +37,10 @@ public ApiResponse getById(@PathVariable Long id) throws JsonParseException, Jso
3737
return projectService.getById(id);
3838
}
3939

40-
// TODO: the following method needs to create FeatureRequest from a FeatureProposal
41-
4240
@RequestMapping("/feature")
4341
@PreAuthorize("hasRole('SERVICE_MANAGER')")
44-
public ApiResponse submitFeatureRequest(@RequestBody FeatureRequest request) throws UserNotFoundException {
45-
return projectService.submitFeatureRequest(request);
42+
public ApiResponse submitFeatureRequest(@WeaverValidatedModel FeatureProposal proposal) throws UserNotFoundException {
43+
return projectService.submitFeatureRequest(proposal);
4644
}
4745

4846
}

src/main/java/edu/tamu/app/controller/UserController.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import edu.tamu.app.model.User;
1414
import edu.tamu.app.model.repo.UserRepo;
1515
import edu.tamu.weaver.auth.annotation.WeaverCredentials;
16+
import edu.tamu.weaver.auth.annotation.WeaverUser;
1617
import edu.tamu.weaver.auth.model.Credentials;
1718
import edu.tamu.weaver.response.ApiResponse;
1819

@@ -50,6 +51,26 @@ public ApiResponse credentials(@WeaverCredentials Credentials credentials) {
5051
return new ApiResponse(SUCCESS, credentials);
5152
}
5253

54+
/**
55+
* Websocket endpoint to request credentials.
56+
*
57+
* @param shibObj
58+
* Object
59+
*
60+
* @return ApiResponse
61+
*
62+
* @throws Exception
63+
*
64+
*/
65+
@RequestMapping("/user")
66+
@PreAuthorize("hasRole('USER')")
67+
public ApiResponse getUser(@WeaverUser User user) {
68+
if (user == null) {
69+
return new ApiResponse(ERROR, "Unable to retrieve user!");
70+
}
71+
return new ApiResponse(SUCCESS, user);
72+
}
73+
5374
/**
5475
* Returns all users.
5576
*

src/main/java/edu/tamu/app/model/AbstractIdea.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package edu.tamu.app.model;
22

3-
import static javax.persistence.CascadeType.MERGE;
43
import static javax.persistence.CascadeType.REFRESH;
54
import static javax.persistence.FetchType.EAGER;
65

@@ -31,10 +30,10 @@ public abstract class AbstractIdea extends ValidatingBaseEntity {
3130
@UpdateTimestamp
3231
private Calendar lastModified;
3332

34-
@ManyToOne(fetch = EAGER, cascade = MERGE, optional = false)
33+
@ManyToOne(fetch = EAGER, cascade = REFRESH, optional = false)
3534
private Service service;
3635

37-
@ManyToOne(cascade = REFRESH, optional = false)
36+
@ManyToOne(fetch = EAGER, cascade = REFRESH, optional = false)
3837
private User author;
3938

4039
public AbstractIdea() {

src/main/java/edu/tamu/app/model/FeatureProposal.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,38 @@
77
import java.util.List;
88

99
import javax.persistence.CascadeType;
10+
import javax.persistence.Column;
1011
import javax.persistence.Entity;
1112
import javax.persistence.JoinTable;
12-
import javax.persistence.OneToMany;
13+
import javax.persistence.ManyToMany;
1314
import javax.persistence.UniqueConstraint;
1415

1516
import org.hibernate.annotations.Fetch;
1617

1718
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
1819
import com.fasterxml.jackson.annotation.JsonIdentityReference;
20+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1921
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
2022

2123
@Entity
24+
@JsonIgnoreProperties(value = { "voters" }, allowGetters = true)
2225
public class FeatureProposal extends AbstractIdea {
2326

24-
@OneToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE })
27+
@ManyToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE })
2528
@JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "feature_proposal_id", "ideas_id" }))
2629
@Fetch(value = SELECT)
2730
private List<Idea> ideas;
2831

29-
@OneToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE })
32+
@ManyToMany(fetch = EAGER, cascade = { CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE })
3033
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, scope = User.class, property = "id")
3134
@JsonIdentityReference(alwaysAsId = true)
3235
@JoinTable(uniqueConstraints = @UniqueConstraint(columnNames = { "feature_proposal_id", "voters_id" }))
3336
@Fetch(value = SELECT)
3437
private List<User> voters;
3538

39+
@Column(nullable = false)
40+
private boolean submitted;
41+
3642
public FeatureProposal() {
3743
super();
3844
setup();
@@ -62,6 +68,7 @@ public FeatureProposal(Idea idea) {
6268
private void setup() {
6369
this.ideas = new ArrayList<Idea>();
6470
this.voters = new ArrayList<User>();
71+
this.submitted = false;
6572
}
6673

6774
public List<Idea> getIdeas() {
@@ -79,10 +86,8 @@ public void setIdeas(List<Idea> ideas) {
7986
}
8087

8188
public void addIdea(Idea idea) {
82-
if (!this.ideas.contains(idea)) {
83-
this.ideas.add(idea);
84-
addVoter(idea.getAuthor());
85-
}
89+
this.ideas.add(idea);
90+
addVoter(idea.getAuthor());
8691
}
8792

8893
public void removeIdea(Idea idea) {
@@ -99,9 +104,7 @@ public void setVoters(List<User> voters) {
99104
}
100105

101106
public void addVoter(User voter) {
102-
if (!this.voters.contains(voter)) {
103-
this.voters.add(voter);
104-
}
107+
this.voters.add(voter);
105108
}
106109

107110
public void removeVoter(User voter) {
@@ -112,4 +115,12 @@ public int getVotes() {
112115
return this.voters.size();
113116
}
114117

118+
public boolean isSubmitted() {
119+
return submitted;
120+
}
121+
122+
public void setSubmitted(boolean submitted) {
123+
this.submitted = submitted;
124+
}
125+
115126
}

src/main/java/edu/tamu/app/model/Idea.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package edu.tamu.app.model;
22

3+
import javax.persistence.Column;
34
import javax.persistence.Entity;
45

56
import edu.tamu.app.model.request.ServiceRequest;
67

78
@Entity
89
public class Idea extends AbstractIdea {
910

11+
@Column(nullable = false)
12+
private boolean elevated;
13+
1014
public Idea() {
1115
super();
16+
this.elevated = false;
1217
}
1318

1419
public Idea(String title, String description) {
1520
super(title, description);
21+
this.elevated = false;
1622
}
1723

1824
public Idea(ServiceRequest serviceRequest) {
@@ -21,10 +27,20 @@ public Idea(ServiceRequest serviceRequest) {
2127

2228
public Idea(String title, String description, User author) {
2329
super(title, description, author);
30+
this.elevated = false;
2431
}
2532

2633
public Idea(String title, String description, User author, Service service) {
2734
super(title, description, author, service);
35+
this.elevated = false;
36+
}
37+
38+
public boolean isElevated() {
39+
return elevated;
40+
}
41+
42+
public void setElevated(boolean elevated) {
43+
this.elevated = elevated;
2844
}
2945

3046
}

src/main/java/edu/tamu/app/model/repo/impl/FeatureProposalRepoImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import edu.tamu.app.model.Idea;
1313
import edu.tamu.app.model.User;
1414
import edu.tamu.app.model.repo.FeatureProposalRepo;
15+
import edu.tamu.app.model.repo.IdeaRepo;
1516
import edu.tamu.app.model.repo.UserRepo;
1617
import edu.tamu.app.model.repo.custom.FeatureProposalRepoCustom;
1718
import edu.tamu.weaver.auth.model.Credentials;
@@ -22,6 +23,9 @@ public class FeatureProposalRepoImpl implements FeatureProposalRepoCustom {
2223
@Autowired
2324
private UserRepo userRepo;
2425

26+
@Autowired
27+
private IdeaRepo ideaRepo;
28+
2529
@Autowired
2630
private FeatureProposalRepo featureProposalRepo;
2731

@@ -42,13 +46,17 @@ public FeatureProposal create(FeatureProposal featureProposal, Credentials crede
4246

4347
@Override
4448
public FeatureProposal create(Idea idea) {
45-
FeatureProposal featureProposal = featureProposalRepo.save(new FeatureProposal(idea));
49+
idea.setElevated(true);
50+
idea = ideaRepo.save(idea);
51+
simpMessagingTemplate.convertAndSend("/channel/ideas/update", new ApiResponse(SUCCESS, idea));
52+
FeatureProposal featureProposal = featureProposalRepo.save(new FeatureProposal(idea));
4653
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/create", new ApiResponse(SUCCESS, featureProposal));
4754
return featureProposal;
4855
}
4956

5057
@Override
5158
public FeatureProposal update(FeatureProposal featureProposal) {
59+
featureProposal = featureProposalRepo.save(featureProposal);
5260
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/update", new ApiResponse(SUCCESS, featureProposal));
5361
return featureProposal;
5462
}

src/main/java/edu/tamu/app/model/repo/impl/IdeaRepoImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Idea create(Idea idea, Credentials credentials) throws UserNotFoundExcept
4141

4242
@Override
4343
public Idea update(Idea idea) {
44+
idea = ideaRepo.save(idea);
4445
simpMessagingTemplate.convertAndSend("/channel/ideas/update", new ApiResponse(SUCCESS, idea));
4546
return idea;
4647
}

src/main/java/edu/tamu/app/model/request/FeatureRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.tamu.app.model.request;
22

3+
import edu.tamu.app.model.FeatureProposal;
4+
35
public class FeatureRequest extends AbstractRequest {
46

57
private static final long serialVersionUID = -6953745180846929244L;
@@ -23,6 +25,10 @@ public FeatureRequest(RequestType type, String title, String description, Long p
2325
this.projectId = projectId;
2426
}
2527

28+
public FeatureRequest(FeatureProposal proposal) {
29+
this(RequestType.FEATURE, proposal.getTitle(), proposal.getDescription(), proposal.getService().getProjectId());
30+
}
31+
2632
public Long getProjectId() {
2733
return projectId;
2834
}

src/main/java/edu/tamu/app/service/ProjectService.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package edu.tamu.app.service;
22

3+
import static edu.tamu.weaver.response.ApiStatus.SUCCESS;
4+
35
import java.io.IOException;
46
import java.net.MalformedURLException;
57
import java.net.URL;
68

79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.beans.factory.annotation.Value;
11+
import org.springframework.messaging.simp.SimpMessagingTemplate;
912
import org.springframework.stereotype.Service;
1013
import org.springframework.web.client.RestTemplate;
1114

1215
import com.fasterxml.jackson.core.JsonParseException;
1316
import com.fasterxml.jackson.databind.JsonMappingException;
1417
import com.fasterxml.jackson.databind.ObjectMapper;
1518

19+
import edu.tamu.app.model.FeatureProposal;
20+
import edu.tamu.app.model.repo.FeatureProposalRepo;
1621
import edu.tamu.app.model.request.FeatureRequest;
1722
import edu.tamu.app.model.request.IssueRequest;
18-
import edu.tamu.weaver.response.ApiResponse;;
23+
import edu.tamu.weaver.response.ApiResponse;
24+
import edu.tamu.weaver.response.ApiStatus;;
1925

2026
@Service
2127
public class ProjectService {
@@ -29,6 +35,12 @@ public class ProjectService {
2935
@Autowired
3036
private RestTemplate restTemplate;
3137

38+
@Autowired
39+
private FeatureProposalRepo featureProposalRepo;
40+
41+
@Autowired
42+
private SimpMessagingTemplate simpMessagingTemplate;
43+
3244
public ApiResponse getAll() throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
3345
return objectMapper.readValue(new URL(projectsUrl), ApiResponse.class);
3446
}
@@ -37,8 +49,14 @@ public ApiResponse getById(Long id) throws JsonParseException, JsonMappingExcept
3749
return objectMapper.readValue(new URL(projectsUrl + "/" + id), ApiResponse.class);
3850
}
3951

40-
public ApiResponse submitFeatureRequest(FeatureRequest request) {
41-
return restTemplate.postForObject(projectsUrl + "/feature", request, ApiResponse.class);
52+
public ApiResponse submitFeatureRequest(FeatureProposal proposal) {
53+
ApiResponse response = restTemplate.postForObject(projectsUrl + "/feature", new FeatureRequest(proposal), ApiResponse.class);
54+
if (response.getMeta().getStatus().equals(ApiStatus.SUCCESS)) {
55+
proposal.setSubmitted(true);
56+
proposal = featureProposalRepo.save(proposal);
57+
simpMessagingTemplate.convertAndSend("/channel/feature-proposals/update", new ApiResponse(SUCCESS, proposal));
58+
}
59+
return response;
4260
}
4361

4462
public ApiResponse submitIssueRequest(IssueRequest request) {

0 commit comments

Comments
 (0)