public IHttpActionResult GetAppUserById(int userId) { try { var appUser = _unitOfWork.AppUsers.Get(userId); var jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) != null && HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') == eTag) { return(new StatusCodeResult(HttpStatusCode.NotModified, new HttpRequestMessage())); } return(Ok(appUser)); } catch { return(BadRequest("Data could not be retrieved, try to relog.")); } }
public IHttpActionResult GetServiceOffice(int officeID) { Office office; try { office = _unitOfWork.Offices.Get(officeID); } catch { return(BadRequest("Office does not exist")); } if (office == null) { return(BadRequest("Office does not exist")); } var jsonObj = JsonConvert.SerializeObject(office, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) != null && HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') == eTag) { return(new StatusCodeResult(HttpStatusCode.NotModified, new HttpRequestMessage())); } return(Ok(office)); }
public IHttpActionResult EditTypeOfVehicle(TypeOfVehicle type) { TypeOfVehicle typeOfVehicle = _unitOfWork.TypesOfVehicles.Get(type.TypeId); if (typeOfVehicle == null) { return(BadRequest("This Vehicle Type can't be found")); } var jsonObj = JsonConvert.SerializeObject(typeOfVehicle, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } typeOfVehicle.Type = type.Type.Trim(); try { _unitOfWork.TypesOfVehicles.Update(typeOfVehicle); _unitOfWork.Complete(); } catch { return(BadRequest("Vehicle Type can't be edited")); } jsonObj = JsonConvert.SerializeObject(typeOfVehicle, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Ok(typeOfVehicle)); }
public IHttpActionResult DisableVehicle(int vehicleId, bool enabled) { Vehicle vehicle = _unitOfWork.Vehicles.Get(vehicleId); if (vehicle == null) { return(BadRequest("Office does not exist")); } var jsonObj = JsonConvert.SerializeObject(vehicle, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } if (vehicle.Available == true) { vehicle.Enabled = enabled; _unitOfWork.Vehicles.Update(vehicle); _unitOfWork.Complete(); jsonObj = JsonConvert.SerializeObject(vehicle, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Ok(vehicle)); } else { return(BadRequest("Vehicle is currently rented")); } }
public IHttpActionResult GetTypeOfVehicle(int typeId) { TypeOfVehicle typeOfVehicle = _unitOfWork.TypesOfVehicles.Get(typeId); if (typeOfVehicle == null) { return(BadRequest("This Vehicle Type cant be found")); } var jsonObj = JsonConvert.SerializeObject(typeOfVehicle, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) != null && HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') == eTag) { return(new StatusCodeResult(HttpStatusCode.NotModified, new HttpRequestMessage())); } return(Ok(typeOfVehicle)); }
public IHttpActionResult EditOffice() { var httpRequest = HttpContext.Current.Request; int officeId = Int32.Parse(httpRequest["OfficeId"]); Office office = _unitOfWork.Offices.Get(officeId); if (office == null) { return(BadRequest("Office does not exist")); } var jsonObj = JsonConvert.SerializeObject(office, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } string imageName = null; office.Address = httpRequest["Address"].Trim(); var numberFormat = (System.Globalization.NumberFormatInfo)System.Globalization.CultureInfo.InstalledUICulture.NumberFormat.Clone(); numberFormat.NumberDecimalSeparator = "."; office.Latitude = double.Parse(httpRequest["Latitude"], numberFormat); office.Longitude = double.Parse(httpRequest["Longitude"], numberFormat); try { _unitOfWork.Offices.Update(office); _unitOfWork.Complete(); } catch { return(BadRequest("Office could not be editer")); } var postedFile = httpRequest.Files["Picture"]; if (postedFile != null) { imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); if (office.Picture != imageName && File.Exists(HttpRuntime.AppDomainAppPath + "Images\\" + office.Picture)) { if (File.Exists(HttpRuntime.AppDomainAppPath + "Images\\" + office.Picture)) { File.Delete(HttpRuntime.AppDomainAppPath + "Images\\" + office.Picture); } imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName); postedFile.SaveAs(filePath); office.Picture = imageName; } } jsonObj = JsonConvert.SerializeObject(office, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Created("Office was edited", office)); }
public IHttpActionResult EditVehicle() { var httpRequest = HttpContext.Current.Request; int vehicleId = Int32.Parse(httpRequest["VehicleId"]); Vehicle vehicle = _unitOfWork.Vehicles.Get(vehicleId); if (vehicle == null) { return(BadRequest("Office does not exist")); } var jsonObj = JsonConvert.SerializeObject(vehicle, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } int numberOfImages = Int32.Parse(httpRequest["ImagesNum"]); vehicle.Model = httpRequest["Model"].Trim(); vehicle.Description = httpRequest["Description"].Trim(); vehicle.Manufacturer = httpRequest["Manufacturer"].Trim(); vehicle.YearOfManufacturing = httpRequest["YearOfManufacturing"]; vehicle.TypeId = Int32.Parse(httpRequest["TypeId"]); vehicle.HourlyPrice = double.Parse(httpRequest["HourlyPrice"]); try { _unitOfWork.Vehicles.Update(vehicle); _unitOfWork.Complete(); } catch { return(BadRequest("Vehicle could not be editer")); } List <VehiclePicture> pictures = _unitOfWork.VehiclePictures.Find(x => x.VehicleId == vehicleId).ToList(); if (numberOfImages > 0) { List <PicData> picsData = new List <PicData>(); for (int i = 0; i < numberOfImages; i++) { var postedFile = httpRequest.Files[String.Format("Image{0}", i)]; var imgName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).ToArray()).Replace(" ", "-") + Path.GetExtension(postedFile.FileName); if (imgName == "default-placeholder.png") { continue; } picsData.Add(new PicData() { name = imgName, position = i }); } foreach (VehiclePicture picture in pictures) { PicData picData = picsData.Find(x => x.name == picture.Data); if (picData == null) { if (File.Exists(HttpRuntime.AppDomainAppPath + "Images\\" + picture.Data)) { File.Delete(HttpRuntime.AppDomainAppPath + "Images\\" + picture.Data); } _unitOfWork.VehiclePictures.Remove(picture); _unitOfWork.Complete(); } else { picsData.Remove(picData); } } foreach (PicData picData in picsData) { var postedFile = httpRequest.Files[String.Format("Image{0}", picData.position)]; picData.name = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); picData.name = picData.name + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Images/" + picData.name); postedFile.SaveAs(filePath); _unitOfWork.VehiclePictures.Add(new VehiclePicture() { Data = picData.name, VehicleId = vehicle.VehicleId }); _unitOfWork.Complete(); } } jsonObj = JsonConvert.SerializeObject(vehicle, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Created("Vehicle was edited", vehicle)); }
public IHttpActionResult ActivateUser(int userId, bool activated) { AppUser appUser = _unitOfWork.AppUsers.Get(userId); if (appUser == null) { return(BadRequest("User does not exist")); } var jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } MailMessage mail = new MailMessage("*****@*****.**", "*****@*****.**"); SmtpClient client = new SmtpClient(); client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("*****@*****.**", "e3942014pusgs2018"); client.Host = "smtp.gmail.com"; client.EnableSsl = true; mail.From = new MailAddress("*****@*****.**"); mail.To.Add(appUser.Email); if (activated) { appUser.Activated = true; mail.Subject = "Profile approved"; mail.Body = "Your profile was approved by our administrators!"; } else { appUser.Activated = false; mail.Subject = "Profile wasn't approved"; mail.Body = "Unfortunately your profile wasn't approved. Try changing your personal information."; if (File.Exists(HttpRuntime.AppDomainAppPath + "Images\\" + appUser.DocumentPicture)) { File.Delete(HttpRuntime.AppDomainAppPath + "Images\\" + appUser.DocumentPicture); appUser.DocumentPicture = null; } } appUser.ProfileEdited = false; try { _unitOfWork.AppUsers.Update(appUser); _unitOfWork.Complete(); } catch { return(BadRequest(string.Format("User could not be {0}", activated?"activated":"deactivated"))); } try { client.Send(mail); } catch { } jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Ok(appUser)); }
public IHttpActionResult EditUser() { var httpRequest = HttpContext.Current.Request; string imageName = null; AppUser appUser; try { var username = User.Identity.Name; var user = _unitOfWork.AppUsers.Find(u => u.Email == username).FirstOrDefault(); if (user == null) { return(BadRequest("Data could not be retrieved, try to relog.")); } appUser = user; } catch { return(BadRequest("Data could not be retrieved, try to relog.")); } var jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } appUser.FullName = httpRequest["FullName"].Trim(); appUser.BirthDate = DateTime.Parse(httpRequest["BirthDate"]); appUser.Email = httpRequest["Email"].Trim(); appUser.ProfileEdited = true; if (appUser.DocumentPicture == null || appUser.DocumentPicture == "") { var postedFile = httpRequest.Files["Image"]; if (postedFile != null) { imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName); appUser.DocumentPicture = imageName; byte[] fileData = null; using (var binaryReader = new BinaryReader(postedFile.InputStream)) { fileData = binaryReader.ReadBytes(postedFile.ContentLength); } string eSecretKey = SecretKey.LoadKey(HttpRuntime.AppDomainAppPath + "Images\\SecretKey.txt"); AES_Symm_Algorithm.EncryptFile(fileData, filePath, eSecretKey); } } jsonObj = JsonConvert.SerializeObject(appUser, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); try { _unitOfWork.AppUsers.Update(appUser); _unitOfWork.Complete(); } catch { return(BadRequest("Profile could not be edited.")); } return(Ok(appUser)); }
public IHttpActionResult ActivateRentService(int serviceId, bool activated) { RentService rentService = _unitOfWork.RentServices.Get(serviceId); if (rentService == null) { return(BadRequest("Rent Service does not exist")); } var jsonObj = JsonConvert.SerializeObject(rentService, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } rentService.Activated = activated; rentService.ServiceEdited = false; try { _unitOfWork.RentServices.Update(rentService); _unitOfWork.Complete(); } catch { return(BadRequest("Rent Service cound not be activated")); } MailMessage mail = new MailMessage("*****@*****.**", "*****@*****.**"); SmtpClient client = new SmtpClient(); client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("*****@*****.**", "e3942014pusgs2018"); client.Host = "smtp.gmail.com"; client.EnableSsl = true; mail.From = new MailAddress("*****@*****.**"); mail.To.Add(rentService.User.Email); if (activated) { mail.Subject = "Rent Service approved"; mail.Body = string.Format("Your Rent Service '{0}' was approved by our administrators!", rentService.Name); } else { mail.Subject = "Rent Service wasn't approved"; mail.Body = string.Format("Unfortunately your Rent Service '{0}' wasn't approved!", rentService.Name); } try { client.Send(mail); } catch { } jsonObj = JsonConvert.SerializeObject(rentService, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Ok(string.Format("Rent Service was {0}", activated == true?"activated":"deactivated"))); }
public IHttpActionResult EditRentService() { var httpRequest = HttpContext.Current.Request; int serviceId = Int32.Parse(httpRequest["RentServiceId"]); RentService service = _unitOfWork.RentServices.Get(serviceId); if (service == null) { return(BadRequest("Rent Service does not exist")); } var jsonObj = JsonConvert.SerializeObject(service, Formatting.None, setting); var eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); if (HttpContext.Current.Request.Headers.Get(ETagHelper.MATCH_HEADER) == null || HttpContext.Current.Request.Headers[ETagHelper.MATCH_HEADER].Trim('"') != eTag) { HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(new StatusCodeResult(HttpStatusCode.PreconditionFailed, new HttpRequestMessage())); } string imageName = null; service.Name = httpRequest["Name"].Trim(); service.Description = httpRequest["Description"].Trim(); service.Email = httpRequest["Email"].Trim(); service.Activated = false; service.ServiceEdited = true; var postedFile = httpRequest.Files["Logo"]; if (postedFile != null) { if (File.Exists(HttpRuntime.AppDomainAppPath + "Images\\" + service.Logo)) { File.Delete(HttpRuntime.AppDomainAppPath + "Images\\" + service.Logo); } imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-"); imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName); var filePath = HttpContext.Current.Server.MapPath("~/Images/" + imageName); postedFile.SaveAs(filePath); service.Logo = imageName; } try { _unitOfWork.RentServices.Update(service); _unitOfWork.Complete(); } catch { return(BadRequest("Rent Service could not be edited")); } NotificationsHub.NotifyAdmin("New Rent Service was edited"); jsonObj = JsonConvert.SerializeObject(service, Formatting.None, setting); eTag = ETagHelper.GetETag(Encoding.UTF8.GetBytes(jsonObj)); HttpContext.Current.Response.Headers.Add("Access-Control-Expose-Headers", ETagHelper.ETAG_HEADER); HttpContext.Current.Response.Headers.Add(ETagHelper.ETAG_HEADER, JsonConvert.SerializeObject(eTag)); return(Created("Rent Service was edited", service)); }