Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / StateRest.java
k-fujii on 3 Jun 2022 1 KB Keywords rest
package com.ntlab.irisserver.resources;

import org.springframework.stereotype.Component;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.models.RoomManager;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Component
@Path("/rooms")

public class StateRest {

    @GET//部屋の情報を入手

    @Path("/{rid}/state")

    public int getState(@PathParam("rid") String rid) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);

        if(room==null){
            var response = Response.status(Response.Status.NO_CONTENT);
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }

        return room.state; //部屋の情報を送る
    }
//----------------------------------------------------------------------------------------------------------------------------------
    @PUT//部屋の情報を変更する

    @Path("/{rid}/state")

    //rid,nickname,stateを引数として持ってくる
    public void putState(@PathParam("rid") String rid,@FormParam("nickname") String nickname,@FormParam("state") Integer state) {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);

        if(nickname.equals(room.ownerName)){ //nicknameがownerだった場合
            room.state = state; //部屋の情報を変更
        }
    }


}