Skip to content

Commit

Permalink
Contest Service and Resource added.
Browse files Browse the repository at this point in the history
  • Loading branch information
timblommerde committed Jan 26, 2012
1 parent b14e186 commit a2a92a9
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
55 changes: 55 additions & 0 deletions domain/src/main/java/nl/jpoint/top2k/service/ContestService.java
Original file line number Diff line number Diff line change
@@ -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) {
}

}
18 changes: 18 additions & 0 deletions domain/src/main/java/nl/jpoint/top2k/service/IContestService.java
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public interface ITrackService {
*/
Track getById(long id);

/**
* Returns a list of all valid track ids.
* @return a list of all valid track ids.
*/
List<Long> getValidTrackIds();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
6 changes: 2 additions & 4 deletions web/src/main/java/nl/jpoint/top2k/guice/WebModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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>());
}
}
38 changes: 38 additions & 0 deletions web/src/main/java/nl/jpoint/top2k/rest/ContestResource.java
Original file line number Diff line number Diff line change
@@ -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.