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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.client.*;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.MediaType;

import java.util.*;

public class Pairs {
	private Map<String, Pair> value = new HashMap<>();
	private Client client = ClientBuilder.newClient();
	public Map<String, Pair> getValue() {
		return new HashMap<>(this.value);
	}
	public Pair getPair(String pid) {
		return this.value.get(pid);
	}
	public void updateFromDeletedFriendPair(String deletedFriendPair_json) throws JsonProcessingException {
		Map<String, Object> deletedFriendPair = new HashMap<>();
		{
			Map<String, Object> i = new ObjectMapper().readValue(deletedFriendPair_json, HashMap.class);
			deletedFriendPair = i;
		}
		Map<String, Pair> temp_if39;
		if (this.value.containsKey(deletedFriendPair.get("pairId"))) {
			this.value.remove(deletedFriendPair.get("pairId"));
			temp_if39 = this.value;
		} else {
			temp_if39 = this.value;
		}
		this.value = temp_if39;
	}
	public void postFriend(Friends friends, String user1Id, String user0Id) throws JsonProcessingException {
		String pairId = client.target("http://localhost:8080").path("/pairId").request().get(String.class);
		Map<String, Map<String, Object>> users_json = client.target("http://localhost:8080").path("/users").request().get(HashMap.class);
		Map<String, Map<String, Object>> users = new HashMap<>();
		users = users_json;
		Map<String, Pair> temp_if42;
		if (user0Id.equals(user1Id)) {
			temp_if42 = this.value;
		} else {
			Map<String, Pair> temp_if41;
			if ((users.containsKey(user0Id)&&users.containsKey(user1Id))) {
				this.value.put(pairId,new Pair(pairId, user0Id, user1Id, friends));
				temp_if41 = this.value;
			} else {
				temp_if41 = this.value;
			}
			temp_if42 = temp_if41;
		}
		this.value = temp_if42;
		Form form = new Form();
		form.param("pid", pairId);
		form.param("pair", new ObjectMapper().writeValueAsString(getPair(pairId).getValue()));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/users/"+user0Id+"/friends").request().post(entity, String.class);
		form = new Form();
		form.param("pid", pairId);
		form.param("pair", new ObjectMapper().writeValueAsString(getPair(pairId).getValue()));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path("/users/"+user1Id+"/friends").request().post(entity, String.class);
		form = new Form();
		form.param("pid", pairId);
		form.param("pair", new ObjectMapper().writeValueAsString(getPair(pairId).getValue()));
		entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		result = client.target("http://localhost:8080").path("/pairId").request().post(entity, String.class);
	}
}