public ReturnMsg InsertUserUploadDetails(UserUploads details)
        {
            ReturnMsg obj = new ReturnMsg();

            List <SqlParameter> sp = new List <SqlParameter>()
            {
                new SqlParameter()
                {
                    ParameterName = "@userId", Value = details.UserId, SqlDbType = SqlDbType.BigInt
                },
                new SqlParameter()
                {
                    ParameterName = "@uploadedImagePath", Value = details.UploadedImagePath, SqlDbType = SqlDbType.VarChar
                },
                new SqlParameter()
                {
                    ParameterName = "@uploadedAudioPath", Value = details.UploadedAudioPath, SqlDbType = SqlDbType.VarChar
                },
                new SqlParameter()
                {
                    ParameterName = "@id", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output
                },
            };

            object[]      parameters = sp.ToArray();
            List <object> pa         = new List <object>();

            _userUploadsRepository.ExecuteStoredProcedureForOutParams(PROC_INSERT_USER_UPLOADS, out pa, parameters);
            if (pa.Count > 0)
            {
                obj.IsSuccess = true;
                obj.Message   = pa[0];
            }
            else
            {
                obj.IsSuccess = false;
            }
            return(obj);
        }
        public async Task <IHttpActionResult> InsertMomentsDetails()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(Content(HttpStatusCode.BadRequest, "Unsupported media type."));
            }
            ImageResult imageResult = new ImageResult();

            try
            {
                bool   isAudio  = false;
                bool   isImage  = false;
                string root     = HttpContext.Current.Server.MapPath("~/MomentUploads");
                var    provider = new MultipartFormDataStreamProvider(root);

                await Request.Content.ReadAsMultipartAsync(provider);

                string  StoragePath   = string.Empty;
                Moments momentDetails = new Moments();
                if (provider.FormData.Count == 0 && provider.FileData.Count == 0)
                {
                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Id is required.")));
                }
                else if (provider.FormData.Count == 0 && provider.FileData.Count > 0)
                {
                    foreach (var item in provider.FileData)
                    {
                        File.Delete(item.LocalFileName);
                    }

                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Id is required.")));
                }
                else if (provider.FormData.Count == 1 && provider.FileData.Count == 0)
                {
                    return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Either  an image or an audio or text and user id and parent id is required. ")));
                }
                long   userId   = Convert.ToInt64(provider.FormData.GetValues(0)[0]);
                long   parentId = Convert.ToInt64(provider.FormData.GetValues(1)[0]);
                string message  = string.Empty;
                if (provider.FormData.Count > 2)
                {
                    message = provider.FormData.GetValues(2)[0];
                }
                //string message = provider.FormData.GetValues(0)[1];


                //moving the file to the required destination
                string    temp      = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                string    fileName1 = string.Empty;
                string    path1     = string.Empty;
                ReturnMsg msg       = new ReturnMsg();
                foreach (MultipartFileData fileData in provider.FileData)
                {
                    string fileName = fileData.Headers.ContentDisposition.FileName;
                    fileName = fileName.Replace(" ", "_");
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }
                    string extension = fileName.Split('.')[1].ToLower();
                    if (!validFormats.Any(a => extension.Contains(a)))
                    {
                        File.Delete(fileData.LocalFileName);
                        if (provider.FileData.Count == 1)
                        {
                            return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Please upload an image or an audio file.")));
                        }
                        else
                        {
                            break;
                        }
                    }
                    fileName1 = fileName.Split('.')[0] + "_" + temp + "." + extension;
                    if (audioFormats.Any(a => extension.Contains(a)))
                    {
                        isImage     = false;
                        isAudio     = true;
                        StoragePath = HttpContext.Current.Server.MapPath("~/MomentUploads/Audio");
                    }
                    else if (imageFormats.Any(a => extension.Contains(a)))
                    {
                        isImage     = true;
                        isAudio     = false;
                        StoragePath = HttpContext.Current.Server.MapPath("~/MomentUploads/Images/OriginalSize");
                    }
                    path1 = Path.Combine(StoragePath, fileName1);
                    File.Move(fileData.LocalFileName, path1);
                    File.Delete(fileData.LocalFileName);

                    if (isImage)
                    {
                        //saving the file in medium size;
                        int    width      = 0;
                        string folderName = string.Empty;
                        width = 400;
                        //Saving the medium size
                        folderName = "MomentUploads/Images";
                        ImageUploadHelper imageUpload = new ImageUploadHelper {
                            Width = width
                        };
                        imageResult = imageUpload.RenameUploadFile(fileData, true, "", fileName1, folderName);

                        //saving the file in thumbnail size

                        width       = 150;
                        imageUpload = new ImageUploadHelper {
                            Width = width
                        };
                        imageResult = imageUpload.RenameUploadFile(fileData, false, "", fileName1, folderName);
                    }
                    UserUploads userUploads = new UserUploads();
                    userUploads.UserId = userId;
                    if (isImage)
                    {
                        userUploads.UploadedImagePath = imageResult.ImageName;
                        userUploads.UploadedAudioPath = string.Empty;
                    }
                    else if (isAudio)
                    {
                        userUploads.UploadedAudioPath = fileName1;
                        userUploads.UploadedImagePath = string.Empty;
                    }
                    msg = _userUploadsService.InsertUserUploadDetails(userUploads);
                }


                //inserting moments in the database;
                long userUploadedId = 0;

                userUploadedId = Convert.ToInt64(msg.Message);

                momentDetails.Message        = message;
                momentDetails.ParentId       = parentId;
                momentDetails.PosterUserId   = userId;
                momentDetails.UserUploadedId = userUploadedId;
                var dateTime = DateTime.UtcNow;
                momentDetails.PostingTime = dateTime.ToString(@"yyyy/MM/dd hh:mm tt", new CultureInfo("en-US"));
                _momentsService.InsertMomentDetails(momentDetails);
            }
            catch (Exception e)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, e.Message)));
            }
            return(Ok("Moment Details inserted successfully."));
        }