예제 #1
0
        public IActionResult CreateRoom(string name)
        {
            // Check if name is empty
            if (name == null)
            {
                return(GetResponse(true, "No name specified"));
            }

            // Check if shorter than 3 chars
            if (name.Length < 3)
            {
                return(GetResponse(true, "Name too short"));
            }

            // Check if longer than 32 chars
            if (name.Length > 32)
            {
                return(GetResponse(true, "Name too long"));
            }

            // Format name
            // TODO: Format emojis etc.
            var id = name.ToLower().Replace(" ", "");

            // Check if name (id) is already taken
            if (System.IO.File.Exists($"data/rooms/{id}.json"))
            {
                return(GetResponse(true, "Name already taken"));
            }

            // Try to save it
            var room = new Room
            {
                Id    = id,
                Name  = name,
                Owner = CookieManager.GetUserValue(HttpContext, "Id")
            };

            try
            {
                System.IO.File.WriteAllText($"data/rooms/{id}.json", JsonConvert.SerializeObject(room));
            }
            catch (IOException)
            {
                return(GetResponse(true, "Invalid name"));
            }

            return(GetResponse(false, id));
        }