Newer
Older
tampopo-server-dtram / src / main / java / org / example / tampoposerverdtram / resources / DeletedFriendPair.java
package org.example.tampoposerverdtram.resources;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import jakarta.ws.rs.*;
import jakarta.ws.rs.client.*;
import jakarta.ws.rs.core.*;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

@Path("/deletedFriendPair")
@Component
public class DeletedFriendPair {
	private Map<String, Object> value = new HashMap<>();
	private Client client = ClientBuilder.newClient();
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public Map<String, Object> getValue() {
		return new HashMap<>(this.value);
	}
	@PUT
	public void deleteFriend(@FormParam("id") String id) throws JsonProcessingException {
		String user0Id = client.target("http://localhost:8080").path("/friends/pairs/" + id + "/user0Id").request().get(String.class);
		String user1Id = client.target("http://localhost:8080").path("/friends/pairs/" + id + "/user1Id").request().get(String.class);
		Map<String, Object> temp_json0 = new HashMap<>();
		temp_json0.put("pairId", id);
		temp_json0.put("user0Id", user0Id);
		temp_json0.put("user1Id", user1Id);
		this.value = temp_json0;
		String result = client.target("http://localhost:8080").path("/users/"+user0Id+"/friends").queryParam("deletedFriendPair", URLEncoder.encode(new ObjectMapper().writeValueAsString(this.value), StandardCharsets.UTF_8)).request().delete(String.class);
		result = client.target("http://localhost:8080").path("/users/"+user1Id+"/friends").queryParam("deletedFriendPair", URLEncoder.encode(new ObjectMapper().writeValueAsString(this.value), StandardCharsets.UTF_8)).request().delete(String.class);
		Form form = new Form();
		form.param("deletedFriendPair", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path("/friends/users").request().post(entity, String.class);
		result = client.target("http://localhost:8080").path("/friends/pairs").queryParam("deletedFriendPair", URLEncoder.encode(new ObjectMapper().writeValueAsString(this.value), StandardCharsets.UTF_8)).request().delete(String.class);
	}
}