public async Task <IHttpActionResult> DeleteEntity(int Id) { try { using (DunkeyContext ctx = new DunkeyContext()) { var Contributor = ctx.Contributors.FirstOrDefault(x => x.Id == Id); if (Contributor != null) { Contributor.is_deleted = 1; ctx.SaveChanges(); return(Ok(new CustomResponse <string> { Message = ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK })); } return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Not Found", StatusCode = (int)HttpStatusCode.NotFound, Result = new Error { ErrorMessage = "Record not found." } })); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (DunkeyContext ctx = new DunkeyContext()) { var tokens = ctx.ForgotPasswordTokens.Where(x => x.User_Id == model.UserId && x.IsDeleted == false); if (tokens.Count() > 0) { foreach (var token in tokens) { token.IsDeleted = true; } ctx.Users.FirstOrDefault(x => x.Id == model.UserId).Password = model.Password; ctx.SaveChanges(); } } return(View("PasswordResetSuccess")); }
public async Task <IHttpActionResult> Register(DriverBindingModel model) { model.Role = 0; try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.Drivers.Any(x => x.Email == model.Email)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Driver with email already exists" } })); //return BadRequest("User with the same email already exists."); //return Request.CreateResponse(HttpStatusCode.OK, new Error { ErrorCode = "400 Conflict", Message = "User with email already exist"}); } else { Driver driverModel = new Driver { FullName = model.FullName, Email = model.Email, Phone = model.Phone, City = model.City, VehicleType = model.VehicleType, HearFrom = model.HearFrom }; ctx.Drivers.Add(driverModel); ctx.SaveChanges(); using (DriverViewModel driverViewModel = new DriverViewModel(driverModel)) { //driverViewModel.Token = await Utility.GenerateToken(driverModel.Email, driverModel.Password, Request); CustomResponse <DriverViewModel> response = new CustomResponse <DriverViewModel> { Message = "Success", StatusCode = (int)HttpStatusCode.OK, Result = driverViewModel }; return(Ok(response)); //return Request.CreateResponse(HttpStatusCode.OK, response); } } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> Register(RideBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.Rider.Any(x => x.Email == model.Email)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Rider with email already exists" } })); } else { Rider rideModel = new Rider() { FullName = model.FullName, BusinessName = model.BusinessName, Email = model.Email, BusinessType = model.BusinessType, Status = (int)Global.StatusCode.NotVerified, Phone = model.Phone, ZipCode = model.ZipCode }; ctx.Rider.Add(rideModel); ctx.SaveChanges(); using (RideViewModel rideViewModel = new RideViewModel(rideModel)) { //userViewModel.Token = await Utility.GenerateToken(userModel.Email, userModel.Password, Request); CustomResponse <RideViewModel> response = new CustomResponse <RideViewModel> { Message = "Success", StatusCode = (int)HttpStatusCode.OK, Result = rideViewModel }; return(Ok(response)); //return Request.CreateResponse(HttpStatusCode.OK, response); } } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> SubmitPharmacyRequest(PharmacyRequestBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else if (model.Product_Ids.Count == 0) { return(Ok(new CustomResponse <Error> { Message = Utility.Global.ResponseMessages.BadRequest, StatusCode = (int)HttpStatusCode.Forbidden, Result = new Error { ErrorMessage = "Please add medicines" } })); } using (DunkeyContext ctx = new DunkeyContext()) { PharmacyRequest pharmModel = new PharmacyRequest { Delivery_Address = model.Delivery_Address, Delivery_City = model.Delivery_City, Delivery_Phone = model.Delivery_Phone, Delivery_State = model.Delivery_State, Delivery_Zip = model.Delivery_Zip, Doctor_FirstName = model.Doctor_FirstName, Doctor_LastName = model.Doctor_LastName, Doctor_Phone = model.Doctor_Phone, Gender = model.Gender, Patient_DOB = DateTime.ParseExact(model.Patient_DOB, "dd/MM/yyyy hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture), Patient_FirstName = model.Patient_FirstName, Patient_LastName = model.Patient_LastName, CreatedDate = DateTime.Now }; foreach (var productId in model.Product_Ids) { pharmModel.PharmacyRequest_Products.Add(new PharmacyRequest_Products { Product_Id = productId.Value }); } ctx.PharmacyRequest.Add(pharmModel); ctx.SaveChanges(); return(Ok(new CustomResponse <PharmacyRequest> { Message = Utility.Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK })); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public IHttpActionResult SubmitStoreReview(ReviewBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.StoreRatings.Any(x => x.User_Id == model.User_Id && x.Store_Id == model.Store_Id)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Forbidden", StatusCode = (int)HttpStatusCode.Forbidden, Result = new Error { ErrorMessage = "User already reviews this store." } })); } else { var storeRating = new StoreRatings { User_Id = model.User_Id, Rating = model.Rating, Store_Id = model.Store_Id, Feedback = model.Feedback }; ctx.StoreRatings.Add(storeRating); ctx.SaveChanges(); var latestStoreRating = ctx.StoreRatings.Include(z => z.User).FirstOrDefault(x => x.Id == storeRating.Id); CustomResponse <StoreRatings> response = new CustomResponse <StoreRatings> { Message = Global.SuccessMessage, StatusCode = (int)HttpStatusCode.OK, Result = storeRating }; return(Ok(response)); } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> Create(ProductBindingModel model) { try { short Status = model.Status; if (Status != 1) { model.Status = 0; } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.Products.Any(x => x.Category_Id == model.Category_Id && x.Name == model.Name)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Product already exists under same store and category" } })); } else { var newProductModel = new Product { Category_Id = model.Category_Id, Name = model.Name, Description = model.Description, Price = model.Price, Status = 0, Store_Id = model.Store_Id }; ctx.Products.Add(newProductModel); ctx.SaveChanges(); CustomResponse <Product> response = new CustomResponse <Product> { Message = Global.SuccessMessage, StatusCode = (int)HttpStatusCode.OK, Result = newProductModel }; return(Ok(response)); } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
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))); } }
public IHttpActionResult Create(ShopCategoriesBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.Categories.Any(x => x.Store_Id == model.Store_Id && x.Name == model.Name)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Category already exists under same store" } })); } else { var newCategoryModel = new Category { Name = model.Name, Description = model.Description, Status = model.Status, Store_Id = model.Store_Id, ParentCategoryId = model.ParentCategoryId }; ctx.Categories.Add(newCategoryModel); ctx.SaveChanges(); CustomResponse <Category> response = new CustomResponse <Category> { Message = Global.SuccessMessage, StatusCode = (int)HttpStatusCode.OK, Result = newCategoryModel }; return(Ok(response)); } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> RequestGetCloth(RequestClothBindingModel model) { try { var Response = new LaundryRequest(); using (DunkeyContext ctx = new DunkeyContext()) { var TodayDate = DateTime.Today.Day; var checkRequest = ctx.LaundryRequest.FirstOrDefault(x => x.User_Id == model.User_Id && x.Store_Id == model.Store_Id && x.RequestDate.Day == TodayDate); if (checkRequest == null) { LaundryRequest Request = new LaundryRequest { User_Id = model.User_Id, Store_Id = model.Store_Id, Name = "Get Cloth Request", RequestDate = DateTime.Now, isDeleted = false, Status = Convert.ToInt16(Global.ClothRequestTypes.Pending) }; ctx.LaundryRequest.Add(Request); ctx.SaveChanges(); } else { return(Ok(new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "You already requested to Get Cloths for today." } })); } } return(Ok(new CustomResponse <LaundryRequest> { Message = Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = Response })); } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> RepeatOrder(string orderId) { try { using (DunkeyContext ctx = new DunkeyContext()) { var Order = ctx.Orders.AsNoTracking().Include(x => x.StoreOrders.Select(x1 => x1.Order_Items)).Where(x => x.OrderNo == orderId).FirstOrDefault(); ctx.Orders.Add(Order); ctx.SaveChanges(); return(Ok(new CustomResponse <Order> { Message = Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = Order })); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public IHttpActionResult InsertComments(string Message, int Post_id, int UserId) { try { using (DunkeyContext ctx = new DunkeyContext()) { if (UserId == 0) { UserId = ctx.Users.FirstOrDefault(x => x.Email == DunkeyDelivery.Utility.GuestEmail).Id; } BlogComments Comment = new BlogComments { Message = Message, PostedDate = DateTime.Now, CreatedDate = DateTime.Now, User_Id = UserId, Post_Id = Post_id }; var BlogData = ctx.BlogPosts.Where(x => x.Id == Post_id).FirstOrDefault(); BlogData.BlogComments.Add(Comment); ctx.SaveChanges(); CustomResponse <BlogComments> response = new CustomResponse <BlogComments> { Message = DunkeyDelivery.Global.SuccessMessage, StatusCode = (int)HttpStatusCode.OK, Result = Comment }; return(Ok(response)); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> ImportProducts() { try { using (DunkeyContext ctx = new DunkeyContext()) { var ValidationCount = 0; var httpRequest = HttpContext.Current.Request; string newFullPath = string.Empty; string fileNameOnly = string.Empty; if (httpRequest.Files.Count > 1) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "UnsupportedMediaType", StatusCode = (int)HttpStatusCode.UnsupportedMediaType, Result = new Error { ErrorMessage = "Multiple files are not supported, please upload one image." } })); } #region ImageSaving var postedFile = httpRequest.Files[0]; if (postedFile != null && postedFile.ContentLength > 0) { //int MaxContentLength = 1024 * 1024 * 10; //Size = 1 MB IList <string> AllowedFileExtensions = new List <string> { ".xlsx", ".xls" }; 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 file of type .xlsx or .xls" } })); } else if (postedFile.ContentLength > Global.MaximumImageSize) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "UnsupportedMediaType", StatusCode = (int)HttpStatusCode.UnsupportedMediaType, Result = new Error { ErrorMessage = "Please Upload a file upto " + Global.ImageSize + "." } })); } else { //int count = 1; //fileNameOnly = Path.GetFileNameWithoutExtension(postedFile.FileName); //newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["UserImageFolderPath"] + postedFile.FileName); //while (File.Exists(newFullPath)) //{ // string tempFileName = string.Format("{0}({1})", fileNameOnly, count++); // newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["UserImageFolderPath"] + tempFileName + extension); //} //postedFile.SaveAs(newFullPath); List <Product> ProductToImport = new List <Product>(); Product SingleProduct = new Product(); Error ReturnError = new Error(); var ErrorCount = 0; SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY"); var workbook = ExcelFile.Load(postedFile.InputStream, extension == ".xls" ? (LoadOptions)LoadOptions.XlsDefault : (LoadOptions)LoadOptions.XlsxDefault); var worksheet = workbook.Worksheets.ActiveWorksheet; foreach (var allocatedCell in worksheet.Rows) { foreach (ExcelCell cell in allocatedCell.AllocatedCells) { if (cell.Row.Index == 0) { if (cell.Column.Index == 0 && cell.Value.ToString().Contains("Name")) { ValidationCount++; } else if (cell.Column.Index == 1 && cell.Value.ToString().Contains("Price")) { ValidationCount++; } else if (cell.Column.Index == 2 && cell.Value.ToString().Contains("Description")) { ValidationCount++; } else if (cell.Column.Index == 3 && cell.Value.ToString().Contains("Status")) { ValidationCount++; } else if (cell.Column.Index == 4 && cell.Value.ToString().Contains("Category_Id")) { ValidationCount++; } else if (cell.Column.Index == 5 && cell.Value.ToString().Contains("Store_Id")) { ValidationCount++; } else if (cell.Column.Index == 6 && cell.Value.ToString().Contains("Size")) { ValidationCount++; } else { } } else { if (ValidationCount == 7) { if (cell.Column.Index == 0) { SingleProduct.Name = Convert.ToString(cell.Value); } else if (cell.Column.Index == 1) { SingleProduct.Price = Convert.ToDouble(cell.Value); } else if (cell.Column.Index == 2) { SingleProduct.Description = Convert.ToString(cell.Value); } else if (cell.Column.Index == 3) { SingleProduct.Status = Convert.ToInt16(cell.Value); } else if (cell.Column.Index == 4) { SingleProduct.Category_Id = Convert.ToInt32(cell.Value); } else if (cell.Column.Index == 5) { SingleProduct.Store_Id = Convert.ToInt32(cell.Value); } else if (cell.Column.Index == 6) { SingleProduct.Size = Convert.ToString(cell.Value); SingleProduct.IsDeleted = false; } else { } } } } if (allocatedCell.Index != 0) { ProductToImport.Add(SingleProduct); } } foreach (var product in ProductToImport) { if (!ctx.Categories.Any(x => x.Id == product.Category_Id && x.Store_Id == product.Store_Id)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Invalid Store_Id " + product.Store_Id + " or Category_Id " + product.Category_Id } })); } if (ctx.Products.Any(x => x.Name.Contains(product.Name))) { ReturnError.ErrorMessage = "Product by name '" + product.Name + "' already exists."; ErrorCount++; } } if (ErrorCount == 0 && ProductToImport.Count > 0) { ctx.Products.AddRange(ProductToImport); ctx.SaveChanges(); } else { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = ReturnError })); } } } #endregion CustomResponse <string> response = new CustomResponse <string> { Message = ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = "Products added successfully. " }; return(Ok(response)); } } catch (Exception ex) { StatusCode(DunkeyDelivery.Utility.LogError(ex)); return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Invalid Store_Id or Category_Id" } })); } }
public async Task <IHttpActionResult> InsertOrder(OrderViewModel model) { try { Order order; if (System.Web.HttpContext.Current.Request.Params["Cart"] != null) { model.Cart = JsonConvert.DeserializeObject <CartViewModel>(System.Web.HttpContext.Current.Request.Params["Cart"]); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (model.Cart.CartItems.Count() > 0) { order = new Order(); using (DunkeyContext ctx = new DunkeyContext()) { order.MakeOrder(model, ctx); order.DeliveryTime_From = DateTime.Now; order.DeliveryTime_To = DateTime.Now; //Charge User // StripeCharge stripeCharge = DunkeyDelivery.Utility.GetStripeChargeInfo(model.StripeEmail, model.StripeAccessToken, Convert.ToInt32(order.Total)); //if (stripeCharge.Status != "succeeded") //{ // return Ok(new CustomResponse<Error> { Message = "Payment Failed", StatusCode = (int)HttpStatusCode.InternalServerError, Result = new Error { ErrorMessage = "We are unable to process your payments. Please try sometime later" } }); //} ctx.Orders.Add(order); await ctx.SaveChangesAsync(); var CurrentUser = ctx.Users.Where(x => x.Id == model.UserId).FirstOrDefault(); if (CurrentUser.RewardPoints == 0) { CurrentUser.RewardPoints = order.Subtotal * DunkeyDelivery.Global.PointsToReward; } else { CurrentUser.RewardPoints = CurrentUser.RewardPoints + (order.Subtotal * DunkeyDelivery.Global.PointsToReward); } ctx.SaveChanges(); return(Ok(new CustomResponse <Order> { Message = Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = order })); } } else { return(Ok(new CustomResponse <Error> { Message = Global.ResponseMessages.BadRequest, StatusCode = (int)HttpStatusCode.BadRequest, Result = new Error { ErrorMessage = "No items in the cart." } })); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> SubmitPharmacyRequestMobile(PharmacyRequestMobileBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } else if (model.Product_Ids.Count == 0 && string.IsNullOrEmpty(model.Product_Id)) { return(Ok(new CustomResponse <Error> { Message = Utility.Global.ResponseMessages.BadRequest, StatusCode = (int)HttpStatusCode.Forbidden, Result = new Error { ErrorMessage = "Please add medicines" } })); } if (!string.IsNullOrEmpty(model.Product_Id)) { var ProductIds = model.Product_Id.Split(',').Select(Int32.Parse).ToList(); foreach (var item in ProductIds) { model.Product_Ids.Add(item); } } using (DunkeyContext ctx = new DunkeyContext()) { UserAddress UserAddress = new UserAddress(); UserAddress = ctx.UserAddresses.FirstOrDefault(x => x.User_ID == model.User_Id && x.IsDeleted == false && x.IsPrimary == true); if (UserAddress == null) { UserAddress = ctx.UserAddresses.Where(x => x.User_ID == model.User_Id && x.IsDeleted == false).OrderByDescending(x => x.Id).FirstOrDefault(); } if (UserAddress == null) { return(Ok(new CustomResponse <Error> { Message = Utility.Global.ResponseMessages.NotFound, StatusCode = (int)HttpStatusCode.NotFound, Result = new Error { ErrorMessage = "User addresses not found." } })); } PharmacyRequest pharmModel = new PharmacyRequest { Delivery_Address = UserAddress.FullAddress, Delivery_City = UserAddress.City, Delivery_Phone = UserAddress.Telephone, Delivery_State = UserAddress.State, Delivery_Zip = UserAddress.PostalCode, Doctor_FirstName = model.Doctor_FirstName, Doctor_LastName = model.Doctor_LastName, Doctor_Phone = model.Doctor_Phone, Gender = model.Gender, Patient_DOB = Convert.ToDateTime(model.Patient_DOB), Patient_FirstName = model.Patient_FirstName, Patient_LastName = model.Patient_LastName, CreatedDate = DateTime.Now }; foreach (var productId in model.Product_Ids) { pharmModel.PharmacyRequest_Products.Add(new PharmacyRequest_Products { Product_Id = productId.Value }); } ctx.PharmacyRequest.Add(pharmModel); ctx.SaveChanges(); return(Ok(new CustomResponse <PharmacyRequest> { Message = Utility.Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK })); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
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))); } }
public async Task <IHttpActionResult> Register(MerchantBindingModel model) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (DunkeyContext ctx = new DunkeyContext()) { if (ctx.Admins.Any(x => x.Email == model.Email)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Merchant with email already exists" } })); //return BadRequest("User with the same email already exists."); //return Request.CreateResponse(HttpStatusCode.OK, new Error { ErrorCode = "400 Conflict", Message = "User with email already exist"}); } else { Admin adminModel = new Admin { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, //Password = "******", //model.Password, FullName = model.FirstName + " " + model.LastName, BusinessName = model.BusinessName, BusinessType = model.BusinessType, Status = (int)Global.StatusCode.NotVerified, Phone = model.Phone, Role = model.Role }; ctx.Admins.Add(adminModel); ctx.SaveChanges(); using (AdminViewModel adminViewModel = new AdminViewModel(adminModel)) { //userViewModel.Token = await Utility.GenerateToken(userModel.Email, userModel.Password, Request); CustomResponse <AdminViewModel> response = new CustomResponse <AdminViewModel> { Message = "Success", StatusCode = (int)HttpStatusCode.OK, Result = adminViewModel }; return(Ok(response)); //return Request.CreateResponse(HttpStatusCode.OK, response); } } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public async Task <IHttpActionResult> AddProductWithImage() { try { var httpRequest = HttpContext.Current.Request; string newFullPath = string.Empty; string fileNameOnly = string.Empty; ProductBindingModel model = new ProductBindingModel(); model.Name = httpRequest.Params["Name"]; model.Price = Convert.ToDouble(httpRequest.Params["Price"]); model.Category_Id = Convert.ToInt32(httpRequest.Params["CatId"]); model.Description = httpRequest.Params["Description"]; model.Store_Id = Convert.ToInt32(httpRequest.Params["StoreId"]); 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.Products.Any(x => x.Category_Id == model.Category_Id && x.Name == model.Name)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Product already exists under same store and category" } })); } else { #region ImageSaving var postedFile = httpRequest.Files[0]; if (postedFile != null && postedFile.ContentLength > 0) { 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 > Global.MaximumImageSize) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "UnsupportedMediaType", StatusCode = (int)HttpStatusCode.UnsupportedMediaType, Result = new Error { ErrorMessage = "Please Upload a file upto " + Global.ImageSize + "." } })); } else { int count = 1; fileNameOnly = Path.GetFileNameWithoutExtension(postedFile.FileName); newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["ProductImageFolderPath"] + postedFile.FileName); while (File.Exists(newFullPath)) { string tempFileName = string.Format("{0}({1})", fileNameOnly, count++); newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["ProductImageFolderPath"] + tempFileName + extension); } postedFile.SaveAs(newFullPath); } } #endregion //var RequestUriString = Request.RequestUri.ToString(); //var baseUrl = RequestUriString.Substring(0, RequestUriString.IndexOf("api")); Product productModel = new Product { Name = model.Name, Category_Id = model.Category_Id, Description = model.Description, Price = model.Price, Store_Id = model.Store_Id, Image = DunkeyDelivery.Utility.BaseUrl + ConfigurationManager.AppSettings["ProductImageFolderPath"] + Path.GetFileName(newFullPath) }; ctx.Products.Add(productModel); ctx.SaveChanges(); CustomResponse <Product> response = new CustomResponse <Product> { Message = Global.SuccessMessage, StatusCode = (int)HttpStatusCode.OK, Result = productModel }; return(Ok(response)); } } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }
public IHttpActionResult InsertBlog() { try { var httpRequest = HttpContext.Current.Request; string newFullPath = string.Empty; string fileNameOnly = string.Empty; var extension = string.Empty; BlogPosts model = new BlogPosts(); BlogPosts existingModel = new BlogPosts(); model.Title = httpRequest.Params["Title"]; model.CategoryType = httpRequest.Params["CategoryType"]; model.DateOfPosting = Convert.ToDateTime(httpRequest.Params["DateOfPosting"]); model.Description = httpRequest.Params["Description"]; model.Admin_ID = Convert.ToInt32(httpRequest.Params["User_Id"]); if (httpRequest.Params["Id"] != null) { model.Id = Convert.ToInt32(httpRequest.Params["Id"]); } 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 (model.Id == 0) { if (ctx.BlogPosts.Any(x => x.Title == model.Title && x.CategoryType == model.CategoryType && x.is_deleted == 0)) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "Conflict", StatusCode = (int)HttpStatusCode.Conflict, Result = new Error { ErrorMessage = "Post with under this category already exists" } })); } //else //{ // BlogPosts post = new BlogPosts // { // Title = model.Title, // CategoryType = model.CategoryType, // Description = model.Description, // ImageUrl = DunkeyDelivery.Utility.BaseUrl + ConfigurationManager.AppSettings["BlogImageFolderPath"] + Path.GetFileName(newFullPath), // DateOfPosting = model.DateOfPosting, // Admin_ID = model.Admin_ID, // is_deleted=0 // }; // ctx.BlogPosts.Add(model); // ctx.SaveChanges(); // CustomResponse<BlogPosts> response = new CustomResponse<BlogPosts> // { // Message = DunkeyDelivery.Global.SuccessMessage, // StatusCode = (int)HttpStatusCode.OK, // Result = post // }; // return Ok(response); // } } else { existingModel = ctx.BlogPosts.FirstOrDefault(x => x.Id == model.Id); } #region ImageSaving string fileExtension = string.Empty; HttpPostedFile postedFile = null; if (httpRequest.Files.Count > 0) { postedFile = httpRequest.Files[0]; if (postedFile != null && postedFile.ContentLength > 0) { IList <string> AllowedFileExtensions = new List <string> { ".jpg", ".gif", ".png" }; var ext = Path.GetExtension(postedFile.FileName); fileExtension = ext.ToLower(); if (!AllowedFileExtensions.Contains(fileExtension)) { 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 > DunkeyDelivery.Global.MaximumImageSize) { return(Content(HttpStatusCode.OK, new CustomResponse <Error> { Message = "UnsupportedMediaType", StatusCode = (int)HttpStatusCode.UnsupportedMediaType, Result = new Error { ErrorMessage = "Please Upload a file upto " + DunkeyDelivery.Global.ImageSize } })); } else { //int count = 1; //fileNameOnly = Path.GetFileNameWithoutExtension(postedFile.FileName); //newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["AdminImageFolderPath"] + postedFile.FileName); //while (File.Exists(newFullPath)) //{ // string tempFileName = string.Format("{0}({1})", fileNameOnly, count++); // newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["AdminImageFolderPath"] + tempFileName + extension); //} //postedFile.SaveAs(newFullPath); } } //model.ImageUrl = ConfigurationManager.AppSettings["AdminImageFolderPath"] + Path.GetFileName(newFullPath); } #endregion if (model.Id == 0) { //BlogPosts post = new BlogPosts //{ // Title = model.Title, // CategoryType = model.CategoryType, // Description = model.Description, // DateOfPosting = model.DateOfPosting, // Admin_ID = model.Admin_ID, // is_deleted = 0 //}; ctx.BlogPosts.Add(model); ctx.SaveChanges(); model.ImageUrl = ConfigurationManager.AppSettings["BlogImageFolderPath"] + model.Id + fileExtension; newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["BlogImageFolderPath"] + model.Id + fileExtension); postedFile.SaveAs(newFullPath); ctx.SaveChanges(); } else { if (httpRequest.Files.Count == 0) { // Check if image deleted if (model.ImageDeletedOnEdit == false) { model.ImageUrl = existingModel.ImageUrl; } } else { newFullPath = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["BlogImageFolderPath"] + model.Id + fileExtension); postedFile.SaveAs(newFullPath); model.ImageUrl = ConfigurationManager.AppSettings["BlogImageFolderPath"] + model.Id + fileExtension; } ctx.Entry(existingModel).CurrentValues.SetValues(model); ctx.SaveChanges(); } CustomResponse <BlogPosts> response = new CustomResponse <BlogPosts> { Message = ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = model }; return(Ok(response)); } } catch (Exception ex) { return(StatusCode(DunkeyDelivery.Utility.LogError(ex))); } }