Example #1
0
        public async Task <HttpResponseMessage> AddPromotionData([FromBody] NearByMePromotion promotion)
        {
            try
            {
                LogRequest(promotion);
                if (!ModelState.IsValid)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
                }
                bool operationCompleted = await System.Threading.Tasks.Task.Run(() => nearByMePromotionManager.UpsertPromotion(promotion));

                var userPromotions = await System.Threading.Tasks.Task.Run(() => nearByMePromotionManager.GetPromotionByUserName(promotion.username));

                return(Request.CreateResponse(HttpStatusCode.OK, new { returnValue = userPromotions }));
            }
            catch (ApplicationException applicationException)
            {
                return(Request.CreateErrorResponse((HttpStatusCode)Convert.ToInt16(applicationException.Message), NeeoDictionaries.HttpStatusCodeDescriptionMapper[Convert.ToInt16(applicationException.Message)]));
            }
            catch (Exception exception)
            {
                Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().GetType(), exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
Example #2
0
        public async Task <HttpResponseMessage> AddPromotionDataNew()
        {
            try
            {
                LogRequest("Im here in new with images");

                Logger.LogManager.CurrentInstance.InfoLogger.LogInfo(System.Reflection.MethodBase.GetCurrentMethod().GetType(), "uzair");

                string  data2       = "XXXXXXXX123";
                var     httpContext = HttpContext.Current;
                dynamic data        = httpContext.Request.Form;
                if (data["username"] == null || data["username"] == "")
                {
                    data2 = data2 + "username data is null";
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, new { returnValue = "Username Null" }));
                }

                LogRequest("X123" + data);
                if (data["featuredImageName"] == null)
                {
                    data2 = data2 + "featured image data is null";
                }
                else
                {
                    data2 = data2 + "featured image value is :" + data["featuredImageName"].ToString() + ";";
                }


                if (data["username"] == null)
                {
                    data2 = data2 + "username data is null";
                }
                else
                {
                    data2 = data2 + "username value is :" + data["username"].ToString() + ";";
                }

                if (data["status"] == null)
                {
                    data2 = data2 + "status data is null";
                }
                else
                {
                    data2 = data2 + "status value is :" + data["status"].ToString() + ";";
                }

                if (data["description"] == null)
                {
                    data2 = data2 + "description data is null";
                }
                else
                {
                    data2 = data2 + "description value is :" + data["description"].ToString() + ";";
                }
                data2 = data2 + ";Number of Files:" + (HttpContext.Current.Request.Files == null ? "0" : HttpContext.Current.Request.Files.Count.ToString());

                LogRequest(data2);
                if (HttpContext.Current.Request.Files == null || HttpContext.Current.Request.Files.Count == 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, new { returnValue = "No images are in attachment." + data }));
                }
                else
                {
                    //Loop through uploaded files
                    for (int i = 0; i < httpContext.Request.Files.Count; i++)
                    {
                        data2 = httpContext.Request.Files[i].FileName;
                        LogRequest(data2);
                    }
                }

                string originalFileName = "";

                //Near by me promotion Values

                //binding Near by me promotion DTO
                LogRequest("1");
                string            featuredImageName = Convert.ToString(data["featuredImageName"]);
                NearByMePromotion promotion         = new NearByMePromotion();
                promotion.promotionId = 0;
                promotion.username    = Convert.ToString(data["username"]);
                promotion.description = Convert.ToString(data["description"]);
                promotion.status      = Convert.ToByte(data["status"]);
                //promotion.createdDate = Convert.ToDateTime(data["createdDate"]);
                //promotion.updatedDate = Convert.ToDateTime(data["updatedDate"]);
                promotion.ImagesXml = new List <NearByMePromotionImage>();
                LogRequest(promotion);
                LogRequest("2");
                //Getting user promotions
                var userPromotions = await System.Threading.Tasks.Task.Run(() => nearByMePromotionManager.GetPromotionByUserName(promotion.username));

                bool featuredImageNameMatched = false;

                //Minimum 1 feature image needed at first time
                if (userPromotions == null && ((featuredImageName == null || featuredImageName == "") || httpContext.Request.Files.Count == 0))
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, new { returnValue = "There must be at least one featured image" }));
                }

                //Check whern feature image name given but Files length 0
                //else if (featuredImageName != null && httpContext.Request.Files.Count == 0)
                //{
                //    return Request.CreateResponse(HttpStatusCode.LengthRequired, new { returnValue = "Given Featured Image does not exist in the provided list of image" });
                //}
                else
                {
                    LogRequest("4");
                    List <FileDetails> images = new List <FileDetails>();
                    // Check for any uploaded file
                    if (httpContext.Request.Files.Count > 0)
                    {
                        string ext;
                        for (int i = 0; i < httpContext.Request.Files.Count; i++)
                        {
                            HttpPostedFile httpPostedFile = httpContext.Request.Files[i];
                            //fileContentType=httpPostedFile.ContentType.Split('/')[1];

                            ext = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf("."));
                            var ggg = httpPostedFile.FileName.Substring(0, httpPostedFile.FileName.Length - (ext.Length));
                            if (httpPostedFile.FileName.Substring(0, httpPostedFile.FileName.Length - (ext.Length)) == featuredImageName)
                            {
                                featuredImageNameMatched = true;
                            }
                        }

                        if ((featuredImageNameMatched == false && featuredImageName != "") || (featuredImageNameMatched == false && featuredImageName != ""))
                        {
                            return(Request.CreateResponse(HttpStatusCode.BadRequest, new { returnValue = "Given Featured Image does not exist in the provided list of image" }));
                        }

                        //Loop through uploaded files
                        for (int i = 0; i < httpContext.Request.Files.Count; i++)
                        {
                            HttpPostedFile httpPostedFile = httpContext.Request.Files[i];

                            string filesExtention = (ConfigurationManager.AppSettings["nearByMeFilesExtention"]);
                            ext = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf("."));
                            string[] filesExtentionSplit = filesExtention.Split(',');

                            LogRequest("5");
                            // if (httpPostedFile != null && filesExtentionSplit.Contains(ext.ToLower()))
                            {
                                NearByMePromotionImage currentImage = new NearByMePromotionImage();
                                currentImage.ImageName = httpPostedFile.FileName;
                                ext = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf("."));
                                string newfileName = httpPostedFile.FileName.Remove(httpPostedFile.FileName.Length - (ext.Length + 1), (ext.Length + 1)) + "" + ext /*".jpg"*/;


                                int w = 0;
                                while (isFileExist(Path.Combine(HostingEnvironment.MapPath("~" + ConfigurationManager.AppSettings["PromotionImagesPath"]), newfileName).ToString()) == true)
                                {
                                    ++w;
                                    //  newfileName = httpPostedFile.FileName.Remove(httpPostedFile.FileName.Length - 4, 4) + "(" + w + ").jpg";
                                    newfileName = httpPostedFile.FileName.Remove(httpPostedFile.FileName.Length - (ext.Length + 1), (ext.Length + 1)) + "(" + w + ")" + ext;
                                }



                                var fileSavePath = Path.Combine(HostingEnvironment.MapPath("~" + ConfigurationManager.AppSettings["PromotionImagesPath"]), newfileName);

                                // Save the uploaded file
                                httpPostedFile.SaveAs(fileSavePath);
                                var returnpath = ConfigurationManager.AppSettings["imagesBaseUrl"] + ConfigurationManager.AppSettings["PromotionImagesPath"] + "/" + newfileName;

                                currentImage.imageCaption = newfileName;
                                currentImage.imagePath    = returnpath;
                                promotion.ImagesXml.Add(currentImage);
                                if (httpPostedFile.FileName.Substring(0, httpPostedFile.FileName.Length - (ext.Length)) == featuredImageName)

                                //                                if (httpPostedFile.FileName.Remove(httpPostedFile.FileName.Length - (ext.Length + 1), (ext.Length + 1)) == featuredImageName)
                                {
                                    currentImage.featuredImage = true;
                                }
                            }
                        }
                    }
                    LogRequest("6");
                    bool operationCompleted = await System.Threading.Tasks.Task.Run(() => nearByMePromotionManager.UpsertPromotionNew(promotion));

                    userPromotions = await System.Threading.Tasks.Task.Run(() => nearByMePromotionManager.GetPromotionByUserName(promotion.username));

                    return(Request.CreateResponse(HttpStatusCode.OK, new { returnValue = userPromotions }));
                }
            }
            catch (ApplicationException applicationException)
            {
                Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().GetType(), applicationException.Message, applicationException);
                string mode = null;

                IEnumerable <String> headerValues;
                if (Request.Headers.TryGetValues("mode", out headerValues))
                {
                    mode = headerValues.FirstOrDefault();
                }
                if (mode != null && mode.ToLower() == "debug")
                {
                    throw;
                }
                else
                {
                    return(Request.CreateErrorResponse((HttpStatusCode)Convert.ToInt16(applicationException.Message), NeeoDictionaries.HttpStatusCodeDescriptionMapper[Convert.ToInt16(applicationException.Message)]));
                }
                // return Request.CreateErrorResponse((HttpStatusCode)Convert.ToInt16(applicationException.Message), NeeoDictionaries.HttpStatusCodeDescriptionMapper[Convert.ToInt16(applicationException.Message)]);
            }
            catch (Exception exception)
            {
                Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().GetType(), exception.Message, exception);
                string mode = null;

                IEnumerable <String> headerValues;
                if (Request.Headers.TryGetValues("mode", out headerValues))
                {
                    mode = headerValues.FirstOrDefault();
                }
                if (mode != null && mode.ToLower() == "debug")
                {
                    throw;
                }
                else
                {
                    {
                        return(Request.CreateResponse(HttpStatusCode.InternalServerError));
                    }
                    //Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().GetType(), exception.Message, exception);
                    //return Request.CreateResponse(HttpStatusCode.InternalServerError);
                }
            }
        }