public async Task <ActionResult <ApiCreateRoom> > Get(string keyCode)
        {
            ApiCreateRoom apiCreateRoom = new ApiCreateRoom();
            RoomEntity    room          = _context.Rooms.Where(x => x.RoomKey == keyCode).FirstOrDefault();

            if (room == null)
            {
                return(NotFound());
            }

            apiCreateRoom.Id                   = room.Id;
            apiCreateRoom.Name                 = room.Name;
            apiCreateRoom.RoomKey              = room.RoomKey;
            apiCreateRoom.Owner_FK_Users_Id    = room.Owner_FK_Users_Id;
            apiCreateRoom.StreamingPlatformIDs = new List <int>();

            using (SqlConnection con = new SqlConnection("Data Source=MSI\\SQLExpress;Initial Catalog=StreamingTinder;Integrated Security=SSPI;"))
            {
                using (SqlCommand cmd = new SqlCommand("SelectRoomServices", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@RoomID", SqlDbType.Int).Value = apiCreateRoom.Id;

                    con.Open();
                    var reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        apiCreateRoom.StreamingPlatformIDs.Add(reader.GetInt32("Id"));
                    }
                }
            }

            return(apiCreateRoom);
        }
Ejemplo n.º 2
0
        private async Task <ApiCreateRoom> PutIUser(ApiCreateRoom room)
        {
            var        content = JsonConvert.SerializeObject(room);
            HttpClient _client = new HttpClient();

            var httpResponse = await _client.PostAsync(this.apiString + API_BASE, new StringContent(content, Encoding.Default, "application/json"));

            if (!httpResponse.IsSuccessStatusCode)
            {
                throw new Exception("Cannot create a room");
            }

            var createdTask = JsonConvert.DeserializeObject <ApiCreateRoom>(await httpResponse.Content.ReadAsStringAsync());

            return(createdTask);
        }
Ejemplo n.º 3
0
        private IRoom GetIRoomFromAPICreateResponse(ApiCreateRoom apiResponse)
        {
            Room room = new Room();

            room.Id                = apiResponse.Id;
            room.Name              = apiResponse.Name;
            room.RoomKey           = apiResponse.RoomKey;
            room.Owner             = _userService.GetUser(apiResponse.Owner_FK_Users_Id);
            room.StreamingServices = new List <IStreamingPlatform>();

            foreach (int streamingPlatformID in apiResponse.StreamingPlatformIDs)
            {
                room.StreamingServices.Add(_platformService.GetStreamingPlatformById(streamingPlatformID));
            }

            return(room);
        }
        public async Task <ActionResult <ApiCreateRoom> > Post([FromBody] ApiCreateRoom room)
        {
            ApiCreateRoom createdRoom = new ApiCreateRoom();
            RoomEntity    roomEntity  = new RoomEntity();

            roomEntity.Name              = room.Name;
            roomEntity.RoomKey           = room.RoomKey;
            roomEntity.Owner_FK_Users_Id = room.Owner_FK_Users_Id;

            var createdRoomEntity = this._context.Rooms.Add(roomEntity).Entity;

            this._context.SaveChanges();

            using (SqlConnection con = new SqlConnection("Data Source=MSI\\SQLExpress;Initial Catalog=StreamingTinder;Integrated Security=SSPI;"))
            {
                foreach (var platformID in room.StreamingPlatformIDs)
                {
                    using (SqlCommand cmd = new SqlCommand("AddStreamingServiceToRoom", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@roomID", SqlDbType.Int).Value             = createdRoomEntity.Id;
                        cmd.Parameters.Add("@StreamingServiceID", SqlDbType.Int).Value = platformID;

                        con.Open();
                        cmd.ExecuteScalar();
                        con.Close();
                    }
                }
            }



            createdRoom.Id                   = createdRoomEntity.Id;
            createdRoom.Name                 = createdRoomEntity.Name;
            createdRoom.RoomKey              = createdRoomEntity.RoomKey;
            createdRoom.Owner_FK_Users_Id    = createdRoomEntity.Owner_FK_Users_Id;
            createdRoom.StreamingPlatformIDs = room.StreamingPlatformIDs;

            return(createdRoom);
        }
Ejemplo n.º 5
0
        public IRoom Create(IRoom room)
        {
            ApiCreateRoom apiCreateRoom = new ApiCreateRoom();

            apiCreateRoom.Name                 = room.Name;
            apiCreateRoom.RoomKey              = room.RoomKey;
            apiCreateRoom.Owner_FK_Users_Id    = room.Owner.Id;
            apiCreateRoom.StreamingPlatformIDs = new List <int>();

            foreach (IStreamingPlatform streamingPlatform in room.StreamingServices)
            {
                apiCreateRoom.StreamingPlatformIDs.Add(streamingPlatform.Id);
            }

            ApiCreateRoom apiRoom = this.PutIUser(apiCreateRoom).Result;

            if (apiRoom == null)
            {
                throw new ArgumentNullException("apiRoom", "apiRoom must not be null. Invalid response received from API.");
            }



            Room returnedRoom = new Room();

            returnedRoom.Id                = apiRoom.Id;
            returnedRoom.Name              = apiRoom.Name;
            returnedRoom.RoomKey           = apiRoom.RoomKey;
            returnedRoom.Owner             = _userService.GetUser(apiRoom.Owner_FK_Users_Id);
            returnedRoom.StreamingServices = new List <IStreamingPlatform>();

            foreach (int streamingPlatformID in apiRoom.StreamingPlatformIDs)
            {
                returnedRoom.StreamingServices.Add(_platformService.GetStreamingPlatformById(streamingPlatformID));
            }

            return(returnedRoom);
        }