package org.carlson.hockeystats.api;

import org.carlson.hockeystats.api.utils.QuickResponse;
import org.carlson.hockeystats.domain.Team;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Objects;
import java.util.UUID;

/**
 * Created by ryancarlson on 5/4/15.
 */
@Path("/teams")
public class TeamResource
{
	@PersistenceContext(name = "hockeystatsDS")
	EntityManager entityManager;

	@GET
	@Path("/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getTeam(@PathParam("id") UUID teamId)
	{
		Team team = entityManager.find(Team.class, teamId);

		if(team == null)
		{
			return QuickResponse.notFound();
		}
		else
		{
			return QuickResponse.ok(team);
		}
	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response createTeam(Team team)
	{
		// make sure the team has an ID in case the client did not set it
		if(team.getId() == null) team.setId(UUID.randomUUID());

		entityManager.persist(team);

		return QuickResponse.created(String.format("/teams/%s", team.getId()));
	}



	@PUT
	@Path("/{teamId}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response updateTeam(@PathParam("teamId") UUID teamId, Team team)
	{
		if(!Objects.equals(teamId, team.getId()))
		{
			return QuickResponse
					.badRequest(String.format("Team ID %s in path did not match team ID in entity %s", teamId, team.getId()));
		}

		if(entityManager.getReference(Team.class, teamId) == null)
		{
			return QuickResponse.notFound();
		}

		entityManager.merge(team);

		return QuickResponse.noContent();
	}

	@GET
	@Path("/{teamId}/players")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getPlayersForTeam(@PathParam("teamId") UUID teamId)
	{
		Team team = entityManager.find(Team.class, teamId);

		if(team == null)
		{
			return QuickResponse.notFound();
		}

		return QuickResponse.ok(team.getPlayers());
	}
}

@@ -83,24 +83,70 @@
 ckResponse;%0A
+import org.carlson.hockeystats.domain.Player;%0A
 import org.c
@@ -2189,11 +2189,572 @@
 s());%0A%09%7D
+%0A%0A%09@POST%0A%09@Path(%22/%7BteamId%7D/players%22)%0A%09@Consumes(MediaType.APPLICATION_JSON)%0A%09public Response addPlayerToTeam(@PathParam(%22teamId%22) UUID teamId, Player player)%0A%09%7B%0A%09%09Team team = entityManager.find(Team.class, teamId);%0A%0A%09%09if(team == null)%0A%09%09%7B%0A%09%09%09return QuickResponse%0A%09%09%09%09%09.badRequest(String.format(%22Team with ID %25s does not exist%22, teamId));%0A%09%09%7D%0A%0A%09%09if(player.getId() == null) player.setId(UUID.randomUUID());%0A%09%09player.setTeam(team);%0A%0A%09%09team.getPlayers().add(player);%0A%0A%09%09return QuickResponse.created(String.format(%22/teams/%25s/players/%25s%22, teamId, player.getId()));%0A%09%7D
 %0A%7D%0A

Add endpoint to add player to team