public IEnumerator CreateGameProfile(string @namespace, string userId, string accessToken,
                                             GameProfileRequest gameProfile, ResultCallback <GameProfile> callback)
        {
            Report.GetFunctionLog(this.GetType().Name);
            Assert.IsNotNull(@namespace, "Can't create a game profile! namespace parameter is null!");
            Assert.IsNotNull(userId, "Can't create a game profile! userId parameter is null!");
            Assert.IsNotNull(accessToken, "Can't create a game profile! accessToken parameter is null!");
            Assert.IsNotNull(gameProfile, "Can't create a game profile! gameProfile parameter is null!");

            var request = HttpRequestBuilder
                          .CreatePost(this.baseUrl + "/public/namespaces/{namespace}/users/{userId}/profiles")
                          .WithPathParam("namespace", @namespace)
                          .WithPathParam("userId", userId)
                          .Accepts(MediaType.ApplicationJson)
                          .WithBearerAuth(accessToken)
                          .WithContentType(MediaType.ApplicationJson)
                          .WithBody(gameProfile.ToUtf8Json())
                          .GetResult();

            IHttpResponse response = null;

            yield return(this.httpWorker.SendRequest(request, rsp => response = rsp));

            var result = response.TryParseJson <GameProfile>();

            callback.Try(result);
        }
        public void CreateGameProfile(GameProfileRequest gameProfile, ResultCallback <GameProfile> callback)
        {
            if (!this.user.IsLoggedIn)
            {
                callback.Try(Result <GameProfile> .CreateError(ErrorCode.IsNotLoggedIn, "User is not logged in"));
                return;
            }

            this.taskDispatcher.Start(
                Task.Retry(
                    cb => this.api.CreateGameProfile(this.user.Namespace, this.user.UserId,
                                                     this.user.AccessToken, gameProfile, result => cb(result)),
                    result => this.coroutineRunner.Run(() => callback((Result <GameProfile>)result)),
                    this.user));
        }
        /// <summary>
        /// Create  new profile for current user
        /// </summary>
        /// <param name="gameProfile">The game profile that about to create</param>
        /// <param name="callback">Returns the created game profile via callback when completed.</param>
        public void CreateGameProfile(GameProfileRequest gameProfile, ResultCallback <GameProfile> callback)
        {
            Report.GetFunctionLog(this.GetType().Name);
            if (!this.session.IsValid())
            {
                callback.TryError(ErrorCode.IsNotLoggedIn);

                return;
            }

            this.coroutineRunner.Run(
                this.api.CreateGameProfile(
                    this.@namespace,
                    this.session.UserId,
                    this.session.AuthorizationToken,
                    gameProfile,
                    callback));
        }
        public HttpResponseMessage UpdateGame(GameProfileRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Field is invalid"));
            }

            bool res = false;
            GameProfileService gPSvc = new GameProfileService();

            try
            {
                res = gPSvc.UpdateGame(model);
                return(Request.CreateResponse(HttpStatusCode.OK, res));
            }
            catch (System.Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
            }
        }
        public IEnumerator <ITask> UpdateGameProfile(string @namespace, string userId, string accessToken,
                                                     string profileId, GameProfileRequest gameProfile, ResultCallback <GameProfile> callback)
        {
            Assert.IsNotNull(@namespace, "Can't update a game profile! namespace parameter is null!");
            Assert.IsNotNull(userId, "Can't update a game profile! userId parameter is null!");
            Assert.IsNotNull(accessToken, "Can't update a game profile! accessToken parameter is null!");
            Assert.IsNotNull(gameProfile, "Can't update a game profile! gameProfile parameter is null!");
            Assert.IsNotNull(profileId, "Can't update a game profile! gameProfile.profileId is null!");

            var request = HttpRequestBuilder
                          .CreatePut(this.baseUrl + "/soc-profile/public/namespaces/{namespace}/users/{userId}/profiles/{profileId}")
                          .WithPathParam("namespace", @namespace)
                          .WithPathParam("userId", userId)
                          .WithPathParam("profileId", profileId)
                          .Accepts(MediaType.ApplicationJson)
                          .WithBearerAuth(accessToken)
                          .WithContentType(MediaType.ApplicationJson)
                          .WithBody(SimpleJson.SimpleJson.SerializeObject(gameProfile))
                          .ToRequest();

            HttpWebResponse response = null;

            yield return(Task.Await(request, rsp => response = rsp));

            if (response == null)
            {
                callback.Try(Result <GameProfile> .CreateError(ErrorCode.NetworkError, "There is no response"));
                yield break;
            }

            var responseText = response.GetBodyText();

            response.Close();
            Result <GameProfile> result;

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                try
                {
                    GameProfile gameProfileResult = SimpleJson.SimpleJson.DeserializeObject <GameProfile>(responseText);
                    result = Result <GameProfile> .CreateOk(gameProfileResult);
                }
                catch (ArgumentException ex)
                {
                    result = Result <GameProfile> .CreateError(ErrorCode.InvalidResponse,
                                                               "Update game profile failed to deserialize response body: " + ex.Message);
                }

                break;

            case HttpStatusCode.NotFound:
                result = Result <GameProfile> .CreateError(ErrorCode.NotFound,
                                                           "Update game profile failed due to the resource not found");

                break;

            default:
                result = Result <GameProfile> .CreateError((ErrorCode)response.StatusCode,
                                                           "Update game profile failed with status: " + response.StatusCode);

                break;
            }

            callback.Try(result);
        }
        public bool UpdateGame(GameProfileRequest model)
        {
            bool res = false;

            string sqlConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(sqlConnectionString))
            {
                conn.Open();

                using (SqlCommand cmd = new SqlCommand("dbo.Games_UpdateWithAttr", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Id", model.Id);
                    cmd.Parameters.AddWithValue("@Title", model.Title);

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("PlatformId", typeof(int));

                        for (int i = 0; i < model.Platforms.Length; i++)
                        {
                            table.Rows.Add(model.Platforms[i]);
                        }

                        var pList = new SqlParameter("@Platforms", SqlDbType.Structured);
                        pList.TypeName = "PlatformsTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("GenreId", typeof(int));

                        for (int i = 0; i < model.Genres.Length; i++)
                        {
                            table.Rows.Add(model.Genres[i]);
                        }

                        var pList = new SqlParameter("@Genres", SqlDbType.Structured);
                        pList.TypeName = "GenresTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    cmd.Parameters.AddWithValue("@Studio", model.Studio);

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("DirectorId", typeof(int));

                        for (int i = 0; i < model.Directors.Length; i++)
                        {
                            table.Rows.Add(model.Directors[i]);
                        }

                        var pList = new SqlParameter("@Directors", SqlDbType.Structured);
                        pList.TypeName = "DirectorsTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    cmd.ExecuteNonQuery();

                    res = true;
                }

                conn.Close();
            }

            return(res);
        }
        public int AddGame(GameProfileRequest model)
        {
            int res = 0;

            string sqlConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(sqlConnectionString))
            {
                conn.Open();

                #region Comments

                /*
                 * back up in case table insert doesnt work
                 * using (SqlCommand cmd = new SqlCommand("dbo.Games_Insert", conn))
                 * {
                 *  cmd.CommandType = CommandType.StoredProcedure;
                 *  cmd.Parameters.AddWithValue("@Title", model.Title);
                 *
                 *  SqlParameter param = new SqlParameter("@Id", SqlDbType.Int);
                 *  param.Direction = ParameterDirection.Output;
                 *  cmd.Parameters.Add(param);
                 *
                 *  cmd.ExecuteNonQuery();
                 *
                 *  res = (int)cmd.Parameters["@Id"].Value;
                 * }
                 *
                 * for (int i = 0; i < model.Platforms.Length; i++)
                 * {
                 *  using (SqlCommand cmd = new SqlCommand("dbo.GamesPlatforms_Insert", conn))
                 *  {
                 *      cmd.CommandType = CommandType.StoredProcedure;
                 *      cmd.Parameters.AddWithValue("@GameId", res);
                 *      cmd.Parameters.AddWithValue("@PlatformId", model.Platforms[i]);
                 *
                 *      cmd.ExecuteNonQuery();
                 *  }
                 * }
                 */
                #endregion Comments

                using (SqlCommand cmd = new SqlCommand("dbo.Games_InsertWithAttr", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Title", model.Title);

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("PlatformId", typeof(int));

                        for (int i = 0; i < model.Platforms.Length; i++)
                        {
                            table.Rows.Add(model.Platforms[i]);
                        }

                        var pList = new SqlParameter("@Platforms", SqlDbType.Structured);
                        pList.TypeName = "PlatformsTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("GenreId", typeof(int));

                        for (int i = 0; i < model.Genres.Length; i++)
                        {
                            table.Rows.Add(model.Genres[i]);
                        }

                        var pList = new SqlParameter("@Genres", SqlDbType.Structured);
                        pList.TypeName = "GenresTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    cmd.Parameters.AddWithValue("@Studio", model.Studio);

                    using (var table = new DataTable())
                    {
                        table.Columns.Add("DirectorId", typeof(int));

                        for (int i = 0; i < model.Directors.Length; i++)
                        {
                            table.Rows.Add(model.Directors[i]);
                        }

                        var pList = new SqlParameter("@Directors", SqlDbType.Structured);
                        pList.TypeName = "DirectorsTemp";
                        pList.Value    = table;

                        cmd.Parameters.Add(pList);
                    }

                    SqlParameter param = new SqlParameter("@Id", SqlDbType.Int);
                    param.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(param);

                    cmd.ExecuteNonQuery();

                    res = (int)cmd.Parameters["@Id"].Value;
                }

                conn.Close();
            }

            return(res);
        }