-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b14e186
commit a2a92a9
Showing
7 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
domain/src/main/java/nl/jpoint/top2k/service/ContestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
domain/src/main/java/nl/jpoint/top2k/service/IContestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
web/src/main/java/nl/jpoint/top2k/rest/ContestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |