package com.yammer.stresstime.resources;

import com.yammer.stresstime.auth.Authorize;
import com.yammer.stresstime.auth.Role;
import com.yammer.stresstime.entities.AssignmentType;
import com.yammer.stresstime.entities.Group;
import com.yammer.stresstime.entities.User;
import com.yammer.stresstime.managers.AssignmentTypeManager;
import com.yammer.stresstime.managers.GroupManager;
import com.yammer.stresstime.utils.ResourceUtils;
import io.dropwizard.hibernate.UnitOfWork;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Set;

@Path("/groups/{group_id}/assignment-types")
@Produces(MediaType.APPLICATION_JSON)
public class AssignmentTypesResource {

    private GroupManager groupManager;
    private AssignmentTypeManager assignmentTypeManager;

    public AssignmentTypesResource(
            AssignmentTypeManager assignmentTypeManager,
            GroupManager groupManager) {

        this.assignmentTypeManager = assignmentTypeManager;
        this.groupManager = groupManager;
    }

    @POST
    @UnitOfWork
    public Response createAssignmentType(
            @Authorize({Role.ADMIN, Role.MEMBER}) User user,
            @PathParam("group_id") long groupId,
            @FormParam("name") String name,
            @FormParam("description") String description) {

        Group group = groupManager.getById(groupId);

        ResourceUtils.checkGroupAdminOrGlobalAdmin(group, user.getEmployee());

        AssignmentType assignmentType = new AssignmentType(name, group);
        assignmentType.setDescription(description);
        assignmentTypeManager.save(assignmentType);
        return Response.ok().entity(assignmentType).build();
    }

    @GET
    @UnitOfWork
    public Response getGroupAssignmentTypes(
            @PathParam("group_id") long groupId) {

        Group group = groupManager.getById(groupId);
        Set<AssignmentType> assignmentTypes = group.getAssignmentTypes();
        return Response.ok().entity(assignmentTypes).build();
    }

    @DELETE
    @Path("/{assignment_type_id}")
    @UnitOfWork
    public Response deleteAssignmentType(
            @Authorize({Role.ADMIN, Role.MEMBER}) User user,
            @PathParam("group_id") long groupId,
            @PathParam("assignment_type_id") long assignmentTypeId) {

        AssignmentType assignmentType = assignmentTypeManager.getById(assignmentTypeId);
        Group group = assignmentType.getGroup();

        ResourceUtils.checkGroupAdminOrGlobalAdmin(group, user.getEmployee());
        ResourceUtils.checkConflictFree(group.getId() == groupId, Group.class);

        assignmentTypeManager.delete(assignmentType);
        return Response.noContent().build();
    }
}

@@ -2717,11 +2717,797 @@
 );%0A    %7D
+%0A%0A    @POST%0A    @Path(%22/%7Bassignment_type_id%7D%22)%0A    @UnitOfWork%0A    public Response updateAssignmentType(%0A            @Authorize(%7BRole.ADMIN, Role.MEMBER%7D) User user,%0A            @PathParam(%22group_id%22) long groupId,%0A            @PathParam(%22assignment_type_id%22) long assignmentTypeId,%0A            @FormParam(%22name%22) String name) %7B%0A%0A        AssignmentType assignmentType = assignmentTypeManager.getById(assignmentTypeId);%0A        Group group = assignmentType.getGroup();%0A%0A        ResourceUtils.checkGroupAdminOrGlobalAdmin(group, user.getEmployee());%0A        ResourceUtils.checkConflictFree(group.getId() == groupId, Group.class);%0A%0A        assignmentType.setName(name);%0A%0A        assignmentTypeManager.save(assignmentType);%0A        return Response.ok().entity(assignmentType).build();%0A    %7D
 %0A%7D%0A

Create endpoint to update assignment type