public PurchaseCoinsResponse PurchaseCoin(PurchaseCoinsRequest purchaseCoinRequest)
 {
     PurchaseCoinsResponse resp = new PurchaseCoinsResponse();
     List<SqlParameter> sqlParam = new List<SqlParameter>();
     string sqlStr = null;
     int coin = 0;
     DBUtility.AddSqlParam(sqlParam, "@Id", SqlDbType.BigInt, purchaseCoinRequest.UserId);
     sqlStr = @"select Coin from UserInfo where Id = @Id";
     DataSet ds = DBUtility.ExecuteDataset(sqlStr, sqlParam);
     if(DBUtility.hasResult(ds))
     {
         coin = Convert.ToInt32(ds.Tables[0].Rows[0]["Coin"]);
     }
     coin += purchaseCoinRequest.PurchasedCoin;
     DBUtility.AddSqlParam(sqlParam, "@Coin", SqlDbType.BigInt, coin);
     DBUtility.AddSqlParam(sqlParam, "@CreatedOn", SqlDbType.DateTime, DateTime.Now.ToString());
     sqlStr = @"Update UserInfo set Coin=@Coin, CreatedOn = @CreatedOn where Id = @Id";
     try
     {
         log.Debug("Update user Coins. SQL: " + sqlStr);
         if (DBUtility.ExecuteNonQuery(sqlStr, sqlParam) != 1)
         {
             throw new ServiceException("Update User purchased coin failed. UserID: " + purchaseCoinRequest.UserId);
         }
         resp.Coins = coin;
         return resp;
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        public Response PostRequest(Stream input)
        {
            String postedValue = retrievePostValue(input);
            Request request = new JavaScriptSerializer().Deserialize<Request>(postedValue);
            Response response = new Response();

            String serializedRequestData = new JavaScriptSerializer().Serialize(request.RequestData);
            log.Debug("EnglishRemix2: PostRequest, MethodName:" + request.MethodName + ", TimeStamp:" + request.PostTime + ", RequestData: " + serializedRequestData);

            try
            {
                if (request.MethodName.ToLower().Equals("activation"))
                {
                    DeviceActivationRequest deviceActivationRequest = new JavaScriptSerializer().Deserialize<DeviceActivationRequest>(serializedRequestData);
                    DeviceActivationResponse deviceActivationResponse = new DeviceActivationResponse();
                    deviceActivationResponse = deviceFacade.ActivateDevice(deviceActivationRequest);
                    response.ResponseData = new JavaScriptSerializer().Serialize(deviceActivationResponse);
                    response.HasError = false;
                    systemFacade.Log("0", "1", "1", "activation", 1);
                    return response;
                }

                //use DUID and UID to do the request authantication here
                if (!DeviceValidator.Instance.isValid(request.UserId, request.Udid))
                {
                    response.HasError = true;
                    response.ErrorMsg = ResponseMessages.DEVICE_VALIDATION_FAILED_MESSAGE;
                    response.ResponseData = new JavaScriptSerializer().Serialize(new DeviceValidationResponse());
                    systemFacade.Log("0", "1", "1", "Request authentication failed.", 1);
                    return response;
                }

                // Request Device is activated. Request is authenticated.
                switch (request.MethodName.ToLower())
                {
                    case "register":
                        log.Debug("EnglishRemix2, Action: Register");
                        RegistrationRequest registrationRequest = new JavaScriptSerializer().Deserialize<RegistrationRequest>(serializedRequestData);
                        RegistrationResponse resgitrationResponse = new RegistrationResponse();
                        resgitrationResponse = userFacade.RegisterUser(registrationRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(resgitrationResponse);
                        systemFacade.AddUserAction("0", request.MethodName.ToLower(), registrationRequest.UserName, registrationRequest.password, "0");
                        systemFacade.Log("0", "1", "1", resgitrationResponse.Message, 1);
                        break;
                    case "login":
                        log.Debug("EnglishRemix2, Action: Login");
                        LoginRequest loginRequest = new JavaScriptSerializer().Deserialize<LoginRequest>(serializedRequestData);
                        LoginResponse loginResponse = new LoginResponse();
                        loginResponse = userFacade.LoginUser(loginRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(loginResponse);
                        systemFacade.AddUserAction("0", request.MethodName.ToLower(), loginRequest.UserName, loginRequest.Password, "0");
                        systemFacade.Log("0", "1", "1", "", 1);
                        break;
                    case "getuserachievement":
                        log.Debug("EnglishRemix2, Action: getuserachievement");
                        AchievementRequest achievementRequest = new JavaScriptSerializer().Deserialize<AchievementRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        AchievementResponse achievementResp = userFacade.GetUserAchievement(achievementRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(achievementResp);
                        systemFacade.Log("0", "1", "1", "userachievements", 1);
                        break;
                    case "getallachievements":
                        log.Debug("EnglishRemix2, Action: getallachievement");
                        AchievementResponse achievementResps = userFacade.GetAllAchievements();
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        response.ResponseData = new JavaScriptSerializer().Serialize(achievementResps);
                        systemFacade.Log("0", "1", "1", "allachievements", 1);
                        break;
                    case "getchapter":
                        log.Debug("EnglishRemix2, Action: getChapter, serializedRequestData:" + serializedRequestData);
                        ChapterRequest chapterRequest = new JavaScriptSerializer().Deserialize<ChapterRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), chapterRequest.ChapterNumber.ToString(), "0", "0");
                        ChapterResponse chapterResp = contentFacade.GetChapterByNumber(chapterRequest.ChapterNumber);
                        response.ResponseData = new JavaScriptSerializer().Serialize(chapterResp);
                        systemFacade.Log("0", "1", "1", "getchapter", 1);
                        break;
                    case "getchapterlist":
                        log.Debug("EnglishRemix2, Action: getChapter, serializedRequestData:" + serializedRequestData);
                        ChapterListRequest chapterListRequest = new JavaScriptSerializer().Deserialize<ChapterListRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        ChapterListResponse chapterListResponse = contentFacade.GetChapterList();
                        response.ResponseData = new JavaScriptSerializer().Serialize(chapterListResponse);
                        systemFacade.Log("0", "1", "1", "getchapterlist", 1);
                        break;
                    case "updateactivitystatusbyuser":
                        log.Debug("EnglishRemix2, Action:updateactivitystatusbyuser");
                        UpdateActivityStatusRequest updateActivityStatusRequest = new JavaScriptSerializer().Deserialize<UpdateActivityStatusRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), updateActivityStatusRequest.ActivityId.ToString(), updateActivityStatusRequest.ActivityStatusName, "0");
                        UpdateActivityStatusResponse updateActivityStatusResponse = userFacade.UpdateActivityStatusByUser(updateActivityStatusRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(updateActivityStatusResponse);
                        systemFacade.Log("0", "1", "1", updateActivityStatusResponse.Message, 1);
                        break;
                    case "getuseractivitystatsus":
                        log.Debug("EnglishRemix2, Action: getuseractivitystatus");
                        UserActivityStatusRequest userActivityStatusRequest = new JavaScriptSerializer().Deserialize<UserActivityStatusRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), userActivityStatusRequest.ChapterNumber.ToString(), "0", "0");
                        UserActivityStatusResponse userActivityStatusResp = new UserActivityStatusResponse();
                        userActivityStatusResp = userFacade.GetUserActivityStatus(userActivityStatusRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(userActivityStatusResp);
                        systemFacade.Log("0", "1", "1", "getuseractivitystatus", 1);
                        break;
                    case "allusers":
                        log.Debug("EnglishRemix2, Action: allusers");
                        response.ResponseData = new JavaScriptSerializer().Serialize(userFacade.GetAllUserInfo());
                        systemFacade.Log("0", "1", "1", "allusers", 1);
                        break;
                    case "getuser":
                        log.Debug("EnglishRemix2, Action: getuser");
                        UserRequest userRequest = new JavaScriptSerializer().Deserialize<UserRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        UserResponse userResp = new UserResponse();
                        userResp = userFacade.GetUserInfo(userRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(userResp);
                        systemFacade.Log("0", "1", "1", "getuser", 1);
                        break;
                    case "updateuser":
                        log.Debug("EnglishRemix2, Action: updateUser");
                        UpdateUserRequest updateUserRequest = new JavaScriptSerializer().Deserialize<UpdateUserRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), updateUserRequest.Coin.ToString(), updateUserRequest.Level.ToString(), updateUserRequest.ActivationStatus.ToString());
                        UpdateUserResponse updatedUserResp = new UpdateUserResponse();
                        updatedUserResp = userFacade.UpdateUser(updateUserRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(updatedUserResp);
                        systemFacade.Log("0", "1", "1", updatedUserResp.Message, 1);
                        break;
                    case "updatepassword":
                        log.Debug("EnglishRemix2, Action: updatePassword");
                        UpdatePasswordRequest changePwdRequest = new JavaScriptSerializer().Deserialize<UpdatePasswordRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        UpdatePasswordResponse changePwdResp = new UpdatePasswordResponse();
                        changePwdResp = userFacade.UpdatePassword(changePwdRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(changePwdResp);
                        systemFacade.Log("0", "1", "1", changePwdResp.Message, 1);
                        break;
                    case "purchaseactivity":
                        log.Debug("EnglishRemix2, Action: purchaseactivity");
                        UserActivityRequest purchaseActivityRequest = new JavaScriptSerializer().Deserialize<UserActivityRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), purchaseActivityRequest.ActivityId.ToString(), "0", "0");
                        UserActivityResponse purchaseActivityResp = new UserActivityResponse();
                        purchaseActivityResp = userFacade.PurchaseActivity(purchaseActivityRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(purchaseActivityResp);
                        systemFacade.Log("0", "1", "1", purchaseActivityResp.Message, 1);
                        break;
                    case "adduserfavorite":
                        log.Debug("EnglishRemix2, Action: adduserFavorite");
                        AddFavoriteRequest addFavoriteRequest = new JavaScriptSerializer().Deserialize<AddFavoriteRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), addFavoriteRequest.ChapterNumber.ToString(), addFavoriteRequest.PageNumber.ToString(), "0");
                        AddFavoriteResponse addFavoriteResp = new AddFavoriteResponse();
                        addFavoriteResp = userFacade.AddUserFavourite(addFavoriteRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(addFavoriteResp);
                        systemFacade.Log("0", "1", "1", addFavoriteResp.Message, 1);
                        break;
                    case "getuserfavorite":
                        log.Debug("EnglishRemix2, Action: getuserFavorite");
                        GetFavoriteRequest getUserFavoriteRequest = new JavaScriptSerializer().Deserialize<GetFavoriteRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        GetFavoriteResponse getUserFavoriteResp = new GetFavoriteResponse();
                        getUserFavoriteResp = userFacade.GetUserFavorite(getUserFavoriteRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(getUserFavoriteResp);
                        systemFacade.Log("0", "1", "1", "getuserfavourite", 1);
                        break;
                    case "deleteuserfavorite":
                        log.Debug("EnglishRemix2, Action: deleteUserFavorite, serializeRequesetData: " + serializedRequestData);
                        DeleteFavoriteRequest deleteUserFavoriteRequest = new JavaScriptSerializer().Deserialize<DeleteFavoriteRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), deleteUserFavoriteRequest.ChapterNumber.ToString(), deleteUserFavoriteRequest.PageNumber.ToString(), "0");
                        DeleteFavoriteResponse deleteUserFavoriteResponse = new DeleteFavoriteResponse();
                        deleteUserFavoriteResponse = userFacade.DeleteUserFavorite(deleteUserFavoriteRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(deleteUserFavoriteResponse);
                        systemFacade.Log("0", "1", "1", deleteUserFavoriteResponse.Message, 1);
                        break;
                    case "uploadvideo":
                        log.Debug("EnglishRemix2, Action: uploadvideo");
                        UploadVideoRequest uploadVideoRequest = new JavaScriptSerializer().Deserialize<UploadVideoRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), uploadVideoRequest.ActivityId.ToString(), uploadVideoRequest.ChapterNumber.ToString(), uploadVideoRequest.SelfScore.ToString());
                        UploadVideoResponse uploadVideoResp = new UploadVideoResponse();
                        uploadVideoResp = videoFacade.UploadVideo(uploadVideoRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(uploadVideoResp);
                        systemFacade.Log("0", "1", "1", "uploadvideo", 1);
                        break;
                    case "getuploadedvideolist":
                        log.Debug("EnglishRemix2, Action: getUploadedVideo, serializedRequestData:" + serializedRequestData);
                        VideoListRequest videoListRequest = new JavaScriptSerializer().Deserialize<VideoListRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), videoListRequest.ActivityId.ToString(), videoListRequest.SentenceNumber.ToString(), "0");
                        VideoListResponse videoListResponse = new VideoListResponse();
                        videoListResponse = videoFacade.GetUploadedVideoList(videoListRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(videoListResponse);
                        systemFacade.Log("0", "1", "1", "getloadvideolist", 1);
                        break;
                    case "getuploadedvideobyid":
                        log.Debug("EnglishRemix2, Action: getuploadedvideobyid");
                        GetUploadedVideoRequest getUploadedVideoRequest = new JavaScriptSerializer().Deserialize<GetUploadedVideoRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), getUploadedVideoRequest.VideoId.ToString(), "0", "0");
                        GetUploadedVideoResponse getUploadedVideoResponse = new GetUploadedVideoResponse();
                        getUploadedVideoResponse = videoFacade.GetUploadedVideoById(getUploadedVideoRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(getUploadedVideoResponse);
                        systemFacade.Log("0", "1", "1", "getuploadedvideobyid", 1);
                        break;
                    case "postcomment":
                        log.Debug("EnglishRemix2, Action:postcomment");
                        PostCommentRequest postCommentRequest = new JavaScriptSerializer().Deserialize<PostCommentRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), postCommentRequest.CreatorId.ToString(), postCommentRequest.ToCommentId.ToString(), postCommentRequest.UserVideoId.ToString());
                        PostCommentResponse postCommentResponse = videoFacade.PostComment(postCommentRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(postCommentResponse);
                        systemFacade.Log("0", "1", "1", postCommentResponse.Message, 1);
                        break;
                    case "getcommentbyuploadedvideoid":
                        log.Debug("EnglishRemix2, Action:getcommentbyuploadedvideoid");
                        CommentRequest commentRequest = new JavaScriptSerializer().Deserialize<CommentRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), commentRequest.UploadedVideoId.ToString(), "0", "0");
                        CommentResponse commentResponse = videoFacade.GetCommentByUploadedVideoId(commentRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(commentResponse);
                        systemFacade.Log("0", "1", "1", "allcomments", 1);
                        break;
                    case "rateuploadedvideo":
                        log.Debug("EnglishRemix2, Action:rateuploadedvideo");
                        RateUploadedVideoRequest rateRequest = new JavaScriptSerializer().Deserialize<RateUploadedVideoRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), rateRequest.CreatorId.ToString(), rateRequest.VideoId.ToString(), rateRequest.Score.ToString());
                        RateUploadedVideoResponse rateResponse = videoFacade.RateUploadedVideo(rateRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(rateResponse);
                        systemFacade.Log("0", "1", "1", rateResponse.Message, 1);
                        break;
                    case "getrankingvideo":
                        log.Debug("EnglishRemix2, Action:getrankingvideo");
                        GetRankingVideoRequest getrankingvideorequest = new JavaScriptSerializer().Deserialize<GetRankingVideoRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), getrankingvideorequest.Score.ToString(), getrankingvideorequest.Ranking.ToString(), "0");
                        GetRankingVideoResponse getrankingvideoresponse = videoFacade.GetRankingVideo(getrankingvideorequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(getrankingvideoresponse);
                        systemFacade.Log("0", "1", "1", "rankingvideos", 1);
                        break;
                    case "purchasecoin":
                        log.Debug("EnglishRemix2, Action: purchaseCoin, serializeRequestData: " + serializedRequestData);
                        PurchaseCoinsRequest purchaseCoinRequest = new JavaScriptSerializer().Deserialize<PurchaseCoinsRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), purchaseCoinRequest.PurchasedCoin.ToString(), "0", "0");
                        PurchaseCoinsResponse purhcaseCoinResp = new PurchaseCoinsResponse();
                        purhcaseCoinResp = userFacade.PurchaseCoin(purchaseCoinRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(purhcaseCoinResp);
                        systemFacade.Log("0", "1", "1", "purchasecoin", 1);
                        break;
                    case "getpurchaseproduct":
                        log.Debug("EnglishRemix2, Action: getPurhcaseProduct");
                        PurchaseProductRequest purchaseRequest = new JavaScriptSerializer().Deserialize<PurchaseProductRequest>(serializedRequestData);
                        systemFacade.AddUserAction(request.UserId, request.MethodName.ToLower(), "0", "0", "0");
                        PurchaseProductResponse purchaseResp = new PurchaseProductResponse();
                        purchaseResp = systemFacade.GetPurchaseProduct(purchaseRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(purchaseResp);
                        systemFacade.Log("0", "1", "1", "gerpurchaseproduct", 1);
                        break;
                    case "getuseractionbyid":
                        //for debugging purpose
                        log.Debug("EnglishRemix2, Action: getUserActionById-debugging");
                        GetUserActionRequest getUserActionRequest = new JavaScriptSerializer().Deserialize<GetUserActionRequest>(serializedRequestData);
                        GetUserActionResponse getUserActionResp = new GetUserActionResponse();
                        getUserActionResp = userFacade.GetUserActionById(getUserActionRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(getUserActionResp);
                        systemFacade.Log("0", "1", "1", "getuseractionbyid", 1);
                        break;
                    case "getactiontype":
                        //for debugging purpose
                        log.Debug("EnglishRemix1, Action: getActionType-debugging");
                        ActionTypeRequest actionTypeRequest = new JavaScriptSerializer().Deserialize<ActionTypeRequest>(serializedRequestData);
                        ActionTypeResponse actionTypeResp = new ActionTypeResponse();
                        actionTypeResp = systemFacade.GetActionType(actionTypeRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(actionTypeResp);
                        systemFacade.Log("0", "1", "1", "getactiontype", 1);
                        break;
                    case "adduserachievement":
                        //Add User Achievement
                        log.Debug("EnglishRemix2, Action: addUserAchievement-debugging");
                        AddAchievementRequest addAchievementRequest = new JavaScriptSerializer().Deserialize<AddAchievementRequest>(serializedRequestData);
                        AddAchievementResponse addAchievementResp = new AddAchievementResponse();
                        addAchievementResp = systemFacade.AddUserAchievement(addAchievementRequest);
                        response.ResponseData = new JavaScriptSerializer().Serialize(addAchievementResp);
                        systemFacade.Log("0", "1", "1", "addAchievement", 1);
                        break;
                    default:
                        //To catch the method name wrong input
                        throw new ServiceException("Method name is wrong. Current method name is " + request.MethodName);
                }
            }
            catch (ServiceException e)
            {
                // Catch service exception and return its response to the user.
                response = e.response;

                if (!request.UserId.Equals(""))
                    systemFacade.Log(request.UserId, "1", "1", e.response.ErrorMsg, 1);
                else
                    systemFacade.Log("0", "1", "1", e.response.ErrorMsg, 1);

                return response;
            }
            catch (Exception e)
            {
                // Catch unknown/sql exception
                response.HasError = true;
                response.ErrorMsg = e.ToString();
                if (!request.UserId.Equals(""))
                    systemFacade.Log("0", "1", "1", e.Message, 1);
                else
                    systemFacade.Log("0", "1", "1", e.Message, 1);
                return response;
            }

            response.HasError = false;
            return response;
        }