Newer
Older
IrisServerWebSocket / src / main / java / com / ntlab / irisserver / resources / RoomsRest.java
Kota on 17 Dec 2022 1 KB 12-17-08:00-Java17にした
package com.ntlab.irisserver.resources;
import com.ntlab.irisserver.entities.Member;
import com.ntlab.irisserver.entities.RoomJson;
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;
import java.util.List;

@Component
@Path("/rooms")
public class RoomsRest {

    @POST //部屋の作成,オーナーを設定
    @Path("/a")
    @Produces(MediaType.APPLICATION_JSON)
    public RoomJson makeRooms(@FormParam("nickname") String owner){
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.createRoom(owner);
        room.addMember(owner);
        RoomJson rj = new RoomJson(room);

        return rj;
    }

    @GET//部屋の情報を表示
    @Path("/{rid}")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Member> getRoomMember(@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.getMembers();
    }

    @DELETE//部屋の削除
    @Path("/{rid}")
    public void deleteRooms(@PathParam("rid") String rid){
        RoomManager rm = RoomManager.getInstance();
        rm.deleteRoom(rid);
    }

//--------------------------------------テスト用-------------------------------------------

    @GET //すべての部屋の情報を表示
    @Path("/aa")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Room> getRoomsInfo() {
        RoomManager rm = RoomManager.getInstance();
        rm.getRooms();
        List<Room> rms = rm.getRooms();

        return rms;
    }


}