Пример #1
0
        public IHttpActionResult CreateShop(ShopBindingModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                using (DunkeyContext ctx = new DunkeyContext())
                {
                    if (ctx.Stores.Any(x => x.BusinessName == model.BusinessName))
                    {
                        return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                        {
                            Message = "Forbidden",
                            StatusCode = (int)HttpStatusCode.Forbidden,
                            Result = new Error {
                                ErrorMessage = "Store already exists"
                            }
                        }));
                    }
                    else
                    {
                        var newStoreModel = new Store {
                            BusinessName = model.BusinessName, BusinessType = model.BusinessType, Longitude = model.Longitude, Latitude = model.Latitude, Location = DunkeyDelivery.Utility.CreatePoint(model.Latitude, model.Longitude)
                        };
                        ctx.Stores.Add(newStoreModel);
                        ctx.SaveChanges();
                        CustomResponse <Store> response = new CustomResponse <Store>
                        {
                            Message    = Global.SuccessMessage,
                            StatusCode = (int)HttpStatusCode.OK,
                            Result     = newStoreModel
                        };

                        return(Ok(response));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(DunkeyDelivery.Utility.LogError(ex)));
            }
        }
Пример #2
0
        public async Task <IHttpActionResult> AddStoreWithImage()
        {
            try
            {
                var    httpRequest  = HttpContext.Current.Request;
                string newFullPath  = string.Empty;
                string fileNameOnly = string.Empty;

                ShopBindingModel model = new ShopBindingModel();
                model.BusinessName = httpRequest.Params["BusinessName"];
                model.BusinessType = httpRequest.Params["BusinessType"];
                model.Latitude     = Convert.ToDouble(httpRequest.Params["Latitude"]);
                model.Longitude    = Convert.ToDouble(httpRequest.Params["Longitude"]);


                Validate(model);

                #region Validations
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!Request.Content.IsMimeMultipartContent())
                {
                    return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                    {
                        Message = "UnsupportedMediaType",
                        StatusCode = (int)HttpStatusCode.UnsupportedMediaType,
                        Result = new Error {
                            ErrorMessage = "Multipart data is not included in request."
                        }
                    }));
                }
                else if (httpRequest.Files.Count > 1)
                {
                    return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                    {
                        Message = "UnsupportedMediaType",
                        StatusCode = (int)HttpStatusCode.UnsupportedMediaType,
                        Result = new Error {
                            ErrorMessage = "Multiple images are not supported, please upload one image."
                        }
                    }));
                }
                #endregion

                using (DunkeyContext ctx = new DunkeyContext())
                {
                    if (ctx.Stores.Any(x => x.BusinessName == model.BusinessName))
                    {
                        return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                        {
                            Message = "Forbidden",
                            StatusCode = (int)HttpStatusCode.Forbidden,
                            Result = new Error {
                                ErrorMessage = "Store already exists"
                            }
                        }));
                    }
                    else
                    {
                        #region ImageSaving
                        var postedFile = httpRequest.Files[0];
                        if (postedFile != null && postedFile.ContentLength > 0)
                        {
                            int MaxContentLength = 1024 * 1024 * 10; //Size = 10 MB

                            IList <string> AllowedFileExtensions = new List <string> {
                                ".jpg", ".gif", ".png"
                            };
                            var ext       = Path.GetExtension(postedFile.FileName);
                            var extension = ext.ToLower();
                            if (!AllowedFileExtensions.Contains(extension))
                            {
                                return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                                {
                                    Message = "UnsupportedMediaType",
                                    StatusCode = (int)HttpStatusCode.UnsupportedMediaType,
                                    Result = new Error {
                                        ErrorMessage = "Please Upload image of type .jpg,.gif,.png."
                                    }
                                }));
                            }
                            else if (postedFile.ContentLength > MaxContentLength)
                            {
                                return(Content(HttpStatusCode.OK, new CustomResponse <Error>
                                {
                                    Message = "UnsupportedMediaType",
                                    StatusCode = (int)HttpStatusCode.UnsupportedMediaType,
                                    Result = new Error {
                                        ErrorMessage = "Please Upload a file upto 1 mb."
                                    }
                                }));
                            }
                            else
                            {
                                int count = 1;
                                fileNameOnly = Path.GetFileNameWithoutExtension(postedFile.FileName);
                                newFullPath  = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["StoreImageFolderPath"] + postedFile.FileName);

                                while (File.Exists(newFullPath))
                                {
                                    string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
                                    newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["StoreImageFolderPath"] + tempFileName + extension);
                                }
                                postedFile.SaveAs(newFullPath);
                            }
                        }
                        #endregion



                        Store storeModel = new Store
                        {
                            BusinessName = model.BusinessName,
                            BusinessType = model.BusinessType,
                            Latitude     = model.Latitude,
                            Longitude    = model.Longitude,
                            ImageUrl     = DunkeyDelivery.Utility.BaseUrl + ConfigurationManager.AppSettings["StoreImageFolderPath"] + Path.GetFileName(newFullPath),
                            Location     = DunkeyDelivery.Utility.CreatePoint(model.Latitude, model.Longitude)
                        };

                        ctx.Stores.Add(storeModel);
                        ctx.SaveChanges();

                        CustomResponse <Store> response = new CustomResponse <Store>
                        {
                            Message    = Global.SuccessMessage,
                            StatusCode = (int)HttpStatusCode.OK,
                            Result     = storeModel
                        };
                        return(Ok(response));
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(DunkeyDelivery.Utility.LogError(ex)));
            }
        }