Newer
Older
IrisServerWebSocket / src / main / java / com / ntlab / irisserver / resources / MemberRest.java
Kota on 8 Dec 2022 1 KB first commit
package com.ntlab.irisserver.resources;

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

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

@Component
@Path("/rooms")
public class MemberRest {
    // チーム変更・役職変更のメソッド
    @Path("/{rid}/members/{nickname}")
    @PUT
    public String putMember(@PathParam("rid") String rid, @PathParam("nickname") String nickname,
                            @FormParam("belongs") String belongs, @FormParam("is-master") boolean isMaster) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);

        //部屋の確認
        if (room != null) {
            //ニックネームの確認
            if (room.getMember(nickname) != null) {
                room.changeTeamAndMaster(nickname, belongs, isMaster);
            } else {
                //ニックネームがなければエラー
                var response = Response.status(Response.Status.NO_CONTENT);
                response.status(404).entity("ニックネームが存在しません");
                throw new WebApplicationException(response.build());
            }
        } else {
            //部屋がなければエラー
            var response = Response.status(Response.Status.NO_CONTENT);
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }

        return "変更しました";
    }

    // プレイヤーを退出させるメソッド
    @Path("/{rid}/members/{nickname}")
    @DELETE
    public String deleteMember(@PathParam("rid") String rid, @PathParam("nickname") String nickname) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        if (room != null && room.getMember(nickname) != null) {
            room.deleteMember(nickname);
        }

        return "退出しました";
    }
}