/// <summary> /// Метод для создания или редактирования точки /// </summary> /// <param name="isNew">Если True, то новая точка</param> private void PointCreateEdit(bool isNew) { var logString = new StringBuilder(); var result = new StringResultDto(); try { var input = ResponseOperator.ConvertStringToValue<EditPointDto>(Context); #if DEBUG logString.Append(DateTime.Now + " " + "Распарсили объект:\r\n"); logString.Append(DateTime.Now + " " + input.ToJson().ToString() + "\r\n"); //ExeptionModel.WriteToLog("Распарсили объект:"); //ExeptionModel.WriteToLog(input.ToJson().ToString()); #endif var userId = input.Auth.Token; if (string.IsNullOrEmpty(userId)) { throw new AuthenticationException("Ошибка авторизации"); } try { DbUser user; using (var db = new DatabaseModel()) { var id = int.Parse(userId); user = db.UsersSet.FirstOrDefault(x => x.Id == id); } if (user == null) { throw new AuthenticationException("Ошибка авторизации"); } //var newPoint = db.PointsSet.Create(); //newPoint.Update(input.Point); //user.Points.Add(newPoint); //db.SaveChanges(); var poss = new[] { input.Point.PointDataDto.Longitude.ToString(CultureInfo.InvariantCulture), input.Point.PointDataDto.Latitude.ToString(CultureInfo.InvariantCulture) }; var a = new Anketa(); var text = a.GetResult(input.Point.PointDataDto); text = input.Point.PointDataDto.PointPart7Summary.OtherComments + "\n\n" + text; #if DEBUG logString.Append(DateTime.Now + " " + "Посчитали коэффициенты" + "\r\n"); //ExeptionModel.WriteToLog("Посчитали коэффициенты"); #endif if (!string.IsNullOrEmpty(input.Point.PointDataDto.Address) && (poss[0] == "0" || poss[1] == "0")) { #if DEBUG logString.Append(DateTime.Now + " " + "Координаты яндекса" + "\r\n"); //ExeptionModel.WriteToLog("Координаты яндекса"); #endif var coor = "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=" + input.Point.PointDataDto.Address; var requestToYa = (HttpWebRequest) WebRequest.Create(coor); var responseFromYa = (HttpWebResponse) requestToYa.GetResponse(); var streamReader = new StreamReader(responseFromYa.GetResponseStream()); coor = streamReader.ReadToEnd(); var o = JObject.Parse(coor); var pos = (string) o["response"]["GeoObjectCollection"]["featureMember"][0]["GeoObject"]["Point"]["pos" ]; poss = pos.Split(' '); #if DEBUG logString.Append(DateTime.Now + " " + "Яндекс посчитался" + "\r\n"); //ExeptionModel.WriteToLog("Яндекс посчитался"); #endif } var response = new StringBuilder("http://www.streetjournal.org/api2/"); response.Append(isNew ? "addissue?" : "editissue?"); response.Append("user="******"&pswd=" + user.Password); if (!isNew) response.Append("&id=" + input.Point.Id); response.Append("&latitude=" + poss[1] + "&longitude=" + poss[0]); response.Append("&name=" + input.Point.Name); response.Append("&address=" + input.Point.PointDataDto.Address); response.Append("&text=" + text); response.Append("&anonymous=false"); response.Append("&categorymain=" + 176 /*input.Point.Category*/); #warning закоменчена отправка дполнительной категории на АПИ. //response.Append("&categoryadd=" + input.Point.Category); #if DEBUG logString.Append(DateTime.Now + " " + "Ищем фотографию" + "\r\n"); //ExeptionModel.WriteToLog("Ищем фотографию"); #endif if (!string.IsNullOrEmpty(input.Point.PointDataDto.PhotoName)) { var pathToServer = AppDomain.CurrentDomain.BaseDirectory; var fileName = String.Format(GlobalContants.FilePathFormat, pathToServer, input.Point.PointDataDto.PhotoName); if (File.Exists(fileName)) { #if DEBUG logString.Append(DateTime.Now + " " + "Нашли фотографию" + "\r\n"); //ExeptionModel.WriteToLog("Нашли фотографию"); #endif var image = new Bitmap(fileName); #if DEBUG logString.Append(DateTime.Now + " " + "Ресайз фотографии" + "\r\n"); //ExeptionModel.WriteToLog("Ресайз фотографии"); #endif image = ResizePhoto(image); using (var ms = new MemoryStream()) { #if DEBUG logString.Append(DateTime.Now + " " + "Конвертация в Base64" + "\r\n"); //ExeptionModel.WriteToLog("Конвертация в Base64"); #endif image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); var imageBytes = ms.ToArray(); var photoStr = Convert.ToBase64String(imageBytes); #warning закоменчена отправка фотографии на АПИ. //response.Append(!string.IsNullOrEmpty(photoStr) // ? "&photo=" + photoStr // : string.Empty); } } } #if DEBUG logString.Append(DateTime.Now + " " + "response:" + response + "\r\n"); //ExeptionModel.WriteToLog("response:" + response); #endif var myHttpWebRequest = (HttpWebRequest) WebRequest.Create(response.ToString()); var myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); var myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream()); var str = myStreamReader.ReadToEnd(); var doc = new XmlDocument(); doc.LoadXml(str); if (str.Contains("Fault")) { result.Result = doc.GetElementsByTagName("Fault")[0].FirstChild.Value; } else { result.Result = "Ваша информация занесена на карту"; result.ResultObjectId = int.Parse(doc.GetElementsByTagName("Issue")[0].Attributes["Id"].Value); } } catch (Exception ex) { result = new StringResultDto(ex.Message); logString.Append(DateTime.Now + " " + "Token:" + input.Auth.Token + " request: " + input.ToJson() + "\r\n"); logString.Append(DateTime.Now + " " + "Token:" + input.Auth.Token + " " + ex.Message + " " + ex.StackTrace + "\r\n"); ExeptionModel.WriteToLog("Token:" + input.Auth.Token + " request: " + input.ToJson()); ExeptionModel.WriteToLog("Token:" + input.Auth.Token + " " + ex.Message + " " + ex.StackTrace); } } catch (Exception ex) { result = new StringResultDto(ex.Message); logString.Append(DateTime.Now + " " + ex.Message + " " + ex.StackTrace + "\r\n"); ExeptionModel.WriteToLog(ex.Message + " " + ex.StackTrace); } #if DEBUG logString.Append(DateTime.Now + " " + result.ToJson().ToString() + "\r\n"); ExeptionModel.WriteToLog(logString.ToString()); //ExeptionModel.WriteToLog(result.ToJson().ToString()); #endif ResponseOperator.ConvertValueToString(result, Context); }
public void MyPoints() { #if DEBUG ExeptionModel.WriteToLog("Вошли в MyPoints()"); #endif //using (var reader = new StreamReader(Context.Request.InputStream)) //{ // var res = reader.ReadToEnd(); // ExeptionModel.WriteToLog(res); // return; //} var result = new PointsSetDto(); try { var input = ResponseOperator.ConvertStringToValue<AuthDto>(Context); #if DEBUG ExeptionModel.WriteToLog("Распарсили объект:"); ExeptionModel.WriteToLog(input.ToJson().ToString()); #endif var userId = input.Token; if (string.IsNullOrEmpty(userId)) throw new AuthenticationException("Ошибка авторизации"); DbUser user; using (var db = new DatabaseModel()) { var id = int.Parse(userId); user = db.UsersSet.FirstOrDefault(x => x.Id == id); } var response = new StringBuilder("http://www.streetjournal.org/api2/issuelist?"); response.Append("user="******"&pswd=" + user.Password); response.Append("&latitude=" + 58.004785.ToString(CultureInfo.InvariantCulture) + "&longitude=" + 56.237654.ToString(CultureInfo.InvariantCulture)); response.Append("&radius=" + 10000); response.Append("&isself=true"); #if DEBUG ExeptionModel.WriteToLog("response:" + response); #endif var myHttpWebRequest = (HttpWebRequest) WebRequest.Create(response.ToString()); var myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); var myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream()); var str = myStreamReader.ReadToEnd(); var doc = new XmlDocument(); doc.LoadXml(str); if (str.Contains("Fault")) { result.Message = doc.GetElementsByTagName("Fault")[0].FirstChild.Value; // var exept = new ExeptionModel(); // var js = exept.ConvertToJSON(str); // //var jsonstring = exept.ConvertToJSON(str); // exept.FromJson(JObject.Parse(js)); // result.Result = exept.Fault.Message; } else { result = new PointsSetDto(); var a = new List<PointDto>(); foreach (XmlNode node in doc.GetElementsByTagName("Item")) { //if (bool.Parse(node.Attributes["isSelf"].Value)) { string str1 = null; if (node.FirstChild.HasChildNodes) { var s = node.FirstChild.FirstChild.Attributes["Value"].Value; var myHttpWebRequest1 = (HttpWebRequest) WebRequest.Create(s); var myHttpWebResponse1 = (HttpWebResponse) myHttpWebRequest1.GetResponse(); var myStreamReader1 = new StreamReader(myHttpWebResponse1.GetResponseStream()); str1 = myStreamReader1.ReadToEnd(); } a.Add(new PointDto { Id = int.Parse(node.Attributes["Id"].Value), Name = node.Attributes["Name"].Value, Category = 0, PointDataDto = new PointDataDto { Address = node.Attributes["Address"].Value, Latitude = double.Parse(node.Attributes["Lat"].Value, CultureInfo.InvariantCulture), Longitude = double.Parse(node.Attributes["Lng"].Value, CultureInfo.InvariantCulture), PhotoName = !string.IsNullOrEmpty(str1) ? str1 : null } }); } } result.Points = a.ToArray(); } //result = new PointsSetDto // { // Points = user.Points.Select(p => p.ToDto()).ToArray() // }; //} } catch (Exception ex) { result.Message = ex.Message; ExeptionModel.WriteToLog(ex.Message + " " + ex.StackTrace); } #if DEBUG ExeptionModel.WriteToLog(result.ToJson().ToString()); #endif ResponseOperator.ConvertValueToString(result, Context); }
public void RemovePoint() { var input = ResponseOperator.ConvertStringToValue<EditPointDto>(Context); var result = new StringResultDto(); int userId = InternalSessionManager.GetUserId(input.Auth.Token); if (userId <= 0) { result = new StringResultDto("Ошибка авторизации"); } else { try { using (var db = new DatabaseModel()) { DbUser user = db.UsersSet.Find(userId); int pointId = input.Point.Id; DbPoint oldPoint = user.Points.FirstOrDefault(p => p.Id == pointId); //Именно тут, чтобы не удалить чужое if (oldPoint == null) { result = new StringResultDto("Редактируемая точка не найден"); } else { user.Points.Remove(oldPoint); db.SaveChanges(); } } } catch (Exception ex) { result = new StringResultDto(ex.Message); } } ResponseOperator.ConvertValueToString(result, Context); }
/// <summary> /// Сохранение или получение фотки с сервера сервиса /// </summary> /// <param name="isSave">True - SavePhoto, False - LoadPhoto</param> private void PhotoSaveLoad(bool isSave) { var result = new StringResultDto(); try { var input = ResponseOperator.ConvertStringToValue<PointPhotoDto>(Context); #if DEBUG ExeptionModel.WriteToLog("Распарсили объект:"); ExeptionModel.WriteToLog(input.ToJson().ToString()); #endif var userId = input.Auth.Token; if (string.IsNullOrEmpty(userId)) { throw new AuthenticationException("Ошибка авторизации"); } try { using (var db = new DatabaseModel()) { var id = int.Parse(userId); var user = db.UsersSet.FirstOrDefault(x => x.Id == id); var pointId = input.Id; if (user == null) { throw new AuthenticationException("Ошибка авторизации"); } //var oldPoint = user.Points.FirstOrDefault(p => p.Id == pointId); ////Именно тут, чтобы не удалить чужое //if (oldPoint == null) //{ // result = new StringResultDto("Редактируемая точка не найдена"); //} //else //{ var photosDir = GetPhotosDir(); if (!Directory.Exists(photosDir)) { result.Result = "Отсутствует дирректория для фото"; #if DEBUG ExeptionModel.WriteToLog("Отсутствует дирректория для фото"); #endif } else { var fileName = Path.Combine(photosDir, input.Id + ".jpg"); if (isSave) { File.WriteAllBytes(fileName, Convert.FromBase64String(input.PhotoBase64)); #if DEBUG ExeptionModel.WriteToLog("Создали фото: " + fileName); #endif } else { if (File.Exists(fileName)) { result.Result = Convert.ToBase64String(File.ReadAllBytes(fileName)); result.ResultObjectId = pointId; } else { result.Result = "Фото не найдено"; result.ResultObjectId = 0; } #if DEBUG ExeptionModel.WriteToLog("Вернули фото: " + fileName); #endif } } /*oldPoint.Photo = Convert.FromBase64String(input.PhotoBase64); db.SaveChanges();*/ //} } } catch (Exception ex) { result = new StringResultDto(ex.Message); ExeptionModel.WriteToLog("Token:" + input.Auth.Token + " request: " + input.ToJson()); ExeptionModel.WriteToLog("Token:" + input.Auth.Token + " " + ex.Message + " " + ex.StackTrace); } } catch (Exception ex) { result = new StringResultDto(ex.Message); ExeptionModel.WriteToLog(ex.Message + " " + ex.StackTrace); } #if DEBUG ExeptionModel.WriteToLog(result.ToJson().ToString()); #endif ResponseOperator.ConvertValueToString(result, Context); }
public void Login() { var input = ResponseOperator.ConvertStringToValue<LoginRequestDto>(Context); /* * var v = new AuthDto { Token = 1.ToString(CultureInfo.InvariantCulture) }; ResponseOperator.ConvertValueToString(v, Context); return; */ using (var model = new DatabaseModel()) { AuthDto value; var response = string.Format( "http://www.streetjournal.org/api2/validateuser?" + "user={0}&pswd={1}&", input.Login, input.Password); var myHttpWebRequest = (HttpWebRequest) WebRequest.Create(response); var myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); var myStreamReader = new StreamReader(myHttpWebResponse.GetResponseStream()); var result = myStreamReader.ReadToEnd(); if (result.Contains("Fault")) { //#if DEBUG // if (!model.UsersSet.Any()) // { // model.UsersSet.Add(new DbUser // { // Login = "******", // Password = "******" // }); // model.SaveChanges(); // } //#endif value = new AuthDto { Token = null }; ResponseOperator.ConvertValueToString(value, Context); } else { var name = input.Login.ToUpperInvariant(); var pass = input.Password; var user = model.UsersSet.FirstOrDefault(u => u.Login == name); if (user == null) { user = model.UsersSet.Add(new DbUser { Login = name, Password = pass }); model.SaveChanges(); } else { if (user.Password != pass) { user.Password = pass; model.SaveChanges(); } } value = new AuthDto { Token = user.Id.ToString(CultureInfo.InvariantCulture) }; ResponseOperator.ConvertValueToString(value, Context); } } }