package com.yammer.stresstime.resources;

import com.yammer.stresstime.auth.Authorize;
import com.yammer.stresstime.auth.Role;
import com.yammer.stresstime.entities.Employee;
import com.yammer.stresstime.entities.Group;
import com.yammer.stresstime.entities.User;
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.List;

@Path("/groups")
@Produces(MediaType.APPLICATION_JSON)
public class GroupsResource {

    private GroupManager groupManager;

    public GroupsResource(GroupManager groupManager) {
        this.groupManager = groupManager;
    }

    @GET
    @UnitOfWork
    public Response getGroups(
            @Authorize({Role.ADMIN, Role.MEMBER, Role.GUEST}) User user) {

        List<Group> groups = groupManager.all();
        Employee employee = user.getEmployee();
        for (Group group : groups) {
            group.setAnnotationProperty("isMember", employee != null && group.isMember(employee));
            group.setAnnotationProperty("isAdmin", employee != null && group.isAdmin(employee));
        }
        return Response.ok().entity(groups).build();
    }

    @POST
    @UnitOfWork
    public Response createGroup(
            @Authorize({Role.ADMIN}) User user,
            @FormParam("name") String name) {

        Group group = new Group(name);
        groupManager.save(group);
        return Response.ok().entity(group).build();
    }

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

        Group group = groupManager.getById(groupId);

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

        groupManager.delete(group);
        return Response.noContent().build();
    }


    @GET
    @Path("/{group_id}")
    @UnitOfWork
    public Response getGroup(
            @PathParam("group_id") long groupId) {

        Group group = groupManager.getById(groupId);
        return Response.ok().entity(group).build();
    }
}

@@ -511,20 +511,85 @@
 va.util.
-List
+Collection;%0Aimport java.util.List;%0Aimport java.util.stream.Collectors
 ;%0A%0A@Path
@@ -1006,290 +1006,56 @@
     
-Employee employee = user.getEmployee();%0A        for (Group group : groups) %7B%0A            group.setAnnotationProperty(%22isMember%22, employee != null && group.isMember(employee));%0A            group.setAnnotationProperty(%22isAdmin%22, employee != null && group.isAdmin(employee));%0A        %7D
+List%3CGroup%3E response = wrapGroups(groups, user);
 %0A   
@@ -1087,22 +1087,24 @@
 .entity(
-groups
+response
 ).build(
@@ -1332,32 +1332,81 @@
 er.save(group);%0A
+        Group response = wrapGroup(group, user);%0A
         return R
@@ -1417,37 +1417,40 @@
 nse.ok().entity(
-group
+response
 ).build();%0A    %7D
@@ -1949,24 +1949,97 @@
 e getGroup(%0A
+            @Authorize(%7BRole.ADMIN, Role.MEMBER, Role.GUEST%7D) User user,%0A
             
@@ -2143,49 +2143,635 @@
     
-return Response.ok().entity(group).build(
+Group response = wrapGroup(group, user);%0A        return Response.ok().entity(response).build();%0A    %7D%0A%0A    private Group wrapGroup(Group group, User user) %7B%0A        Employee employee = user.getEmployee();%0A        group.setAnnotationProperty(%22isMember%22, employee != null && group.isMember(employee));%0A        group.setAnnotationProperty(%22isAdmin%22, employee != null && group.isAdmin(employee));%0A        return group;%0A    %7D%0A%0A    private List%3CGroup%3E wrapGroups(Collection%3C? extends Group%3E groups, User user) %7B%0A        return groups.stream()%0A                .map(g -%3E wrapGroup(g, user))%0A                .collect(Collectors.toList()
 );%0A 

Add new group which wasn't being shown in group list