Permalink
Browse files

Contest Service and Resource added.

  • Loading branch information...
1 parent b14e186 commit a2a92a997813c8b9cfa86d9c28c77cd9bd5a4b73 @timblommerde timblommerde committed Jan 26, 2012
View
1 domain/src/main/java/nl/jpoint/top2k/guice/ServiceModule.java
@@ -11,5 +11,6 @@ protected void configure() {
bind(IMailService.class).to(MailService.class);
bind(IArtistService.class).to(ArtistService.class);
bind(ITrackService.class).to(TrackService.class);
+ bind(IContestService.class).to(ContestService.class);
}
}
View
55 domain/src/main/java/nl/jpoint/top2k/service/ContestService.java
@@ -0,0 +1,55 @@
+package nl.jpoint.top2k.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import javax.inject.Inject;
+
+import nl.jpoint.top2k.domain.Artist;
+import nl.jpoint.top2k.domain.Track;
+import nl.jpoint.top2k.domain.User;
+
+/**
+ * Default implementation of the {@link IContestService}.
+ */
+public class ContestService implements IContestService {
+
+ @Inject
+ private IUserService userService;
+ @Inject
+ private ITrackService trackService;
+
+ private final Random random = new Random();
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public List<Track> getTracksForContest() {
+ List<Track> tracks = new ArrayList<Track>();
+
+ List<Long> validIds = trackService.getValidTrackIds();
+ tracks.add(trackService.getById(getRandomIdFrom(validIds)));
+ tracks.add(trackService.getById(getRandomIdFrom(validIds)));
+ return tracks;
+ }
+
+ /**
+ * Returns a random id from the provided valid ids.
+ * @param validIds list of valid ids.
+ * @return
+ */
+ private long getRandomIdFrom(final List<Long> validIds) {
+ int idIndex = random.nextInt(validIds.size());
+ return validIds.get(idIndex);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void registerContestResult(Track trackOne, Track trackTwo, int result, User user) {
+ }
+
+}
View
18 domain/src/main/java/nl/jpoint/top2k/service/IContestService.java
@@ -0,0 +1,18 @@
+package nl.jpoint.top2k.service;
+
+import java.util.List;
+
+import nl.jpoint.top2k.domain.Track;
+import nl.jpoint.top2k.domain.User;
+
+/**
+ * Contest Service that provides the methods for the actual contest. It provides a method to get the tracks for a
+ * contest and a method to register the result of a Contest.
+ */
+public interface IContestService {
+
+ List<Track> getTracksForContest();
+
+ void registerContestResult(Track trackOne, Track trackTwo, int result, User user);
+
+}
View
5 domain/src/main/java/nl/jpoint/top2k/service/ITrackService.java
@@ -33,4 +33,9 @@
*/
Track getById(long id);
+ /**
+ * Returns a list of all valid track ids.
+ * @return a list of all valid track ids.
+ */
+ List<Long> getValidTrackIds();
}
View
4 domain/src/main/java/nl/jpoint/top2k/service/TrackService.java
@@ -41,4 +41,8 @@ public Track getById(final long id) {
return provider.get().find(Track.class, id);
}
+ @Override
+ public List<Long> getValidTrackIds() {
+ return provider.get().createQuery("SELECT t.id FROM Track t WHERE t.artist IS NOT NULL").getResultList();
+ }
}
View
6 web/src/main/java/nl/jpoint/top2k/guice/WebModule.java
@@ -3,10 +3,7 @@
import com.google.inject.persist.PersistFilter;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
-import nl.jpoint.top2k.rest.ArtistResource;
-import nl.jpoint.top2k.rest.RegisterResource;
-import nl.jpoint.top2k.rest.TrackResource;
-import nl.jpoint.top2k.rest.VersionResource;
+import nl.jpoint.top2k.rest.*;
import java.util.HashMap;
@@ -19,6 +16,7 @@ protected void configureServlets() {
bind(RegisterResource.class);
bind(ArtistResource.class);
bind(TrackResource.class);
+ bind(ContestResource.class);
serve("/rest/*").with(GuiceContainer.class, new HashMap<String, String>());
}
}
View
38 web/src/main/java/nl/jpoint/top2k/rest/ContestResource.java
@@ -0,0 +1,38 @@
+package nl.jpoint.top2k.rest;
+
+import java.util.List;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.ws.rs.*;
+import javax.ws.rs.core.GenericEntity;
+import javax.ws.rs.core.MediaType;
+
+import com.google.inject.persist.Transactional;
+import com.sun.jersey.api.json.JSONWithPadding;
+import nl.jpoint.top2k.domain.Track;
+import nl.jpoint.top2k.domain.User;
+import nl.jpoint.top2k.service.IContestService;
+import nl.jpoint.top2k.service.IMailService;
+import nl.jpoint.top2k.service.ITrackService;
+import nl.jpoint.top2k.service.IUserService;
+
+
+@Singleton
+@Path("/contest")
+public class ContestResource {
+
+ @Inject
+ private IUserService userService;
+ @Inject
+ private IContestService contestService;
+
+
+ @GET
+ @Path("/new")
+ @Produces({"application/x-javascript", MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
+ public JSONWithPadding newContest(@QueryParam("jsoncallback") @DefaultValue("fn") final String callback) {
+ final List<Track> tracks = contestService.getTracksForContest();
+ return new JSONWithPadding(new GenericEntity<List<Track>>(tracks) { }, callback);
+ }
+
+}

0 comments on commit a2a92a9

Please sign in to comment.