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

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("/chatRooms")
@Component
public class ChatRooms {
	private Map<String, Map<String, String>> value = new HashMap<>();
	private Client client = ClientBuilder.newClient();
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public Map<String, Map<String, String>> getValue() {
		return new HashMap<>(this.value);
	}
	public Map<String, String> getChatRoom(String chatRoomId) {
		return this.value.get(chatRoomId);
	}
	@Path("/{chatRoomId}")
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public Map<String, String> getChatRoomValue(@PathParam("chatRoomId") String chatRoomId) {
		return getChatRoom(chatRoomId);
	}
	public String getChatRoom(String chatRoomId, String userId) {
		return this.value.get(chatRoomId).get(userId);
	}
	@Path("/{chatRoomId}/{userId}")
	@Produces(MediaType.APPLICATION_JSON)
	@GET
	public String getChatRoomValue(@PathParam("chatRoomId") String chatRoomId, @PathParam("userId") String userId) {
		return getChatRoom(chatRoomId,userId);
	}
	@Path("/{chatRoomId}/{userId}")
	@PUT
	public void addChatRoomMessage(@PathParam("userId") String userId, @PathParam("chatRoomId") String chatRoomId, @FormParam("message") String message) {
		this.value.get(chatRoomId).put(userId,message);
	}
	@POST
	public void postChatRoom(@FormParam("user1Id") String user1Id, @FormParam("user0Id") String user0Id) throws JsonProcessingException {
		String roomId = client.target("http://localhost:8080").path("/roomId").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, String> temp_nil17 = new HashMap<String, String>();
		temp_nil17.put(user0Id,null);
		temp_nil17.put(user1Id,null);
		this.value.put(roomId,temp_nil17);
		Form form = new Form();
		form.param("chatRooms", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/roomId").request().post(entity, String.class);
	}
	@DELETE
	public void deleteChatRoomMember(@FormParam("userId") String userId, @QueryParam("chatRoomId") String chatRoomId) throws JsonProcessingException {
		Map<String, Map<String, String>> temp_if34;
		if (this.value.get(chatRoomId).containsKey(userId)) {
			Map<String, Map<String, String>> temp_if33;
		if ((this.value.get(chatRoomId).size()>1)) {
			this.value.remove(chatRoomId);
		this.value.get(chatRoomId).remove(userId);
		this.value.put(chatRoomId,this.value.get(chatRoomId));
			temp_if33 = this.value;
		} else {
			this.value.remove(chatRoomId);
			temp_if33 = this.value;
		}
			temp_if34 = temp_if33;
		} else {
			temp_if34 = this.value;
		}
		this.value = temp_if34;
		Form form = new Form();
		form.param("chatRooms", new ObjectMapper().writeValueAsString(this.value));
		Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
		String result = client.target("http://localhost:8080").path("/roomId").request().post(entity, String.class);
	}
}