<commit_before>/*
 * Copyright (C) 2013  <NAME>, <NAME>,
 *                     <NAME>, <NAME>, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.car.server.rest.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import io.car.server.core.entities.User;
import io.car.server.core.entities.Users;
import io.car.server.core.exception.UserNotFoundException;
import io.car.server.rest.MediaTypes;
import io.car.server.rest.Schemas;
import io.car.server.rest.auth.Authenticated;
import io.car.server.rest.validation.Schema;

/**
 * @author <NAME> <<EMAIL>>
 */
public class FriendsResource extends AbstractResource {
    public static final String FRIEND = "{friend}";
    private final User user;

    @Inject
    public FriendsResource(@Assisted User user) {
        this.user = user;
    }

    @GET
    @Schema(response = Schemas.USERS)
    @Produces(MediaTypes.USERS)
    public Users get() {
        return user.getFriends();
    }

    @POST
    @Authenticated
    @Schema(request = Schemas.USER_REF)
    @Consumes(MediaTypes.USER_REF)
    public void add(User friend) throws UserNotFoundException {
        if (friend.getName() == null) {
            throw new WebApplicationException(Status.BAD_REQUEST);
        }
        getService().addFriend(user, friend);
    }

    @Path(FRIEND)
    public UserResource friend(@PathParam("friend") String friendName) throws
            UserNotFoundException {
        User friend = getService().getUser(friendName);<commit_msg>users can not be friends of them self<commit_after>/*
 * Copyright (C) 2013  <NAME>, <NAME>,
 *                     <NAME>, <NAME>, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.car.server.rest.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import io.car.server.core.entities.User;
import io.car.server.core.entities.Users;
import io.car.server.core.exception.UserNotFoundException;
import io.car.server.rest.MediaTypes;
import io.car.server.rest.Schemas;
import io.car.server.rest.auth.Authenticated;
import io.car.server.rest.validation.Schema;

/**
 * @author <NAME> <<EMAIL>>
 */
public class FriendsResource extends AbstractResource {
    public static final String FRIEND = "{friend}";
    private final User user;

    @Inject
    public FriendsResource(@Assisted User user) {
        this.user = user;
    }

    @GET
    @Schema(response = Schemas.USERS)
    @Produces(MediaTypes.USERS)
    public Users get() {
        return user.getFriends();
    }

    @POST
    @Authenticated
    @Schema(request = Schemas.USER_REF)
    @Consumes(MediaTypes.USER_REF)
    public void add(User friend) throws UserNotFoundException {
        if (friend.getName() == null ||
            friend.getName().equals(getCurrentUser())) {
            throw new WebApplicationException(Status.BAD_REQUEST);
        }
        getService().addFriend(user, getService().getUser(friend.getName()));
    }

    @Path(FRIEND)
    public UserResource friend(@PathParam("friend") String friendName) throws
            UserNotFoundException {
        User friend = getService().getUser(friendName);
{'fraction_non_alphanumeric': 0.10517766497461929, 'fraction_numerical': 0.0020304568527918783, 'mean_word_length': 3.502742230347349, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5452366', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1411, 'n_words': 429}