Exemple #1
0
        public HttpResponseMessage PutUserInformation([FromBody] UpdateUserRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            request.Uid      = (request.Uid != null) ? request.Uid.Trim() : request.Uid;
            request.name     = (request.name != null) ? request.name.Trim() : request.name;
            request.fileData = (request.fileData != null) ? request.fileData.Trim() : request.fileData;
            bool  isUpdated = false;
            ulong temp      = 0;

            LogRequest(request);


            NeeoUser user = new NeeoUser(request.Uid);

            try
            {
                isUpdated = user.UpdateUserProfile(request.name, new LibNeeo.IO.File()
                {
                    Data = request.fileData
                });
                return(Request.CreateResponse(HttpStatusCode.OK, isUpdated));

                #region old impl
                //using (TransactionScope scope = new TransactionScope())
                //{
                //    if (!user.UpdateUsersDisplayName(name))
                //    {
                //        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidUser);
                //    }
                //    else if (NeeoUtility.IsNullOrEmpty(fileData))
                //    {
                //        isCompleted = true;
                //        scope.Complete();
                //    }
                //    else
                //    {
                //        LibNeeo.IO.File file = new LibNeeo.IO.File()
                //        {
                //            Data = fileData,
                //            FileOwner = userID,
                //            MediaType = MediaType.Image,
                //            MimeType = MimeType.Image_jpeg
                //        };
                //        if (user.SaveFile(file, FileCategory.Profile))
                //        {
                //            isCompleted = true;
                //            scope.Complete();
                //        }
                //    }
                //}
                #endregion
            }
            catch (ApplicationException appExp)
            {
                return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)(Convert.ToInt32(appExp.Message))));
            }
            catch (Exception exp)
            {
                LogManager.CurrentInstance.ErrorLogger.LogError(
                    MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
                return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)CustomHttpStatusCode.ServerInternalError));
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates user information into the database and the profile picture.
        /// </summary>
        /// <param name="userID">A string containing phone number as user id.</param>
        /// <param name="name">A string containing the name of the user.</param>
        /// <param name="fileData">A base64 encoded string containing the file data.</param>
        /// <returns>
        /// true if information is successfully updated; otherwise, false.
        /// </returns>
        public bool UpdateUserInformation(string userID, string name, string fileData)
        {
            userID   = (userID != null) ? userID.Trim() : userID;
            name     = (name != null) ? name.Trim() : name;
            fileData = (fileData != null) ? fileData.Trim() : fileData;
            bool  isUpdated = false;
            ulong temp      = 0;

            #region log user request and response

            /***********************************************
            *  To log user request and response
            ***********************************************/
            if (_logRequestResponse)
            {
                LogManager.CurrentInstance.InfoLogger.LogInfo(
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, System.Reflection.MethodBase.GetCurrentMethod().Name + "===>" +
                    "Request ===> userID : " + userID + ", name : " + name);
            }

            #endregion

            //#region Verify User
            //var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            //string keyFromClient = request.Headers["key"];
            //if (NeeoUtility.AuthenticateUserRequest(userID, keyFromClient))
            //{
            //#endregion

            if (NeeoUtility.IsNullOrEmpty(userID) || !ulong.TryParse(userID, out temp))
            {
                NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
            }
            else if (NeeoUtility.IsNullOrEmpty(name))
            {
                NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
            }
            else
            {
                NeeoUser user = new NeeoUser(userID);
                try
                {
                    isUpdated = user.UpdateUserProfile(name, new LibNeeo.IO.File()
                    {
                        Data = fileData
                    });
                    #region old impl
                    //using (TransactionScope scope = new TransactionScope())
                    //{
                    //    if (!user.UpdateUsersDisplayName(name))
                    //    {
                    //        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidUser);
                    //    }
                    //    else if (NeeoUtility.IsNullOrEmpty(fileData))
                    //    {
                    //        isCompleted = true;
                    //        scope.Complete();
                    //    }
                    //    else
                    //    {
                    //        LibNeeo.IO.File file = new LibNeeo.IO.File()
                    //        {
                    //            Data = fileData,
                    //            FileOwner = userID,
                    //            MediaType = MediaType.Image,
                    //            MimeType = MimeType.Image_jpeg
                    //        };
                    //        if (user.SaveFile(file, FileCategory.Profile))
                    //        {
                    //            isCompleted = true;
                    //            scope.Complete();
                    //        }
                    //    }
                    //}
                    #endregion
                }
                catch (ApplicationException appExp)
                {
                    NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)(Convert.ToInt32(appExp.Message)));
                }
                catch (Exception exp)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
                    NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.ServerInternalError);
                }
            }
            return(isUpdated);
            //}
            //else
            //{
            //   NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.Unauthorized);

            //    return false;
            //}
        }
Exemple #3
0
        public async Task <HttpResponseMessage> PostUserInformation()
        {
            if (!Request.Headers.Contains("uid"))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            if (!Request.Headers.Contains("name"))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            string uId       = Request.Headers.GetValues("uid").First();
            string name      = Request.Headers.GetValues("name").First();;
            bool   isUpdated = false;

            if (Request.Content.IsMimeMultipartContent())
            {
                MultipartMemoryStreamProvider provider = await Request.Content.ReadAsMultipartAsync(new MultipartMemoryStreamProvider());

                foreach (HttpContent content in provider.Contents)
                {
                    if (content.Headers.ContentType == null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest));
                    }
                    if (content.Headers.ContentDisposition.FileName == null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest));
                    }
                    string contentType     = content.Headers.ContentType.ToString();
                    string sourceExtension = content.Headers.ContentDisposition.FileName.Split(new char[] { '.' }).Last().Split(new char[] { '"' }).First();

                    if (!MimeTypeMapping.ValidateMimeType(contentType))
                    {
                        return(Request.CreateResponse(HttpStatusCode.BadRequest));
                    }

                    using (var stream = content.ReadAsStreamAsync().Result)
                    {
                        NeeoUser user = new NeeoUser(uId);
                        try
                        {
                            isUpdated = user.UpdateUserProfile(name, new LibNeeo.IO.File()
                            {
                                FileStream = new FileDataStream()
                                {
                                    Stream = stream
                                }
                            });
                            return(Request.CreateResponse(HttpStatusCode.OK, isUpdated));
                        }
                        catch (ApplicationException appExp)
                        {
                            return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)(Convert.ToInt32(appExp.Message))));
                        }
                        catch (Exception exp)
                        {
                            LogManager.CurrentInstance.ErrorLogger.LogError(
                                MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
                            return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)CustomHttpStatusCode.ServerInternalError));
                        }
                    }
                }
            }
            else
            {
                NeeoUser user = new NeeoUser(uId);
                try
                {
                    isUpdated = user.UpdateUserProfile(name, null);
                    return(Request.CreateResponse(HttpStatusCode.OK, isUpdated));
                }
                catch (ApplicationException appExp)
                {
                    return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)(Convert.ToInt32(appExp.Message))));
                }
                catch (Exception exp)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
                    return(SetCustomResponseMessage(isUpdated, (HttpStatusCode)CustomHttpStatusCode.ServerInternalError));
                }
            }

            return(Request.CreateResponse(HttpStatusCode.BadRequest));
        }