Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UN-202 Changes #79

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/main/java/com/nicusa/assembler/FeatureAssembler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nicusa.assembler;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;

import com.nicusa.controller.FeatureController;
import com.nicusa.controller.VoteController;
import com.nicusa.domain.Feature;
import com.nicusa.domain.Vote;
import com.nicusa.resource.FeatureResource;

@Component
public class FeatureAssembler extends ResourceAssemblerSupport<Feature, FeatureResource> {

public FeatureAssembler() {
super(FeatureController.class, FeatureResource.class);
}

@Override
public FeatureResource toResource(Feature feature) {
FeatureResource featureResource = createResourceWithId(feature.getId(), feature);
featureResource.setTitle(feature.getTitle());
featureResource.setRequestText(feature.getRequestText());

Integer numberOfLikes = 0;
Integer numberOfDislikes = 0;
for (Vote current : feature.getVotes()) {
if (current.getLike()) {
numberOfLikes++;
} else {
numberOfDislikes++;
}
}

featureResource.setNumberOfLikes(numberOfLikes);
featureResource.setNumberOfDislikes(numberOfDislikes);

featureResource.add(linkTo(methodOn(VoteController.class).voteLike(featureResource)).withRel("voteLike"));
featureResource.add(linkTo(methodOn(VoteController.class).voteDislike(featureResource)).withRel("voteDislike"));
return featureResource;
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/nicusa/assembler/VoteAssembler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.nicusa.assembler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.mvc.ResourceAssemblerSupport;
import org.springframework.stereotype.Component;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import com.nicusa.controller.FeatureController;
import com.nicusa.controller.VoteController;
import com.nicusa.domain.Vote;
import com.nicusa.resource.VoteResource;

@Component
public class VoteAssembler extends ResourceAssemblerSupport<Vote, VoteResource> {
@Autowired
private UserProfileAssembler userProfileAssembler;

public VoteAssembler() {
super(VoteController.class, VoteResource.class);
}

public VoteResource toResource(Vote vote) {
VoteResource voteResource = createResourceWithId(vote.getId(), vote);
voteResource.setLike(vote.getLike());
if (vote.getUserProfile() != null) {
voteResource.setUserProfileResource(userProfileAssembler.toResource(vote.getUserProfile()));
}
voteResource.add(linkTo(methodOn(FeatureController.class).getFeature(vote.getFeature().getId())).withRel("feature"));
return voteResource;
}

}
53 changes: 53 additions & 0 deletions src/main/java/com/nicusa/controller/FeatureController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.nicusa.controller;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.nicusa.assembler.FeatureAssembler;
import com.nicusa.domain.Feature;
import com.nicusa.resource.FeatureResource;

@RestController
public class FeatureController {

@PersistenceContext
private EntityManager entityManager;

@Autowired
private FeatureAssembler featureAssembler;

@ResponseBody
@RequestMapping(value = "/feature/{id}", method = RequestMethod.GET, produces = "application/hal+json")
public ResponseEntity<FeatureResource> getFeature(@PathVariable("id") Long id) {
Feature feature = entityManager.find(Feature.class, id);
if (feature == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(featureAssembler.toResource(feature), HttpStatus.OK);
}

@ResponseBody
@RequestMapping(value = "/features", method = RequestMethod.GET, produces = "application/hal+json")
public ResponseEntity<List<FeatureResource>> getFeatures() {
Query query = entityManager.createQuery("SELECT f FROM Feature f");
List<Feature> features = query.getResultList();

if (features == null || features.size() == 0) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

return new ResponseEntity<>(featureAssembler.toResources(features), HttpStatus.OK);
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/nicusa/controller/VoteController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.nicusa.controller;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.nicusa.assembler.UserProfileAssembler;
import com.nicusa.assembler.VoteAssembler;
import com.nicusa.converter.VoteResourceToDomainConverter;
import com.nicusa.domain.UserProfile;
import com.nicusa.domain.Vote;
import com.nicusa.resource.FeatureResource;
import com.nicusa.resource.VoteResource;

@RestController
public class VoteController {
@PersistenceContext
private EntityManager entityManager;

@Autowired
private VoteAssembler voteAssembler;

@Autowired
private UserProfileAssembler userProfileAssembler;

@Autowired
private VoteResourceToDomainConverter voteResourceToDomainConverter;

@Autowired
private SecurityController securityController;

@ResponseBody
@RequestMapping(value = "/vote/{id}", method = RequestMethod.GET, produces = "application/hal+json")
public ResponseEntity<VoteResource> getVote(@PathVariable("id") Long id) {
Vote vote = entityManager.find(Vote.class, id);
if (vote == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(voteAssembler.toResource(vote), HttpStatus.OK);
}

@ResponseBody
@RequestMapping(value = "/vote-like", method = RequestMethod.POST, produces = "application/hal+json")
public ResponseEntity<VoteResource> voteLike(FeatureResource featureResource) {
VoteResource voteResource = createVoteResource(Boolean.TRUE);
entityManager.persist(voteResourceToDomainConverter.convert(voteResource));
return new ResponseEntity<>(voteResource, HttpStatus.OK);
}

@ResponseBody
@RequestMapping(value = "/vote-dislike", method = RequestMethod.POST, produces = "application/hal+json")
public ResponseEntity<VoteResource> voteDislike(FeatureResource featureResource) {
VoteResource voteResource = createVoteResource(Boolean.FALSE);
entityManager.persist(voteResourceToDomainConverter.convert(voteResource));
return new ResponseEntity<>(voteResource, HttpStatus.OK);
}

private VoteResource createVoteResource(Boolean like) {
VoteResource voteResource = new VoteResource();
voteResource.setLike(like);

Long userProfileId = securityController.getAuthenticatedUserProfileId();
UserProfile userProfile = entityManager.find(UserProfile.class, userProfileId);
if (userProfile != null) {
voteResource.setUserProfileResource(userProfileAssembler.toResource(userProfile));
}

return voteResource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nicusa.converter;

/* import com.nicusa.controller.FeatureContoller; */
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

import com.nicusa.controller.FeatureController;
import com.nicusa.domain.Feature;
import com.nicusa.resource.FeatureResource;

@Component
public class FeatureResourceToDomainConverter extends ResourceToDomainConverter<FeatureResource, Feature> {

@PersistenceContext
private EntityManager entityManager;

@Override
public Feature convert(FeatureResource featureResource) {
Feature feature = entityManager.find(Feature.class, extractIdFromLink(FeatureController.class,
featureResource, "getFeature", Long.class));

if (feature == null) {
feature = new Feature();
}

feature.setTitle(featureResource.getTitle());
feature.setRequestText(featureResource.getRequestText());
return feature;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class UserProfileResourceToDomainConverter extends ResourceToDomainConver
@Override
public UserProfile convert(UserProfileResource userProfileResource) {
UserProfile userProfile = entityManager.find(UserProfile.class, extractIdFromLink(UserProfileController.class,
userProfileResource, "getUserProfile", Long.class));
userProfileResource, "getUserProfile", Long.class));
if (userProfile == null) {
userProfile = new UserProfile();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nicusa.converter;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nicusa.controller.VoteController;
import com.nicusa.domain.Vote;
import com.nicusa.resource.FeatureResource;
import com.nicusa.resource.VoteResource;

@Component
public class VoteResourceToDomainConverter extends ResourceToDomainConverter<VoteResource, Vote> {
@PersistenceContext
private EntityManager entityManager;

@Autowired
private FeatureResourceToDomainConverter featureConverter;

@Override
public Vote convert(VoteResource voteResource) {
Vote vote = entityManager.find(Vote.class, extractIdFromLink(VoteController.class,
voteResource, "getVote", Long.class));

if (vote == null) {
vote = new Vote();
}

vote.setLike(voteResource.getLike());

FeatureResource featureResource = new FeatureResource();
featureResource.add(voteResource.getLink("feature").withRel("self"));
vote.setFeature(featureConverter.convert(featureResource));

return vote;
}

}
51 changes: 51 additions & 0 deletions src/main/java/com/nicusa/domain/Feature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.nicusa.domain;

import javax.persistence.*;
import java.util.Collection;

@Entity
public class Feature {

public static final String SEQUENCE_NAME = "FEATURE_SEQUENCE";

private Long id;
private String title;
private String requestText;
private Collection<Vote> votes;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = SEQUENCE_NAME, allocationSize = 1)
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getRequestText() {
return requestText;
}

public void setRequestText(String requestText) {
this.requestText = requestText;
}

public Collection<Vote> getVotes() {
return votes;
}

public void setVotes(Collection<Vote> votes) {
this.votes = votes;
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/nicusa/domain/UserProfile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserProfile {
private String emailAddress;
private Portfolio portfolio;
private Collection<NotificationSetting> notificationSettings;
private Collection<Vote> votes;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Expand Down Expand Up @@ -66,4 +67,12 @@ public Collection<NotificationSetting> getNotificationSettings() {
public void setNotificationSettings(Collection<NotificationSetting> notificationSettings) {
this.notificationSettings = notificationSettings;
}

public Collection<Vote> getVotes() {
return votes;
}

public void setVotes(Collection<Vote> votes) {
this.votes = votes;
}
}
Loading