public HttpResponseMessage MakeResrvation(int TripID) { if (context.Reservations.Where(r => r.TripId == TripID).FirstOrDefault() != null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "This trip is already reserved")); } var CurrentUserID = context.UserMasters.FirstOrDefault(u => u.UserName == RequestContext.Principal.Identity.Name).UserID; var reservation = new Reservation(); reservation.TravellerId = CurrentUserID; reservation.TripId = TripID; try { context.Reservations.Add(reservation); context.notification_.Add(new notification_ { Message_ = "you requested to reserve this trip, please send 10.LE to this vodafone cash 01012345678 " + "and you will be notified when your request be accepted", RaiserID = context.trips.FirstOrDefault(t => t.ID == reservation.TripId).DriverId, ReceiverID = reservation.TravellerId, TypeOfNotification = "RequestReserveTrip" }); context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "your request have been sent")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage PostTrip([FromBody] TripDTO NewTrip) { var CurrentUser = context.UserMasters.Where(u => u.UserName == RequestContext.Principal.Identity.Name).FirstOrDefault(); if (CurrentUser.DriverLicense != null && (CurrentUser.Cars != null) && CurrentUser.Cars.Count > 0) { if (ModelState.IsValid) { try { var TripToAdd = new trip() { DateOfTrip = NewTrip.DateOfTrip, DriverId = CurrentUser.UserID, FromCity = NewTrip.FromCity, PlaceToMeet = NewTrip.PlaceToMeet, TimeOfPost = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Egypt Standard Time")), TimeOfTrip = NewTrip.TimeOfTrip, ToCity = NewTrip.ToCity, }; context.trips.Add(TripToAdd); context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, "Trip was successfully Added")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } } return(Request.CreateResponse(HttpStatusCode.BadRequest, NewTrip)); } try{ var message = Request.CreateResponse(HttpStatusCode.BadRequest, "You should enter your car details and your driving License"); message.Headers.Location = new Uri("https://seka.azurewebsites.net/" + "/api/HomePage/AddLicense/"); return(message); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage AddCar() { try { var CarToAdd = new Car(); var form = HttpContext.Current.Request.Form; CarToAdd.carColor = HttpContext.Current.Request.Form["carColor"]; CarToAdd.CarModel = HttpContext.Current.Request.Form["CarModel"]; CarToAdd.CarLicense = HttpContext.Current.Request.Form["CarLicense"]; CarToAdd.UserId = context.UserMasters.FirstOrDefault(u => u.UserName == (RequestContext.Principal.Identity.Name)).UserID; var files = HttpContext.Current.Request.Files; if (files == null || files.Count == 0) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "You must upload photo of car")); } foreach (string file in files) { var PostedImage = files[file]; var filename = PostedImage.FileName.NoRepeateInFileName(RequestContext.Principal.Identity.Name); var postedImagePath = HttpContext.Current.Server.MapPath("~/" + filename); PostedImage.SaveAs(postedImagePath); CarToAdd.CarImagePath = postedImagePath; } context.Cars.Add(CarToAdd); context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "Car added successfully")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage MakeRate(RateDto rateDto) { try { if (ModelState.IsValid) { var CurrentUser = context.UserMasters.FirstOrDefault(u => u.UserName == RequestContext.Principal.Identity.Name); var Driver = context.UserMasters.Find(rateDto.DriverId); if (Driver == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "this driver was not found")); } var Reservation = context.Reservations.FirstOrDefault(r => r.TravellerId == CurrentUser.UserID && context.trips.FirstOrDefault(t => t.ID == r.ID).DriverId == Driver.UserID); if (Reservation == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "you can't rate this car owner because you didn't travel with him before")); } var RecentlyRated = context.Ratings.FirstOrDefault(r => r.TravellerId == CurrentUser.UserID && r.DriverId == Driver.UserID); if (RecentlyRated != null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "you have recently rated this user")); } Rating rate = new Rating() { DriverId = rateDto.DriverId, TravellerId = CurrentUser.UserID, RateValue = rateDto.RateValue, }; Driver.DriverTotalRate = Driver.CalcTotalRate(); context.Ratings.Add(rate); context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "rate was made successfully")); } return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "you didnt complete some required fields")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public async Task <HttpResponseMessage> AddPhoto() { #region oldMethod /* * * var CurrentUserName = RequestContext.Principal.Identity.Name; * var CurrentUser = context.UserMasters.FirstOrDefault(c => c.UserName == CurrentUserName); * var ctx = HttpContext.Current; * var root = ctx.Server.MapPath("~/UsersPhotos"); * var Provider = new MultipartFormDataStreamProvider(root); * * * try * { * await Request.Content.ReadAsMultipartAsync(Provider); * string FilePath=string.Empty; * foreach (var file in Provider.FileData ) * { * var name = file.Headers.ContentDisposition.FileName; * * name=name.Trim('"'); * * var Extension = name.Substring(name.LastIndexOf(".")); * var NameWithoutExtension = name.Substring(0, name.LastIndexOf(".")); * * * var LocalFileName = file.LocalFileName; * NameWithoutExtension = NameWithoutExtension +CurrentUser.UserName+DateTime.Now; * List<string> charToRemove = new List<string>() * {"/",":" * * }; * foreach (var c in charToRemove ) * { * NameWithoutExtension = NameWithoutExtension.Replace(c, string.Empty); * * } * * name = NameWithoutExtension + Extension; * name= String.Concat(name.Where(c=>!char.IsWhiteSpace(c))); * FilePath = Path.Combine(root, name); * File.Move(LocalFileName, FilePath); * * * * } * CurrentUser.imagePath = FilePath; * var user= context.UserMasters.Where(u=>u.UserID==CurrentUser.UserID).FirstOrDefault(); * user.imagePath = CurrentUser.imagePath; * context.SaveChanges(); * return Request.CreateResponse(HttpStatusCode.Created, "Photo added successfully"); * * * } * catch (Exception ex) * { * * * return Request.CreateErrorResponse(HttpStatusCode.BadRequest,ex.InnerException); * } */ #endregion var CurrentUserName = RequestContext.Principal.Identity.Name; var CurrentUser = context.UserMasters.FirstOrDefault(c => c.UserName == CurrentUserName); var httpRequest = HttpContext.Current.Request; var files = httpRequest.Files; if (files.Count < 1) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "you must upload image")); } foreach (string file in files) { var postedFile = files[file]; var fileName = postedFile.FileName; fileName = fileName.NoRepeateInFileName(CurrentUser.UserName); var filePath = HttpContext.Current.Server.MapPath("~/" + fileName); postedFile.SaveAs(filePath); CurrentUser.imagePath = filePath; CurrentUser.ImageUrl = "~/" + fileName; } context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, "photo Added successfully")); //var folderName = Path.Combine("UsersPhotos"); //var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); //if (files.Any(f => f.Length == 0)) // throw new Exception(); }
public HttpResponseMessage UpdateProfile() { try { /*city , FullName , PhoneNumber, UserEmailID, carColor, CarModel, image file , CarLicense */ var currentUser = context.UserMasters.FirstOrDefault(u => u.UserName == RequestContext.Principal.Identity.Name); var files = HttpContext.Current.Request.Files; if (HttpContext.Current.Request.Form["city"] != null && HttpContext.Current.Request.Form["city"] != string.Empty) { currentUser.city = HttpContext.Current.Request.Form["city"]; } if (HttpContext.Current.Request.Form["FullName"] != null && HttpContext.Current.Request.Form["FullName"] != string.Empty) { currentUser.FullName = HttpContext.Current.Request.Form["FullName"]; } if (HttpContext.Current.Request.Form["PhoneNumber"] != null && HttpContext.Current.Request.Form["PhoneNumber"] != string.Empty) { currentUser.PhoneNumber = HttpContext.Current.Request.Form["PhoneNumber"]; } if (HttpContext.Current.Request.Form["UserEmailID"] != null && HttpContext.Current.Request.Form["UserEmailID"] != string.Empty) { currentUser.UserEmailID = HttpContext.Current.Request.Form["UserEmailID"]; } if (currentUser.DriverLicense == null || currentUser.DriverLicense == string.Empty) { if (HttpContext.Current.Request.Form["DriverLicense"] != null && HttpContext.Current.Request.Form["DriverLicense"] != string.Empty) { currentUser.DriverLicense = HttpContext.Current.Request.Form["DriverLicense"]; } } Car car = context.Cars.FirstOrDefault(c => c.UserId == currentUser.UserID); if (car == null) { car = new Car() { UserId = currentUser.UserID, CarLicense = "" }; context.Cars.Add(car); } var tset = HttpContext.Current.Request.Form["carColor"]; if ((tset != null && tset != string.Empty)) { car.carColor = HttpContext.Current.Request.Form["carColor"]; } if (HttpContext.Current.Request.Form["CarModel"] != null && HttpContext.Current.Request.Form["CarModel"] != string.Empty) { car.CarModel = HttpContext.Current.Request.Form["CarModel"]; } if (HttpContext.Current.Request.Form["CarLicense"] != null && HttpContext.Current.Request.Form["CarModel"] != string.Empty) { car.CarLicense = HttpContext.Current.Request.Form["CarLicense"]; } foreach (string file in files) { var PostedImage = files[file]; var fileNameWithExtension = PostedImage.FileName.NoRepeateInFileName(RequestContext.Principal.Identity.Name); var CarImagePath = HttpContext.Current.Server.MapPath("~/" + fileNameWithExtension); car.CarImagePath = CarImagePath; car.CarImageRelativeUrl = $"~/{fileNameWithExtension}"; PostedImage.SaveAs(CarImagePath); } context.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, "your Profile updated successfully")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }