Beispiel #1
0
        public async Task <Room> AddRoom(NewRoomForm newRoom)
        {
            Room room = new Room {
                RoomName    = newRoom.RoomName,
                CreatedDate = DateTime.Now,
                RoomUsers   = new List <UserInRoomObject>()
                {
                },
                RoomMessages = new List <Message>()
                {
                }
            };

            await rooms.InsertOneAsync(room);

            return(room);
        }
Beispiel #2
0
        public async Task <ActionResult <Room> > CreateRoom([FromBody] NewRoomForm roomForm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { message = "Room form is not valid" }));
            }

            var groupNameExist = await roomRepository.GetRoomByRoomName(roomForm.RoomName);

            if (groupNameExist != null)
            {
                return(BadRequest(new { message = "Group Name is already used" }));
            }

            var room = await roomRepository.AddRoom(roomForm);

            await hubContext.Clients.All.SendAsync(KeyConstants.receiveNewRoom, room);

            return(CreatedAtRoute("GetRoom", new { roomId = room.Id.ToString() }, room));
        }
Beispiel #3
0
        public async Task <LoginResult> CreateNewRoomAsync(string roomName)
        {
            LoginResult result;

            var url = ChatRootUrl + NewRoomRoute;

            using var client  = new HttpClient();
            using var request = new HttpRequestMessage(HttpMethod.Post, url);

            var content     = new NewRoomForm(roomName);
            var jsonContent = JsonConvert.SerializeObject(content);

            using var stringContent = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            request.Content    = stringContent;
            using var response = await client.SendAsync(request).ConfigureAwait(false);

            var responseData = await response.Content.ReadAsStringAsync();

            if (response.StatusCode == HttpStatusCode.Created)
            {
                var newRoom = JsonConvert.DeserializeObject <Room>(responseData);

                result = new LoginResult(true, newRoom.Id);

                await JoinRoomAsync(newRoom.Id, newRoom.RoomName);
            }
            else
            {
                result = response.StatusCode == HttpStatusCode.BadRequest
                    ? new LoginResult(false, "Room already exists")
                    : new LoginResult(false, "Internal server error");
            }

            return(result);
        }