Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Contest Service and Resource added.

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

0 comments on commit a2a92a9

Please sign in to comment.
Something went wrong with that request. Please try again.