/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016  Khartec Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.web.endpoints.api;

import com.khartec.waltz.model.EntityReference;
import com.khartec.waltz.model.perspective.PerspectiveRating;
import com.khartec.waltz.model.perspective.PerspectiveRatingValue;
import com.khartec.waltz.service.perspective_rating.PerspectiveRatingService;
import com.khartec.waltz.web.endpoints.Endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spark.Request;
import spark.Response;

import java.io.IOException;
import java.util.Collection;

import static com.khartec.waltz.common.SetUtilities.fromArray;
import static com.khartec.waltz.web.WebUtilities.*;
import static com.khartec.waltz.web.endpoints.EndpointUtilities.getForList;
import static com.khartec.waltz.web.endpoints.EndpointUtilities.putForDatum;


@Service
public class PerspectiveRatingEndpoint implements Endpoint {

    private static final String BASE = mkPath("api", "perspective-rating");


    private final PerspectiveRatingService perspectiveRatingService;


    @Autowired
    public PerspectiveRatingEndpoint(PerspectiveRatingService perspectiveRatingService) {
        this.perspectiveRatingService = perspectiveRatingService;
    }


    @Override
    public void register() {
        String entityPath = mkPath(BASE, ":x", ":y", "entity", ":kind", ":id");
        getForList(entityPath, this::findForEntity);
        putForDatum(entityPath, this::updateForEntity);
    }


    private Collection<PerspectiveRating> findForEntity(Request request, Response z) {
        EntityReference ref = getEntityReference(request);
        long categoryX = getLong(request, "x");
        long categoryY = getLong(request, "y");
        return perspectiveRatingService.findForEntity(categoryX, categoryY, ref);
    }


    private int updateForEntity(Request request, Response z) throws IOException {
        String username = getUsername(request);
        EntityReference ref = getEntityReference(request);
        long categoryX = getLong(request, "x");
        long categoryY = getLong(request, "y");
        PerspectiveRatingValue values[] = readBody(request, PerspectiveRatingValue[].class);

        return perspectiveRatingService.updatePerspectiveRatings(categoryX, categoryY, ref, username, fromArray(values));
    }
}

@@ -965,16 +965,58 @@
 gValue;%0A
+import com.khartec.waltz.model.user.Role;%0A
 import c
@@ -1081,24 +1081,79 @@
 ingService;%0A
+import com.khartec.waltz.service.user.UserRoleService;%0A
 import com.k
@@ -1403,16 +1403,76 @@
 ction;%0A%0A
+import static com.khartec.waltz.common.Checks.checkNotNull;%0A
 import s
@@ -1951,16 +1951,67 @@
 ervice;%0A
+    private final UserRoleService userRoleService;%0A
 %0A%0A    @A
@@ -2105,19 +2105,255 @@
 gService
-) %7B
+, %0A                                     UserRoleService userRoleService) %7B%0A        checkNotNull(perspectiveRatingService, %22perspectiveRatingService cannot be null%22);%0A        checkNotNull(userRoleService, %22userRoleService cannot be null%22);%0A
 %0A       
@@ -2407,24 +2407,72 @@
 ingService;%0A
+        this.userRoleService = userRoleService;%0A
     %7D%0A%0A%0A    
@@ -3121,16 +3121,87 @@
 ption %7B%0A
+        requireRole(userRoleService, request, Role.CAPABILITY_EDITOR);%0A
         

Secure Perspective Rating Endpoint